CDK Speedster - fast Lambda deployment

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:
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:

    desc: Only Update Lambda Auth code
    dir: lambda
      zip: ../dist/
      lambda: CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0
      - 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   };

5 - Deploy

 time task deploy-auth-fast
zip -r -dd -q ../dist/ .
aws lambda update-function-code --function-name  CdkLambdaSimpleStack-HelloHandler2E4FBA4D-1HQ0BSAQCW4Z0 --zip-file fileb://../dist/
    "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!


Photo by amirali mirhashemian on Unsplash

Similar Posts You Might Enjoy

Using CloudFormation Modules for Serverless Standard Architecture

Serverless - a Use Case for CloudFormation Modules? Let´s agree to “infrastructure as code” is a good thing. The next question is: What framework do you use? To compare the frameworks, we have the tRick-benchmark repository, where we model infrastructure with different frameworks. Here is a walk through how to use CloudFormation Modules. This should help you to compare the different frameworks. - by Gernot Glawe

The CDK pipeline construct

Generation of Infrastructure-as-Code is fun. To be the real DevOps hero, you should build a complete CI-CD pipeline. But this is a piece of work. And if you want to deploy to multiple accounts, it gets tricky. With the new CDK, builtin pipeline Construct, it’s easy - if you solve a few problems. Here is a complete walk-through. - by Gernot Glawe

Bridging the terraform - CloudFormation gap

CloudFormation does not cover all AWS Resource types. Terraform does a better job in covering resource types just in time. So if you want to use a resource type which CloudFormation does not support yet, but you want to use CloudFormation, you have to build a Custom Resource with an own Lambda Function. CDK to the rescue: use AwsCustomResource. - by Gernot Glawe