CDK Speedster - fast Lambda deployment
This content is more than 4 years old and the cloud moves fast so some information may be slightly out of date.
CDK is great for serverless architectures. But the deploy times even for small lambda functions is to slow.
Here is a little trick which can speed up things a lot. A small caveat: It is cheating.
CDK creates a Cloudformation change set each time it deploys. That is great for large deployments, but not if you change some code in a Lambda function and want to test the function.
But If the following applies to your project:
- you only change the function code
- your code is zipped smaller than 50 MB see limits
Then you can upload the code directly!
Here is how with the example of the “lambda-simple” of the tecracer githup cdk-example repo.
Normal deploy with CDK and CloudFormation change set
time task deploy
npm run build
> cdk-lambda-simple@0.1.0 build cdk-templates/lambda-simple
> tsc
Profile gg*******n
cdk deploy --require-approval never --profile $AWSUME_PROFILE
CdkLambdaSimpleStack: deploying...
[0%] start: Publishing 761361f83f6a8a4cedf755c1d3b6f714678c3fdf70a6472078d826d47592fd96:current
[100%] success: Published 761361f83f6a8a4cedf755c1d3b6f714678c3fdf70a6472078d826d47592fd96:current
CdkLambdaSimpleStack: creating CloudFormation changeset...
✅ CdkLambdaSimpleStack
Stack ARN:
arn:aws:cloudformation:eu-central-1:123456789012:stack/CdkLambdaSimpleStack/c2b07b40-cb41-11ea-a4d8-064aba521f98
task deploy 5,20s user 0,58s system 11% cpu 51,752 total
51 seconds in total!
Add fast deploy
Using the aws Lambda api directly to update the code is faster.
1 - Add output with function name
Add an output to your cdk code.
new CfnOutput(this, "HelloLambda", {
value: hello.functionName
}
)
2 - Deploy the stack
Get Lambda function name:
CdkLambdaSimpleStack.HelloLambda = CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0
3 - Add following lines to the Taskfile:
deploy-auth-fast:
desc: Only Update Lambda Auth code
dir: lambda
vars:
zip: ../dist/hello.zip
lambda: CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0
cmds:
- zip -r -dd -q {{.zip}} .
- aws lambda update-function-code --function-name {{.lambda}} --zip-file fileb://{{.zip}}
Or put the lines in a shell script.
4 - Change some code
1 exports.handler = async function(event) {
2 console.log('request:', JSON.stringify(event, undefined, 2));
3 return {
4 statusCode: 200,
5 headers: { 'Content-Type': 'text/plain' },
6 body: `Hello, CDK 1.53 again! You've hit ${event.path}\n`
7 };
8 };
9
5 - Deploy
time task deploy-auth-fast
zip -r -dd -q ../dist/hello.zip .
aws lambda update-function-code --function-name CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0 --zip-file fileb://../dist/hello.zip
{
"FunctionName": "CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0",
"FunctionArn": "arn:aws:lambda:eu-central-1:123456789012:function:CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0",
"Runtime": "nodejs10.x",
"Role": "arn:aws:iam::123456789012:role/CdkLambdaSimpleStack-HelloHandlerServiceRole11EF7C-1GSSZUNT7PQE7",
"Handler": "hello.handler",
"CodeSize": 3225,
"Description": "",
"Timeout": 3,
"MemorySize": 1024,
"LastModified": "2020-07-21T11:15:36.460+0000",
"CodeSha256": "5dATrYq8ruN5z4LwLdO/uJLC7ExByjCG22DpRPtgN4c=",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "e5555ef7-d87a-45fe-b5a4-5cfe479a81cc",
"State": "Active",
"LastUpdateStatus": "Successful"
}
task deploy-auth-fast 0,51s user 0,22s system 65% cpu 1,119 total
Now you are 50 times faster!
Thanks for reading, please comment on twitter. And visit our twitch channel: twitch.
Stay healthy in the cloud and on earth!
Thanks
Photo by amirali mirhashemian on Unsplash