Published on

Hotswap CDK deployments for faster serverless development cycle

Tired of using AWS Console? 🤕
Time to boost your productivity with Cloudash — an AWS desktop client.

When developing a serverless stack using AWS CDK, you probably don't want to wait for a CloudFormation deployment every time you change a single line in a Lambda function. In this blogpost you'll learn how to use Hotswap to speed up your CDK development feedback loop.

You can pass the --hotswap flag to the deploy command:

Example: cdk deploy --hotswap

Note that this command should be used during development only and all production deployments should be done using the cdk deploy command (without the hotswap flag).

This will attempt to perform a faster, short-circuit deployment if possible (for example, if you only changed the code of a Lambda function in your CDK app), skipping CloudFormation, and updating the affected resources directly. If your changes cannot be hotswapped, a full CloudFormation deployment will be performed (which will take longer, but will be more reliable).

As noted in AWS CDK toolkit README:

Passing this option to cdk deploy will make it use your current AWS credentials to perform the API calls - it will not assume the Roles from your bootstrap stack, even if the @aws-cdk/core:newStyleStackSynthesis feature flag is set to true (as those Roles do not have the necessary permissions to update AWS resources directly, without using CloudFormation). For that reason, make sure that your credentials are for the same AWS account that the Stack(s) you are performing the hotswap deployment for belong to, and that you have the necessary IAM permissions to update the resources that are being deployed.

According to AWS docs, hotswapping is currently supported for the following changes (additional changes will be supported in the future):

  • Code asset (including Docker image and inline code) and tag changes of AWS Lambda functions.
  • AWS Lambda Versions and Aliases changes.
  • Definition changes of AWS Step Functions State Machines.
  • Container asset changes of AWS ECS Services.
  • Website asset changes of AWS S3 Bucket Deployments.
  • Source and Environment changes of AWS CodeBuild Projects.
  • VTL mapping template changes for AppSync Resolvers and Functions

Tired of switching between AWS console tabs? 😒

Cloudash provides clear access to CloudWatch logs and metrics, to help you make quicker decisions.
Try it for free:

Logs screen