This site runs best with JavaScript enabled.

AWS CDK Made Simple: Run a Lambda function locally

Tomasz Łakomy

July 05, 2020


Every single development workflow looks like this and building stuff with AWS Lambda is no exception:

A fake book cover with a 'Changing Stuff and Seeing What Happens' title

Luckily, there's a way of testing AWS-CDK powered lambda functions on our local machine, without fiddling with the AWS Console.

It's definitely easier and faster than re-deploying a function to THE CLOUD every time we want to test something.

In order to do that, we need to have AWS SAM - AWS Serverless Application Model installed on our machine (if you'd like to get a short introduction to AWS SAM, check out WTF is AWS Serverless Application Model (AWS SAM))

How do I run a Lambda function built with CDK locally?

Imagine that we have a simple CDK stack, with a single Lambda function that we'd like to be able to run on our local machine:

import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
export class SampleCdkAppStack extends cdk.Stack {
constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const myLambda = new lambda.Function(this, 'MyFunction', {
runtime: lambda.Runtime.NODEJS_12_X,
handler: 'index.handler',
code: lambda.Code.fromAsset('lambda'),
});
}
}

In order to do that, go through the following steps:

  1. Run
cdk synth --no-staging > template.yaml

in order to create a CloudFormation template and store it in a template.yaml file

  1. Find the logical ID for your Lambda function in template.yaml. It will look like MyFunction12345678, where 12345678 represents an 8-character unique ID that the AWS CDK generates for all resources. The line right after it should look like Type: AWS::Lambda::Function
  2. Run the function by executing:
sam local invoke HelloLambda12345678
  1. We can also pass custom events to the function, to do that - for instance, given a sample hello.json file:
{
"path": "/hello/friends",
"body": "hello"
}
  1. To execute a lambda function locally with a custom event, run
sam local invoke HelloLambda3D9C82D6 -e sample_events/hello.json

I'd like to learn more, how can I do that?

Excellent question!

I've launched an entire AWS CDK course on egghead.io - check out Build an App with the AWS Cloud Development Kit

A Build an App with the AWS Cloud Development Kit course header

It's the best piece of content I've ever created and I'm incredibly proud of it 🌟

Apart from that, I also have egghead.io collections about serverless topics, such as:

On top of that those are excellent resources that I'm more than happy to recommend:

Share articleBuy Me A Beer

Join the Newsletter



Tomasz Łakomy © 2022
Cloudash - serverless monitoring. Simplified