Speeding up Lambda development with SAM & CDK

At FourCo we are big fans of ‘serverless’. Letting your cloud provider take care of the provisioning of resources is completely in line with our ‘Low-ops/No-ops’ approach. Despite the many upsides of using serverless, developing and testing a serverless application locally can be a bit tedious. There are some tools available to mimic the cloud environment locally by mocking some services. For example the AWS SAM CLI (more about this later) provides ways to invoke your Lambda functions locally (`sam local invoke`) or run an API Gateway locally (`sam local start-api`). They don’t fully mimic the cloud environment though and are not very popular with developers.

A better way is actually deploying to the cloud and testing there. The big downside here is that each deployment can take a few minutes with Cloudformation and if you’re debugging your Lambda code this wastes a lot of time. Especially if you’re in the stage where you are just making tiny code changes that you want to test quickly and not wait minutes for each deployment to complete. What if you could bring this down to seconds? In this blog I’d like to go over two ways to do this.

AWS Serverless Application Model

AWS has an open-source framework to build serverless applications: the AWS Serverless Application Model (SAM). It consists of two components:

  • AWS SAM template specification
  • AWS SAM Command Line Interface (CLI)

The template specification is an abstraction layer on top of Cloudformation. It provides simple and clean syntax to write your serverless application. The CLI is mainly used to build and deploy your application. Under the hood it first converts your SAM template to a Cloudformation template and then deploys it with Cloudformation. The SAM CLI has some other useful options though.

0 jinob at1isulbhc
Sam, the mascot of SAM

SAM Accelerate

Late last year AWS launched SAM Accelerate! From their blog:

The AWS SAM team has listened to developers wanting a better way to emulate the cloud on their local machine and we believe that testing against the cloud is the best path forward. With that in mind, I am happy to announce the beta release of AWS SAM Accelerate!

SAM Accelerate tries to solve the exact problem of developing serverless applications and testing them in the cloud. Instead of fully deploying your whole application through Cloudformation for just a small change in your Lambda code, you can now deploy only code changes using the Lambda API and bypassing Cloudformation. This takes seconds instead of minutes!

You do all of this with the new sam sync command. This command differentiates between code and configuration. Let’s go over a few ways of updating your project.

sam sync --stack-name teststack

If you just run the command without any options it updates the whole infrastructure and code, just like the old sam deploy.

sam sync --stack-name teststack --code

If you use the –code flag you only update the code (using the service APIs and bypassing CloudFormation).

sam sync --stack-name teststack --code --resource-id LambdaFunction

You can even choose a specific resourcename from your SAM template and only update that specific code. This is very useful if your serverless application has a lot of Lambda functions and you’re only developing a specific one.

sam sync –stack-name teststack --watch

This is my preferred way of using sam sync. If you use the –watch flag SAM starts with a full infra sync. After that it watches for file changes and immediately syncs the code in the cloud in seconds.

I use a Lambda function as an example here, but the code updates also work for other serverless services (ie. Layers or Step Functions). If you want to learn more about this, have a look at the SAM documentation and AWS blog linked at the end of this article.

CDK Watch

Around the same time SAM accelerate was launched, some similar features were introduced for AWS CDK (Cloud Development Kit). They are named a bit different though:

cdk deploy --hotswap

The --hotswap flag checks if the changes can be deployed with just API calls (similar to the --code flag for SAM). If it is possible it bypasses Cloudformation and quickly deploys.

cdk deploy --watch

The --watch flag is similar to the same SAM flag. Depending on if you’re updating code or configuration, it uses API calls or Cloudformation.

cdk deploy --watch --no-rollback

An extra option for CDK which doesn’t have an equivalent in SAM is --no-rollback. It will prevent CloudFormation from rolling back failed changes, saving more iteration time on failed deployments.


Using these options for SAM and CDK will save a lot of time when developing a serverless application. Instead of waiting minutes to test small code changes you can now iterate a lot faster. Over the span of a year this will save a lot of time! An important warning though: using these options will lead to Cloudformation drift and should only be used for development and never for a production stack. In production you want to remove the possibility of drift and have the full safety features Cloudformation deployments provides.


AWS SAM Documentation

CDK Watch AWS Blog

SAM Accelerate AWS Blog


Sebastiaan Smit

Partner at FourCo Specialising in Cloud and Big Data infrastructure.