Skip to content

Commit

Permalink
Introduce bundling with esbuild, --hotswap and --watch (#1321)
Browse files Browse the repository at this point in the history
* bundle cdk

* remove env variable

* optimise

* set externalModules

* add hotswap

* Create rude-bobcats-unite.md

* Run `skuba format`

* introduce hotswap

* add watch

* Update rude-bobcats-unite.md

* Run `skuba format`

* add commas

* Tweak changeset

* Update rude-bobcats-unite.md

* Remove scripts

* does this work

* oops

* try

* kill set -e

---------

Co-authored-by: skuba <[email protected]>
Co-authored-by: Ryan Ling <[email protected]>
  • Loading branch information
3 people authored Nov 30, 2023
1 parent 2cfc4da commit 3345668
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
9 changes: 9 additions & 0 deletions .changeset/rude-bobcats-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'skuba': patch
---

template/lambda-sqs-worker-cdk: Introduce bundling with esbuild, `--hotswap` and `--watch`

This template now uses the `aws_lambda_nodejs.NodejsFunction` construct which uses esbuild to bundle the Lambda function. This [reduces cold start time](https://aws.amazon.com/blogs/developer/reduce-lambda-cold-start-times-migrate-to-aws-sdk-for-javascript-v3/) and time to build on CI.

The `--hotswap` and `--watch` options allow you to rapidly deploy your code changes to AWS, enhancing the developer feedback loop. This change introduces `deploy:hotswap` and `deploy:watch` scripts to the `package.json` manifest and a `Deploy Dev (Hotswap)` step to the Buildkite pipeline. Read more about watch and hotswap [on the AWS Developer Tools Blog](https://aws.amazon.com/blogs/developer/increasing-development-speed-with-cdk-watch/).
8 changes: 5 additions & 3 deletions scripts/test-template.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#!/usr/bin/env sh

set -e

template="${1}"
if [ -z "$template" ]; then
echo "Usage: yarn test:template <template_name>"
Expand Down Expand Up @@ -55,7 +53,11 @@ yarn skuba -v
yarn skuba --version

echo "--- skuba build ${template}"
yarn build
output=$(yarn build 2>&1)
echo $output
if [[ $? -ne 0 && $output != *"Command \"build\" not found"* ]]; then
exit 1
fi

echo "--- skuba lint ${template}"
yarn lint
Expand Down
23 changes: 17 additions & 6 deletions template/lambda-sqs-worker-cdk/.buildkite/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ configs:
- yarn deploy
concurrency: 1
plugins:
- artifacts#v1.9.2:
build: ${BUILDKITE_BUILD_ID}
download: lib/*
- *aws-sm
- *private-npm
- *docker-ecr-cache
Expand All @@ -44,14 +41,11 @@ configs:

steps:
- label: 🧪 Test, Lint & Build
artifact_paths: lib/**/*
commands:
- echo '+++ yarn test:ci'
- yarn test
- echo '--- yarn lint'
- yarn lint
- echo '--- yarn build'
- yarn build
env:
GET_GITHUB_TOKEN: please
plugins:
Expand Down Expand Up @@ -87,6 +81,23 @@ steps:
concurrency_group: '<%- repoName %>/deploy/dev'
key: deploy-dev

- block: 🙋🏻‍♀️ Deploy Dev (Hotswap)
key: deploy-dev-hotswap-block
branches: '!${BUILDKITE_PIPELINE_DEFAULT_BRANCH}'

- <<: *deploy
branches: '!${BUILDKITE_PIPELINE_DEFAULT_BRANCH}'
depends_on: deploy-dev-hotswap-block
agents:
queue: <%- devBuildkiteQueueName %>
env:
ENVIRONMENT: dev
commands:
- echo '+++ yarn deploy:hotswap'
- yarn deploy:hotswap
label: 🤞 Deploy Dev (Hotswap)
concurrency_group: '<%- repoName %>/deploy/dev'

- <<: *deploy
env:
ENVIRONMENT: prod
Expand Down
6 changes: 5 additions & 1 deletion template/lambda-sqs-worker-cdk/cdk.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,9 @@
}
}
},
"progress": "events"
"progress": "events",
"watch": {
"include": "src/**/*.ts",
"exclude": "src/**/*.test.ts"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ exports[`returns expected CloudFormation stack for dev 1`] = `
},
},
"FunctionName": "serviceName",
"Handler": "app.handler",
"Handler": "index.handler",
"KmsKeyArn": {
"Fn::GetAtt": [
"kmskey49FBC3B3",
Expand Down Expand Up @@ -524,7 +524,7 @@ exports[`returns expected CloudFormation stack for prod 1`] = `
},
},
"FunctionName": "serviceName",
"Handler": "app.handler",
"Handler": "index.handler",
"KmsKeyArn": {
"Fn::GetAtt": [
"kmskey49FBC3B3",
Expand Down
14 changes: 11 additions & 3 deletions template/lambda-sqs-worker-cdk/infra/appStack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
aws_kms,
aws_lambda,
aws_lambda_event_sources,
aws_lambda_nodejs,
aws_sns,
aws_sns_subscriptions,
aws_sqs,
Expand Down Expand Up @@ -52,11 +53,16 @@ export class AppStack extends Stack {

const architecture = '<%- lambdaCdkArchitecture %>';

const worker = new aws_lambda.Function(this, 'worker', {
const worker = new aws_lambda_nodejs.NodejsFunction(this, 'worker', {
architecture: aws_lambda.Architecture[architecture],
code: new aws_lambda.AssetCode('./lib'),
entry: './src/app.ts',
bundling: {
sourceMap: true,
target: 'node20',
// By default the aws-sdk-v3 is set as an external module, however, we want it to be bundled with the lambda
externalModules: [],
},
runtime: aws_lambda.Runtime.NODEJS_20_X,
handler: 'app.handler',
functionName: '<%- serviceName %>',
environmentEncryption: kmsKey,
environment: {
Expand All @@ -65,6 +71,8 @@ export class AppStack extends Stack {
NODE_OPTIONS: '--enable-source-maps',
...context.workerLambda.environment,
},
// aws-sdk-v3 sets this to true by default so it is not necessary to set the environment variable
awsSdkConnectionReuse: false,
});

worker.addEventSource(new aws_lambda_event_sources.SqsEventSource(queue));
Expand Down
4 changes: 2 additions & 2 deletions template/lambda-sqs-worker-cdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "skuba build",
"deploy": "cdk deploy appStack --require-approval never --context stage=${ENVIRONMENT}",
"deploy:hotswap": "yarn deploy --hotswap",
"deploy:watch": "yarn deploy:hotswap --watch",
"format": "skuba format",
"lint": "skuba lint",
"package": "yarn install --ignore-optional --ignore-scripts --modules-folder ./lib/node_modules --non-interactive --offline --production",
"test": "skuba test",
"test:ci": "skuba test --coverage",
"test:watch": "skuba test --watch"
Expand Down

0 comments on commit 3345668

Please sign in to comment.