AWS Lambda testing locally using SAM



In this post, I will create a AWS nodejs 10 Lambda function, test it locally using SAM AWS Serverless Application Model.


What it does
Testing Lambda Locally
SAM CLI also allows faster, iterative  development of your Lambda function code.
You can catch errors before they are put into place.

Required Tools

  • AWS provides SAM CLI 
  • SAM CLI is based on AWS SAM
  • It is a docker based solution and utilizes docker image name 'docker-lambda'.

SAM CLI creates a local testing environment and it stimulates a real-time AWS run-time environment.

The code repository for this is located in my github. https://github.com/nikhilbhoj/samrepo

Step 1. 
The SAM template which is named as serverless.yaml, this is shorter version of cloud formation template used for deploying Lambda. The name of my Lambda is simple.



Step 2. 
Validate the SAM template located at the cloud formation directory.


Step 3. 

The event.json and the index.js files.


Step 4. 

Testing Lambda function locally with below mentioned command.

sam local invoke simple -e /home/vagrant/nodejslambda/nodejs-lambda-cicd/event.json -t serverless.yaml


Lambda can be deployed using Cloudformation,terraform, or with SAM as SAM deploy command or via pipeline from AWS.

In my next blog, I will deploy this Lambda function via CDK (Cloud development Kit from AWS.


Comments

Popular Posts