Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News AWS Release ‘SAM Local’ to Facilitate Local Build and Test of AWS Lambda Serverless Applications

AWS Release ‘SAM Local’ to Facilitate Local Build and Test of AWS Lambda Serverless Applications

This item in japanese

AWS have released a new tool in beta, SAM Local, that makes it easy to build and test serverless AWS Lambda applications on a local development machine. Core features provided by SAM Local include: the ability to test AWS Lambda functions locally running within Docker, a simple CLI to start a local API Gateway that fronts a Lambda function via a SAM template, validation of SAM templates, and the generation of sample payloads for testing the handling of various AWS event sources such as S3, Kinesis and CloudTrail.

AWS introduced the Serverless Application Model (SAM) last year with the goal of making it easier for developers to deploy serverless applications. SAM is an open source specification built upon and extending AWS CloudFormation, which is AWS's infrastructure as code (IaC) offering that allows developers to declaratively specify the deployment and configuration of cloud resources.

The main features of SAM Local include:

Running Serverless projects and functions locally with SAM Local requires Docker to be installed and running, and SAM Local will use the DOCKER_HOST environment variable to contact the docker daemon. SAM Local can be installed through NPM (npm install -g aws-sam-local), or the latest version can be installed straight from source (go get which will create a binary named aws-sam-local, not sam).

Developers specify the deployment configuration of functions and associated integrated AWS resources by using an enhanced CloudFormation template format (that is ultimately transformed into a standard CloudFormation template). For example, the template.yaml file shown below defines a simple DynamoDB table, a Python-based Lambda function (the source code is included within a separate file with the name as specified in the 'Handler property', and an API Gateway instance with GET and POST methods supported.

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
    Type: "AWS::Serverless::SimpleTable"
    Type: "AWS::Serverless::Function"
      Timeout: 30
      Runtime: python3.6
      Handler: lambda_function.lambda_handler
      Policies: AmazonDynamoDBFullAccess
          TABLE_NAME: !Ref VotesTable
            Method: get
            Path: /
          Type: Api
            Method: post
            Path: /
          Type: Api


An AWS Blog post written by Randall Hunt contains an example SAM Deployment with API Gateway integration, and demonstrates how SAM Local assists with the development workflow. The instructions are comprehensive, but your correspondent did have to create the associated DynamoDB table before the function could be tested (an example 'create-table' command that can be issued via the AWS CLI is shown below), and when running a local DynamoDB for testing purposes also change the DynamoDB hostname within the function to be the machine's local network IP address, and not simply 'localhost', due to the fact that the function runs within a Docker container, which on Docker for Mac or Docker for Windows runs within its own VM where 'localhost' provides a different context.

aws dynamodb create-table \
    --table-name spaces-tabs-votes \
    --attribute-definitions \
        AttributeName=id,AttributeType=S \
    --key-schema AttributeName=id,KeyType=HASH \
    --provisioned-throughput ReadCapacityUnits=1,WriteCapacityUnits=1 \
    --endpoint-url http://localhost:8000   


SAM local can generate AWS service events for testing purpose, for example, by issuing the following CLI command 'sam local generate-event <service>', and also allows interactive debugging via a supported IDE. In Java and Node.js the -d flag and a port can be passed to the 'sam local' CLI command to immediately enable the debugger. The AWS Blog suggests that for Python a library like epdb could be used.

Once a serverless application has been developed and tested locally, the SAM CLI can be used to package and deploy the application to a live environment. The 'sam package' command will zip the code artifacts, upload to S3 and produce a SAM file that is ready to be deployed to Lambda using AWS CloudFormation. The 'sam deploy' command will deploy the packaged SAM template to CloudFormation. Both 'sam package' and 'sam deploy' are identical to their AWS CLI equivalents commands aws cloudformation package and aws cloudformation deploy respectively.

Additional information on SAM local can be found on the AWS Blog "New - AWS SAM Local (Beta) - Build and Test Serverless Applications Locally" or within the project's GitHub repository.

Rate this Article