Local Testing with AWS SAM CLI
Developing serverless applications often involves deploying to the cloud for testing. However, this can be slow and costly. The AWS Serverless Application Model (SAM) Command Line Interface (CLI) provides powerful tools for local development and testing, significantly speeding up your workflow and reducing the need for frequent cloud deployments.
Why Local Testing is Crucial
Local testing allows you to:
- Iterate faster: Get immediate feedback on code changes without waiting for deployments.
- Reduce costs: Avoid unnecessary AWS service charges during development.
- Debug efficiently: Use familiar debugging tools and techniques directly on your local machine.
- Simulate cloud environments: Mimic AWS Lambda and API Gateway behavior locally.
Key SAM CLI Commands for Local Testing
SAM CLI simulates AWS Lambda and API Gateway locally.
The SAM CLI allows you to run your Lambda functions and API Gateway endpoints on your local machine, mimicking the behavior of the actual AWS services. This is achieved through Docker containers.
The SAM CLI leverages Docker to create isolated environments that closely resemble the AWS Lambda execution environment. When you invoke a function locally using sam local invoke
, the CLI builds your function's dependencies and runs it within a Docker container configured to act like Lambda. Similarly, sam local start-api
spins up a local API Gateway endpoint that routes requests to your local Lambda functions.
sam local invoke
for functions and sam local start-api
for API Gateway endpoints.
Invoking Lambda Functions Locally (`sam local invoke`)
The
sam local invoke
Example usage:
sam local invoke MyLambdaFunction --event events/event.json
Here,
MyLambdaFunction
events/event.json
Starting a Local API Gateway (`sam local start-api`)
The
sam local start-api
Example usage:
sam local start-api
Once started, you can make HTTP requests to
http://127.0.0.1:3000/
The SAM CLI uses Docker containers to simulate the AWS Lambda execution environment. When you run sam local invoke
or sam local start-api
, the CLI pulls or builds a Docker image that matches the runtime of your Lambda function (e.g., Node.js, Python, Java). This container then executes your function code, receiving event data and returning a response, effectively mimicking the behavior of AWS Lambda and API Gateway without requiring a network connection to AWS.
Text-based content
Library pages focus on text content
Debugging Locally
SAM CLI supports attaching debuggers to your local Lambda functions. You can configure your IDE (like VS Code) to connect to the running function in the Docker container, allowing you to set breakpoints, inspect variables, and step through your code.
For Node.js, you can use the --debug-port
flag with sam local invoke
or sam local start-api
to enable remote debugging.
Building and Packaging for Local Execution
Before invoking or starting an API locally, you often need to build your application. The
sam build
.aws-sam/build
Example usage:
sam build
This command reads your
template.yaml
Best Practices for Local Testing
- Use realistic event payloads: Craft files that accurately represent the data your Lambda function will receive in production.codeevent.json
- Test different event sources: Simulate various triggers like API Gateway, SQS, S3, etc.
- Integrate with your IDE: Set up remote debugging for a more efficient development cycle.
- Keep Docker updated: Ensure your Docker installation is current for optimal performance and compatibility.
Learning Resources
Official AWS documentation for installing and using the SAM CLI, covering local testing commands and configurations.
Detailed guide on the `sam local` commands, including `invoke`, `start-api`, and their options for simulating AWS services.
A blog post from AWS explaining how to set up debugging for your Lambda functions when using SAM CLI locally with popular IDEs.
A video tutorial demonstrating a typical local development workflow using AWS SAM CLI, from building to testing and debugging.
Learn about the expanded capabilities of SAM Local in simulating various AWS services beyond Lambda and API Gateway.
An article from the AWS Builders' Library focusing on best practices for building and testing serverless APIs using SAM.
The official GitHub repository for AWS SAM CLI, offering source code, issue tracking, and community contributions.
A comparative analysis of AWS SAM and the Serverless Framework, highlighting differences in local development and testing approaches.
Essential documentation for understanding and working with Docker, the underlying technology for SAM's local testing.
Reference for the structure of events that AWS Lambda functions receive, crucial for creating accurate local test payloads.