Testing Serverless Code Locally and in the Cloud with AWS SAM
This lesson explains how AWS SAM and saved test events let you run and test serverless code quickly, before and after it is deployed.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how sam local invoke, sam local start-api and sam local start-lambda run code in Docker.
- Explain how generated and shareable test events reproduce the structure of real events.
- Explain why some behaviour can only be tested in AWS and how sam sync speeds up cloud testing.
Running code locally
The SAM CLI runs functions in Docker containers that use the same runtime images as Lambda, so code behaves locally much as it does in the cloud. sam local invoke runs one function once with an event file, such as a sample S3 or SQS event produced by sam local generate-event, and prints the result and logs. sam local start-api starts a local HTTP server that maps the API routes in the template to their functions, so a browser or test client can call http://localhost:3000/orders. sam local start-lambda starts a local endpoint that implements the Lambda Invoke API, so tests that use the AWS SDK or CLI can invoke functions by setting the endpoint URL. The following table summarises the commands.
| Command | What it does |
|---|---|
sam local invoke | Runs one function in a container that mimics Lambda |
sam local start-api | Serves API Gateway routes locally |
sam local start-lambda | Provides a local Lambda endpoint for SDK calls |
The following list describes the requirements and options for running code locally.
- All
sam localcommands require Docker. - Pass environment variables with
--env-vars env.json. - Debugger ports let an IDE step through code in the local container.
- Local runs use your AWS credentials, so code can call real DynamoDB tables or S3 buckets in a development account, catching integration problems early.
Test events
A Lambda function is invoked with an event, and testing it realistically requires an event with exactly the structure the real source produces. Writing these by hand is error-prone, because an S3 notification or an SQS batch contains many nested fields. sam local generate-event produces realistic sample events for many sources, including Amazon S3, Amazon SQS, Amazon SNS, API Gateway, DynamoDB Streams and EventBridge, with options to set values such as the bucket name and object key.
Event files should be saved in the repository, for example in an events/ folder, and reused by local runs and automated tests, so every developer tests with the same inputs. In the Lambda console, shareable test events serve the same purpose for a deployed function: they are stored with the function and visible to every user with access to it, rather than only to the person who created them. The following example generates an S3 event, invokes a function with it and then runs the API locally.
sam build
sam local generate-event s3 put --bucket photo-uploads --key users/42/avatar.jpg > events/s3-put.json
sam local invoke ResizeFunction --event events/s3-put.json --env-vars env.json
sam local start-api # serves the API on http://127.0.0.1:3000
curl -X POST http://127.0.0.1:3000/orders -d '{"id": "9017"}'Testing in the cloud
Local testing cannot reproduce everything: IAM permissions, VPC networking, event source mappings and service quotas behave only in AWS. SAM therefore also supports fast testing against resources deployed in a development account.
sam sync --watch watches the project folder and pushes changes to a development stack within seconds. Code changes are applied by updating the function directly, without a full CloudFormation deployment, while changes to infrastructure still go through CloudFormation. Because it bypasses CloudFormation's change sets and safety checks, sam sync is intended only for development stacks, never production. sam remote invoke then invokes the deployed function with a test event and shows the response and logs, and sam logs displays the function's recent CloudWatch logs in the terminal.
Reproducing a production bug locally
A function fails in production for S3 keys containing spaces. The developer reproduces it locally without deploying:
sam local generate-event s3 put --bucket uploads --key "reports/q3 summary.pdf" > event.json
sam build && sam local invoke ProcessUpload -e event.jsonThe local run fails the same way: S3 event keys are URL-encoded (q3+summary.pdf), and the code never decodes them. After adding urllib.parse.unquote_plus, the local test passes, and the same event file becomes a regression test in the pipeline.
Which SAM CLI commands run a function locally with a sample S3 event?
Show answer
sam local generate-event s3 put creates the sample event, and sam local invoke MyFunction --event event.json runs the function with it in a local container.
Exam Tips
Remember for the exam
- To create a sample event for testing, use
sam local generate-event. - To share test events with other developers, use shareable test events in the Lambda console.
- To pass environment variables to
sam local invoke, use the--env-varsoption. - The
sam localcommands require Docker.