Home > Categories > Aws cdk > Invoke AWS CDK Lambda Locally With AWS SAM

Home > Categories > Lambda > Invoke AWS CDK Lambda Locally With AWS SAM

Home > Invoke AWS CDK Lambda Locally With AWS SAM

Invoke AWS CDK Lambda Locally With AWS SAM

Updated:

11 Feb 2021

Published:

19 Dec 2020

AWS CDK is great way to deploy lambda but for testing lambda you have to deploy very frequently.

Deploying lambda is relatively fast but it can be much faster to iterate if we can test it locally.

Now local testing of lambda will not work for every situation.

For example if you are using lambda layers in your lambda then you cannot test it locally.

There are few more unique situations like this where local invoke will not work.

But for most cases local lambda testing should work so just be aware that your mileage may vary.

To read rest of the article please Purchase Subscription

Before we get started with invoking lambda locally I would recommend to develop CDK project inside development docker container.

So we no longer have the issue of "this works on my computer". And it will not mess with your existing workflow.

So checkout my blog post for detailed video explanation.

Then checkout CDK Lambda Local development container settings on github

View Source code on github

There are some prerequisite's for local testing

  1. You need to have docker desktop installed.
  2. You need to have SAM cli installed

Let's start with docker. Docker desktop is available only for windows and macOS.

So if you are using linux then you have to install docker with terminal.

In any case make sure you have access to docker cli on terminal.

Run docker --version to verify that you have access to docker cli.

At the time of writing this article SAM can only be installed with homebrew.

I mean there are other ways to install SAM cli but AWS team doesn't recommend you use anything else.

However recently they have promised to provide solution without using homebrew. You can keep track of that issue for future updates.

So on windows you MUST use WSL. To install homebrew and to install SAM CLI.

So to install homebrew enter following into terminal

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Run brew doctor to verify that brew is properly installed. Then install SAM CLI as follows

brew tap aws/tap
brew install aws-sam-cli

Then run sam --version to verify that SAM cli has been installed properly.

Also this goes without saying you need to have CDK installed

npm i -g aws-cdk

Then run cdk --version to verify cdk has been installed properly.

Then create new folder and initialize cdk project as follows

mkdir lambda-local && cd $_
cdk init app --language typescript
npm i @aws-cdk/aws-lambda

The $_ is variable for last word of previous command. So in this case its lambda-local

Then create a lambda function as follows

mkdir -p lambda-fns/one
cd lambda-fns/one
touch index.js Taskfile.yml

You will notice that in cdk project .js files will not be committed by default.

So comment out the following line from .gitignore

#*.js

Add the lambda function as follows

We are just going to log the received event and return success. Then add the optional Taskfile.yml

This is optional. You can just manually run these command but I prefer to use task.

Then create the lambda with cdk as follows

Now this is the Important part. You will have to do this only once. Run the following code

cdk synth LambdaLocalStack-WDzD8ERde7sB6sfJ --no-staging > template.yml

Then inside the template.yml file look for following code

oneFnE76FAA20:
Type: AWS::Lambda::Function

Search for AWS::Lambda::Function and line above that is the important line. Copy that word and run the following

sam local invoke oneFnE76FAA20

Then sam will download the docker image if not available locally and run lambda and show output as follow

START RequestId: 1d9ca9b3-48e9-4395-b3ed-e52f0a6f1047 Version: $LATEST
2020-12-19T09:03:34.522Z 1d9ca9b3-48e9-4395-b3ed-e52f0a6f1047 INFO {}
END RequestId: 1d9ca9b3-48e9-4395-b3ed-e52f0a6f1047
END RequestId: 1d9ca9b3-48e9-4395-b3ed-e52f0a6f1047
REPORT RequestId: 1d9ca9b3-48e9-4395-b3ed-e52f0a6f1047 Init Duration: 0.06 ms Duration: 64.98 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode":200,"body":"{\"message\":\"success\"}"}

Now there was no event passed into the function so you can create new event json as follows

mkdir events && cd $_
touch hello.json

Then add the following json object

{ "givenName": "Apoorv", "familyName": "Mote" }

Then pass in this hello.json as follows

sam local invoke oneFnE76FAA20 -e events/hello.json

Then I get following output in terminal

START RequestId: 4faa08db-97cb-41d3-9d9a-21f7d33999ae Version: $LATEST
2020-12-19T09:12:01.395Z 4faa08db-97cb-41d3-9d9a-21f7d33999ae INFO { givenName: 'Apoorv', familyName: 'Mote' }
END RequestId: 4faa08db-97cb-41d3-9d9a-21f7d33999ae
REPORT RequestId: 4faa08db-97cb-41d3-9d9a-21f7d33999ae Init Duration: 0.20 ms Duration: 65.88 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 128 MB
{"statusCode":200,"body":"{\"message\":\"success\"}"}

View Source code on github

Very important you need to have template.yml at the root of your project to invoke lambda locally.

I have not added template.yml inside git repository.

Conclusion

So its very easy to run lambda locally and deploy with cdk. So there no reason not to use this to iterate lambda faster.

Free users cannot comment below so if you have questions then tweet me @apoorvmote. I would really like to hear your brutally honest feedback.

If you like this article please consider purchasing paid