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

Commit b176d0a

Browse files
committed
feat(lambda-config): CLI configuration for memory and timeout
1 parent fb98377 commit b176d0a

File tree

6 files changed

+24
-14
lines changed

6 files changed

+24
-14
lines changed

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ With this, you can pass `.env` files during build time and overwrite / extend th
109109

110110
### Via CDK
111111

112-
See `NextStandaloneStack` construct in `cdk/example.ts`.
113-
Or just use `next-utils deploy` command so you don't have to manage CDK yourself.
112+
See `NextStandaloneStack` construct in `lib/cdk-app.ts`.
113+
Or just use `next-utils deploy` command so you don't have to manage CDK yourself. See CLI help command for all congiruation, notably, it's possible to set Timeout and Memory for lambda from CLI. It is advised to always use custom `--stackName` in `deploy` command as it will affect names of all resources and will help you distinguish between different environments/applications.
114114

115115
#### Benchmark
116116

Diff for: lib/cdk-app.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class NextStandaloneStack extends Stack {
2929
customServerHandler: 'handler.handler',
3030
customImageHandler: 'handler.handler',
3131
cfnViewerCertificate: undefined,
32+
lambdaTimeout: process.env.LAMBDA_TIMEOUT ? Number(process.env.LAMBDA_TIMEOUT) : 512,
33+
lambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.LAMBDA_MEMORY) : 1024,
3234
...props,
3335
}
3436

@@ -46,8 +48,8 @@ class NextStandaloneStack extends Stack {
4648
handler: config.customServerHandler,
4749
layers: [depsLayer],
4850
// No need for big memory as image handling is done elsewhere.
49-
memorySize: 512,
50-
timeout: Duration.seconds(15),
51+
memorySize: config.lambdaMemory,
52+
timeout: Duration.seconds(config.lambdaTimeout),
5153
environment: {
5254
// Set env vars based on what's available in environment.
5355
...Object.entries(process.env)

Diff for: lib/cli.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,12 @@ program
8080
.option('--stackName <name>', 'Name of the stack to be deployed.', 'StandaloneNextjsStack-Temporary')
8181
.option('--appPath <path>', 'Absolute path to app.', path.resolve(__dirname, '../dist/cdk-app.js'))
8282
.option('--bootstrap', 'Bootstrap CDK stack.', false)
83+
.option('--lambdaTimeout <sec>', 'Set timeout for lambda function handling server requirests', Number, 15)
84+
.option('--lambdaMemory <mb>', 'Set memory for lambda function handling server requirests', Number, 512)
8385
.action(async (options) => {
8486
console.log('Our config is: ', options)
85-
const { stackName, appPath, bootstrap } = options
86-
wrapProcess(deployHandler({ stackName, appPath, bootstrap }))
87+
const { stackName, appPath, bootstrap, lambdaTimeout, lambdaMemory } = options
88+
wrapProcess(deployHandler({ stackName, appPath, bootstrap, lambdaTimeout, lambdaMemory }))
8789
})
8890

8991
program

Diff for: lib/cli/deploy.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,29 @@ interface Props {
44
stackName: string
55
appPath: string
66
bootstrap: boolean
7+
lambdaMemory?: number
8+
lambdaTimeout?: number
79
}
810

911
const cdkExecutable = require.resolve('aws-cdk/bin/cdk')
1012

11-
export const deployHandler = async ({ stackName, appPath, bootstrap }: Props) => {
13+
export const deployHandler = async ({ stackName, appPath, bootstrap, lambdaMemory, lambdaTimeout }: Props) => {
1214
// All paths are absolute.
1315
const cdkApp = `node ${appPath}`
1416
const cdkCiFlags = `--require-approval never --ci`
17+
const envConfig = [
18+
`STACK_NAME=${stackName}`,
19+
lambdaMemory ? `LAMBDA_MEMORY=${lambdaMemory}` : '',
20+
lambdaTimeout ? `LAMBDA_TIMEOUT=${lambdaTimeout}` : '',
21+
].join(' ')
1522

1623
if (bootstrap) {
1724
await executeAsyncCmd({
18-
cmd: `STACK_NAME=${stackName} ${cdkExecutable} bootstrap --app "${cdkApp}"`,
25+
cmd: `${envConfig} ${cdkExecutable} bootstrap --app "${cdkApp}"`,
1926
})
2027
}
2128

2229
await executeAsyncCmd({
23-
cmd: `STACK_NAME=${stackName} ${cdkExecutable} deploy --app "${cdkApp}" ${cdkCiFlags}`,
30+
cmd: `${envConfig} ${cdkExecutable} deploy --app "${cdkApp}" ${cdkCiFlags}`,
2431
})
2532
}

Diff for: lib/cli/pack.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export const packHandler = async ({ handlerPath, outputFolder, publicFolder, sta
4747
rmSync(outputFolder, { force: true, recursive: true })
4848
mkdirSync(outputFolder)
4949

50+
// @TODO: We need to include nested node_modules in case of mono-repos.
5051
// Zip dependencies from standalone output in a layer-compatible format.
5152
await zipFolder({
5253
outputName: dependenciesOutputPath,

Diff for: package-lock.json

+3-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)