Deploying SAM Applications
Use the SAM CLI to build, test locally, and deploy your serverless applications to AWS, managing different environments.
Deploying SAM Applications is a free Serverless Backend with AWS Lambda & API Gateway lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Serverless Backend with AWS Lambda & API Gateway learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Introduction to SAM CLI
Welcome to deploying SAM applications! The AWS Serverless Application Model Command Line Interface (SAM CLI) is your best friend for building and managing serverless projects.
It extends AWS CloudFormation to simplify defining serverless resources. The SAM CLI provides commands to build, test, and deploy your serverless applications.
Building Your Application
Before deploying, your SAM application needs to be built. The sam build command takes your application code and dependencies, then prepares them for deployment.
- It packages dependencies (e.g., Python libraries).
- It transpiles code if needed (e.g., TypeScript to JavaScript).
- It creates a deployable artifact in a
.aws-sam/builddirectory.
Example Lambda Function
Let's look at a simple Python Lambda function and its template.yaml. The sam build command processes this to create deployable code.
After running sam build, the packaged code would be ready for local testing or deployment.
import json
def lambda_handler(event, context):
"""
A simple Lambda handler.
"""
return {
"statusCode": 200,
"headers": {
"Content-Type": "application/json"
},
"body": json.dumps({
"message": "Hello from your SAM Lambda!"
})
}Local Testing with `sam local invoke`
The SAM CLI allows you to test your Lambda functions locally without deploying them to AWS, saving time and costs. Use sam local invoke to simulate a Lambda invocation.
You can pass event data directly to your function, just like how AWS services (like API Gateway or S3) would trigger it.
Running a Lambda Locally
To test the Lambda function from the previous scene, you'd use sam local invoke with the logical ID of your function and an event file.
For example, sam local invoke MyHelloWorldFunction --event event.json would test a function named 'MyHelloWorldFunction' using the data in event.json.
{
"httpMethod": "GET",
"path": "/hello",
"queryStringParameters": null,
"headers": {
"Accept": "*/*"
},
"body": null
}Local API Testing with `sam local start-api`
If your SAM application includes an API Gateway, you can simulate the entire API locally using sam local start-api. This command starts a local web server that mimics API Gateway.
You can then send HTTP requests to http://127.0.0.1:3000 and test your API endpoints end-to-end, locally.
Preparing for Deployment: `sam deploy`
Once your application is built and tested locally, you're ready to deploy it to AWS. The sam deploy command is used for this.
It packages your application artifacts, uploads them to an S3 bucket, and then uses AWS CloudFormation to deploy your serverless resources.
Your First Guided Deployment
For your first deployment, using the --guided flag is highly recommended. It will walk you through setting up necessary parameters interactively.
- Stack Name: Unique identifier for your CloudFormation stack.
- AWS Region: Where to deploy your resources.
- S3 Bucket: For storing deployment artifacts.
- Capabilities: Acknowledging IAM resource creation.
Example: sam deploy --guided
Managing Multiple Environments
In real-world scenarios, you'll often have development, staging, and production environments. SAM CLI helps manage this by allowing you to specify different stack names and parameters.
- Use distinct Stack Names (e.g.,
my-app-dev,my-app-prod). - Use
--parameter-overridesto pass different values for environment-specific configurations (e.g., database names, API keys).
Quick Check
Which SAM CLI command is used to test your API Gateway endpoints locally?
Recap & Next Steps
Congratulations! You've learned the essential steps for building, testing, and deploying serverless applications using the SAM CLI.
sam build: Packages and prepares your application.sam local invoke: Tests individual Lambda functions locally.sam local start-api: Simulates API Gateway for local API testing.sam deploy: Deploys your application to AWS CloudFormation.
Mastering these commands is key to efficient serverless development!
Frequently asked questions
Is the “Deploying SAM Applications” lesson free?
Yes — the full text of “Deploying SAM Applications” is free to read here on the web, and the Serverless Backend with AWS Lambda & API Gateway course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Serverless Backend with AWS Lambda & API Gateway course, upgrade to CoddyKit PRO.
What will I learn in “Deploying SAM Applications”?
Use the SAM CLI to build, test locally, and deploy your serverless applications to AWS, managing different environments. You practise Serverless Backend with AWS Lambda & API Gateway with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Serverless Backend with AWS Lambda & API Gateway?
No prior experience is required. Serverless Backend with AWS Lambda & API Gateway on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Deploying SAM Applications” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Serverless Backend with AWS Lambda & API Gateway lesson?
Yes. Every Serverless Backend with AWS Lambda & API Gateway lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Introduction to AWS SAM
- Defining Serverless Resources
- Deploying SAM Applications
- Local Testing and Debugging with SAM CLI