Skip to content

Commit

Permalink
feat(lambda-config): CLI configuration for memory and timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
sladg committed Oct 23, 2022
1 parent fb98377 commit b176d0a
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 14 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ With this, you can pass `.env` files during build time and overwrite / extend th

### Via CDK

See `NextStandaloneStack` construct in `cdk/example.ts`.
Or just use `next-utils deploy` command so you don't have to manage CDK yourself.
See `NextStandaloneStack` construct in `lib/cdk-app.ts`.
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.

#### Benchmark

Expand Down
6 changes: 4 additions & 2 deletions lib/cdk-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class NextStandaloneStack extends Stack {
customServerHandler: 'handler.handler',
customImageHandler: 'handler.handler',
cfnViewerCertificate: undefined,
lambdaTimeout: process.env.LAMBDA_TIMEOUT ? Number(process.env.LAMBDA_TIMEOUT) : 512,
lambdaMemory: process.env.LAMBDA_MEMORY ? Number(process.env.LAMBDA_MEMORY) : 1024,
...props,
}

Expand All @@ -46,8 +48,8 @@ class NextStandaloneStack extends Stack {
handler: config.customServerHandler,
layers: [depsLayer],
// No need for big memory as image handling is done elsewhere.
memorySize: 512,
timeout: Duration.seconds(15),
memorySize: config.lambdaMemory,
timeout: Duration.seconds(config.lambdaTimeout),
environment: {
// Set env vars based on what's available in environment.
...Object.entries(process.env)
Expand Down
6 changes: 4 additions & 2 deletions lib/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,12 @@ program
.option('--stackName <name>', 'Name of the stack to be deployed.', 'StandaloneNextjsStack-Temporary')
.option('--appPath <path>', 'Absolute path to app.', path.resolve(__dirname, '../dist/cdk-app.js'))
.option('--bootstrap', 'Bootstrap CDK stack.', false)
.option('--lambdaTimeout <sec>', 'Set timeout for lambda function handling server requirests', Number, 15)
.option('--lambdaMemory <mb>', 'Set memory for lambda function handling server requirests', Number, 512)
.action(async (options) => {
console.log('Our config is: ', options)
const { stackName, appPath, bootstrap } = options
wrapProcess(deployHandler({ stackName, appPath, bootstrap }))
const { stackName, appPath, bootstrap, lambdaTimeout, lambdaMemory } = options
wrapProcess(deployHandler({ stackName, appPath, bootstrap, lambdaTimeout, lambdaMemory }))
})

program
Expand Down
13 changes: 10 additions & 3 deletions lib/cli/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,29 @@ interface Props {
stackName: string
appPath: string
bootstrap: boolean
lambdaMemory?: number
lambdaTimeout?: number
}

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

export const deployHandler = async ({ stackName, appPath, bootstrap }: Props) => {
export const deployHandler = async ({ stackName, appPath, bootstrap, lambdaMemory, lambdaTimeout }: Props) => {
// All paths are absolute.
const cdkApp = `node ${appPath}`
const cdkCiFlags = `--require-approval never --ci`
const envConfig = [
`STACK_NAME=${stackName}`,
lambdaMemory ? `LAMBDA_MEMORY=${lambdaMemory}` : '',
lambdaTimeout ? `LAMBDA_TIMEOUT=${lambdaTimeout}` : '',
].join(' ')

if (bootstrap) {
await executeAsyncCmd({
cmd: `STACK_NAME=${stackName} ${cdkExecutable} bootstrap --app "${cdkApp}"`,
cmd: `${envConfig} ${cdkExecutable} bootstrap --app "${cdkApp}"`,
})
}

await executeAsyncCmd({
cmd: `STACK_NAME=${stackName} ${cdkExecutable} deploy --app "${cdkApp}" ${cdkCiFlags}`,
cmd: `${envConfig} ${cdkExecutable} deploy --app "${cdkApp}" ${cdkCiFlags}`,
})
}
1 change: 1 addition & 0 deletions lib/cli/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export const packHandler = async ({ handlerPath, outputFolder, publicFolder, sta
rmSync(outputFolder, { force: true, recursive: true })
mkdirSync(outputFolder)

// @TODO: We need to include nested node_modules in case of mono-repos.
// Zip dependencies from standalone output in a layer-compatible format.
await zipFolder({
outputName: dependenciesOutputPath,
Expand Down
8 changes: 3 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b176d0a

Please sign in to comment.