DEV Community

Cover image for Test your AWS Lambda function locally
JP Scriven
JP Scriven

Posted on • Originally published at jpscriven.com

Test your AWS Lambda function locally

Using AWS SAM to run your Lambda function locally

What is AWS SAM?
SAM stands for Serverless Application Model and it is a framework that is used for developing and deploying serverless applications. The idea is that you can create a very simple SAM YAML file which would then generate more complex CloudFormation templates.
All the configuration is done in YAML and it supports anything from CloudFormation like Parameters, Resources, Mapping, Outputs, etc...

SAM gives you the ability to help you run Lambda, API Gateway, DynamoDB locally. Where you can then test your serverless solution before deploying to AWS.

Differences between SAM and CloudFormation:
You must include the Transform Header within your template and it indicates it is a SAM template.

  • Transform: 'AWS::Serverless-2016-10-31'

Then you have the following types for resources:

  • AWS::Serverless::Function
  • AWS::Serverless::Api
  • AWS::Serverless::SimpleTable

Important commands to know:

  • sam build: Fetches dependencies and creates local deployment artifacts
  • sam package: Package and upload to Amazon S3 and generates a CloudFormation template
  • sam deploy: Deploy to CloudFormation

Simple HelloWorld SAM template to get you started:

First create a folder for your application and then you can use VS Code or any editor of your choice to open that folder.

  1. Next we will run the sam init command, which will initialize a new serverless application with a SAM template. sam init command
  2. We will select option 1 - AWS Quick Start Templates to get us started quickly with a template.
  3. Then it will ask us which package type we want and for this example we will select option 1, which will zip the artifact and upload to Amazon S3. Select the runtime
  4. Now we need to select a runtime that we want to use for our serverless function. It supports a wide range of runtimes so you can select any version that you are comfortable with. I will select option 2 here and go with python 3.8. Quick start template AWS SAM
  5. Choose a Project name or leave blank to accept the default of sam-app.
  6. Next you will be provided with a list of different templates, we want just the basic Hello World example so we will go with option 1 again. Generating the application
  7. As you can see now it is busy generating the application for us and creating the folder and file structure with the template that we need to get going.

Folder structure:
SAM template folder structure
After it is done generating all the files your folder structure should look similar to this.
Our focus will be on the hello_world folder, which is where our Lambda function code is stored and then the template.yaml file, which is our AWS SAM template.

The template.yaml file:
template.yaml SAM file
As we can see at the top the Transform: AWS::Serverless-2016-10-31 is included, which has to be included within the SAM template.
Next under Resources we can see that we have a Lambda function and it's type is AWS::Serverless::Function, which you include with your Lambda function in a SAM template.

Under properties next to CodeUri, we need to specify where our lambda files will be, which in this case is under the folder hello_world. The handler will be the file name where the handler function is located (app.py) and our handler name (lambda_handler), which is why it is added as app.lambda_handler. The runtime is what we selected during the initializing stage so depending on what you choose this might be different for you.
With this Hello World example the also included an event attached to our function, which is an API and it is a get method.

Run this template locally for testing

IMPORTANT: To be able to test this template locally you will need to install Docker first as it will use Docker to run your function locally Download Docker here

  1. Now we can run sam build which will fetch the dependencies and create local deployment artifacts.
  2. Next we want to invoke our function, so we need to run the following command sam local invoke
  3. It will mount our build function to a container and this is where Docker will be used. You don't have to know Docker to be able to use your function locally, you just need to have it installed and running, SAM does the rest for us.
  4. You will receive an output with the following if the function was invoke successfully. sam local invoke Shows you the same status you will receive as when you run your function from the AWS console. From here you can view details as the Duration, the billed duration, Max memory used etc. With the response from our Lambda function.

Conclusion:

This is a quick way to get up and running with AWS SAM so you can test out, edit and deploy functions from your local machine. You can add code to your function and test the changes and play around with some other features of SAM. This quick short template once deployed to AWS will generate a more complex CloudFormation template and then deploy the stack to your AWS account.

Top comments (0)