Skip to content
This repository was archived by the owner on Jan 28, 2025. It is now read-only.

Commit 704120b

Browse files
authored
feat: add custom inputs for lambda name (#283)
* feat: add custom inputs for lambda name - closes #232
1 parent 476e529 commit 704120b

File tree

4 files changed

+107
-4
lines changed

4 files changed

+107
-4
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ myNextApplication:
175175

176176
Note the maximum timeout allowed for Lambda@Edge is 30 seconds. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html
177177

178+
You can also set a custom name for **default** and **api** lambdas - if not the default is set by the [aws-lambda serverless component](https://github.com/serverless-components/aws-lambda) to the resource id:
179+
180+
```yml
181+
# serverless.yml
182+
183+
myNextApplication:
184+
component: serverless-next.js
185+
inputs:
186+
name:
187+
defaultLambda: fooDefaultLambda
188+
apiLambda: fooApiLambda
189+
```
190+
178191
### Architecture
179192

180193
![architecture](./arch_no_grid.png)
@@ -205,6 +218,7 @@ The fourth cache behaviour handles next API requests `api/*`.
205218
| nextStaticDir | `string` | `./` | If your `static` or `public` directory is not a direct child of `nextConfigDir` this is needed |
206219
| memory | `number\|object` | `512` | When assigned a number, both the default and api lambdas will assigned memory of that value. When assigned to an object, values for the default and api lambdas can be separately defined | |
207220
| timeout | `number\|object` | `10` | Same as above |
221+
| name | `string\|object` | / | When assigned a string, both the default and api lambdas will assigned name of that value. When assigned to an object, values for the default and api lambdas can be separately defined |
208222
| build | `boolean\|object` | `true` | When true builds and deploys app, when false assume the app has been built and uses the `.next` `.serverless_nextjs` directories in `nextConfigDir` to deploy. If an object is passed `build` allows for overriding what script gets called and with what arguments. |
209223
| build.cmd | `string` | `node_modules/.bin/next` | Build command |
210224
| build.args | `Array\|string` | `['build']` | Arguments to pass to the build |

packages/serverless-nextjs-component/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,19 @@ myNextApplication:
175175

176176
Note the maximum timeout allowed for Lambda@Edge is 30 seconds. See https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-requirements-limits.html
177177

178+
You can also set a custom name for **default** and **api** lambdas - if not the default is set by the [aws-lambda serverless component](https://github.com/serverless-components/aws-lambda) to the resource id:
179+
180+
```yml
181+
# serverless.yml
182+
183+
myNextApplication:
184+
component: serverless-next.js
185+
inputs:
186+
name:
187+
defaultLambda: fooDefaultLambda
188+
apiLambda: fooApiLambda
189+
```
190+
178191
### Architecture
179192

180193
![architecture](./arch_no_grid.png)
@@ -205,6 +218,7 @@ The fourth cache behaviour handles next API requests `api/*`.
205218
| nextStaticDir | `string` | `./` | If your `static` or `public` directory is not a direct child of `nextConfigDir` this is needed |
206219
| memory | `number\|object` | `512` | When assigned a number, both the default and api lambdas will assigned memory of that value. When assigned to an object, values for the default and api lambdas can be separately defined | |
207220
| timeout | `number\|object` | `10` | Same as above |
221+
| name | `string\|object` | / | When assigned a string, both the default and api lambdas will assigned name of that value. When assigned to an object, values for the default and api lambdas can be separately defined |
208222
| build | `boolean\|object` | `true` | When true builds and deploys app, when false assume the app has been built and uses the `.next` `.serverless_nextjs` directories in `nextConfigDir` to deploy. If an object is passed `build` allows for overriding what script gets called and with what arguments. |
209223
| build.cmd | `string` | `node_modules/.bin/next` | Build command |
210224
| build.args | `Array\|string` | `['build']` | Arguments to pass to the build |

packages/serverless-nextjs-component/__tests__/custom-inputs.test.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,4 +196,64 @@ describe("Custom inputs", () => {
196196
);
197197
});
198198
});
199+
200+
describe.each([
201+
[undefined, { defaultName: undefined, apiName: undefined }],
202+
[{}, { defaultName: undefined, apiName: undefined }],
203+
["fooFunction", { defaultName: "fooFunction", apiName: "fooFunction" }],
204+
[
205+
{ defaultLambda: "fooFunction" },
206+
{ defaultName: "fooFunction", apiName: undefined }
207+
],
208+
[
209+
{ apiLambda: "fooFunction" },
210+
{ defaultName: undefined, apiName: "fooFunction" }
211+
],
212+
[
213+
{ defaultLambda: "fooFunction", apiLambda: "barFunction" },
214+
{ defaultName: "fooFunction", apiName: "barFunction" }
215+
]
216+
])("Lambda name input", (inputName, expectedName) => {
217+
let tmpCwd;
218+
const fixturePath = path.join(__dirname, "./fixtures/generic-fixture");
219+
220+
beforeEach(async () => {
221+
execa.mockResolvedValueOnce();
222+
223+
tmpCwd = process.cwd();
224+
process.chdir(fixturePath);
225+
226+
mockCloudFront.mockResolvedValueOnce({
227+
url: "https://cloudfrontdistrib.amazonaws.com"
228+
});
229+
230+
const component = new NextjsComponent();
231+
componentOutputs = await component.default({
232+
name: inputName
233+
});
234+
});
235+
it(`sets default lambda name to ${expectedName.defaultName} and api lambda name to ${expectedName.apiName}`, () => {
236+
const { defaultName, apiName } = expectedName;
237+
238+
// Default Lambda
239+
const expectedDefaultObject = {
240+
code: path.join(fixturePath, DEFAULT_LAMBDA_CODE_DIR)
241+
};
242+
if (defaultName) expectedDefaultObject.name = defaultName;
243+
244+
expect(mockLambda).toBeCalledWith(
245+
expect.objectContaining(expectedDefaultObject)
246+
);
247+
248+
// Api Lambda
249+
const expectedApiObject = {
250+
code: path.join(fixturePath, API_LAMBDA_CODE_DIR)
251+
};
252+
if (apiName) expectedApiObject.name = apiName;
253+
254+
expect(mockLambda).toBeCalledWith(
255+
expect.objectContaining(expectedApiObject)
256+
);
257+
});
258+
});
199259
});

packages/serverless-nextjs-component/serverless.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -395,8 +395,13 @@ class NextjsComponent extends Component {
395395
? inputs.timeout
396396
: (inputs.timeout && inputs.timeout[lambdaType]) || 10;
397397

398+
const getLambdaName = lambdaType =>
399+
typeof inputs.name === "string"
400+
? inputs.name
401+
: inputs.name && inputs.name[lambdaType];
402+
398403
if (hasAPIPages) {
399-
apiEdgeLambdaOutputs = await apiEdgeLambda({
404+
const apiEdgeLambdaInput = {
400405
description: "API Lambda@Edge for Next CloudFront distribution",
401406
handler: "index.handler",
402407
code: join(nextConfigPath, API_LAMBDA_CODE_DIR),
@@ -410,7 +415,11 @@ class NextjsComponent extends Component {
410415
},
411416
memory: getLambdaMemory("apiLambda"),
412417
timeout: getLambdaTimeout("apiLambda")
413-
});
418+
};
419+
const apiLambdaName = getLambdaName("apiLambda");
420+
if (apiLambdaName) apiEdgeLambdaInput.name = apiLambdaName;
421+
422+
apiEdgeLambdaOutputs = await apiEdgeLambda(apiEdgeLambdaInput);
414423

415424
apiEdgeLambdaPublishOutputs = await apiEdgeLambda.publishVersion();
416425

@@ -431,7 +440,7 @@ class NextjsComponent extends Component {
431440
};
432441
}
433442

434-
const defaultEdgeLambdaOutputs = await defaultEdgeLambda({
443+
const defaultEdgeLambdaInput = {
435444
description: "Default Lambda@Edge for Next CloudFront distribution",
436445
handler: "index.handler",
437446
code: join(nextConfigPath, DEFAULT_LAMBDA_CODE_DIR),
@@ -445,7 +454,13 @@ class NextjsComponent extends Component {
445454
},
446455
memory: getLambdaMemory("defaultLambda"),
447456
timeout: getLambdaTimeout("defaultLambda")
448-
});
457+
};
458+
const defaultLambdaName = getLambdaName("defaultLambda");
459+
if (defaultLambdaName) defaultEdgeLambdaInput.name = defaultLambdaName;
460+
461+
const defaultEdgeLambdaOutputs = await defaultEdgeLambda(
462+
defaultEdgeLambdaInput
463+
);
449464

450465
const defaultEdgeLambdaPublishOutputs = await defaultEdgeLambda.publishVersion();
451466

0 commit comments

Comments
 (0)