Photo by Ian Stauffer on Unsplash

The Power of AWS SAM & AWS CDK together 🚀

How to use AWS SAM and AWS CDK together in serverless development to quickly push code changes to the cloud, and easily tail the live cloud logs as we test; with example code repo and visuals.


In this article we are going to cover speeding up your AWS CDK serverless development through two key features; AWS CDK Hotswapping and tailing logs with the AWS SAM CLI.

Lee’s Pirate Costume Hire — suggestion from the fantastic Daniel Mackenzie who I have the pleasure of working with!
Fig 1: We are going to build the most basic of solutions to create and get orders
  1. Users can access our API to both order and get the status of their pirate costume orders. This uses Amazon API Gateway.
  2. A user can create an order using a POST on the /orders/ endpoint.
  3. We have a Lambda Function which validates and creates the order through an Express Workflow, which also publishes the event to Amazon EventBridge.
  4. The order is subsequently stored in DynamoDB via the Create Order function.
  5. A user can also check the status of their pirate costume order using a GET based on order id /orders/111 which uses a synchronous Express Workflow to get the order from DynamoDB.

What issues do we face currently?

When we are developing in the serverless world we typically develop against ephemeral cloud stacks (the cloud provider, say AWS), as opposed to performing local development through tools like LocalStack.

Waiting for our CloudFormation stacks to deploy can be exhausting..

What tools can we use to circumvent the issues?

So let’s cover the two key tools for this article below; hotswapping and tailing logs.

Hotswapping ♼

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, but nothing else in your CDK code), skipping CloudFormation, and updating the affected resources directly; this includes changes to resources in nested stacks.

Tailing logs with SAM CLI 📝

OK, so now we will look at tailing live CloudWatch logs using the SAM CLI, which allows us to test our code whilst seeing the tailed logs appear in our terminal in real time.

Lets see this in action 🚀

Let’s get the solution deployed using the following command in the root folder once we have cloned the repo:

npm run deploy

Tailing the logs of our lambda function

We can tail the Lambda function CloudWatch logs from our solution using its function name, which we can find in the .\cdk-outputs.json file in our repo with the key ‘GetOrderLambdaName’ once we have deployed the solution:

Viewing our deployed Lambda function name using the cdk-outputs.json file which is auto-generated
Using the AWS Toolkit to view your deployed Lambda names
sam logs --name PirateCostumeStatelessStack-GetOrderLambda123456 --tail

Deploying the stack with hotswap

Now let’s run hotswap for our quick deployments:

npm run deploy:hot
cdk deploy --outputs-file ./cdk-outputs.json --all --hotswap

Testing! 🧪

So let’s start using the postman file to do some testing to see this in action! Firstly, let’s create a new order as shown below:

Example of creating a new Pirate Costume Order
Example output in the terminal window based on hitting the endpoint using Postman
We can see the hotswap feature in action!
Example showing the live tailing of logs and the super quick deployments using hotswap, and swapping the console logs from ‘*’s to ‘!’s


I hope you found that useful as a way of speeding up the development of our AWS CDK apps using the CDK hotswapping feature alongside the AWS SAM CLI!

Wrapping up 👋

Please go and subscribe on my YouTube channel for similar content!

About me

Hi, I’m Lee, an AWS Community Builder, Blogger, AWS certified cloud architect and Global Enterprise Serverless Architect (GESA) based in the UK; currently working for City Electrical Factors (UK) & City Electric Supply (US), having worked primarily in full-stack JavaScript on AWS for the past 6 years.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Lee James Gilmore

Principal Serverless Engineer | Enterprise Cloud Architect | Serverless Advocate | Mentor | Blogger | AWS x 7 Certified 🚀