Complete Guide: Creating a SAM Project and Deploying to AWS
Step 0 — Prerequisites
AWS Account
- Sign up at https://aws.amazon.com/.
AWS CLI Installed & Configured
bashaws configure- Enter Access Key ID, Secret Access Key, region (e.g., us-east-1), output (json).
Python 3.10+ Installed
bashpython --versionSAM CLI Installed
Docker (Optional for local testing)
Step 1 — Initialize SAM Project
bash
sam init
Choose:
1 - AWS Quick Start Templates
1 - Hello World Example
Runtime: python3.10
Project name: weather-api
Folder structure:
weather-api/
├── template.yaml
├── hello_world/
│ ├── app.py
│ ├── requirements.txt
└── events/
└── event.json
Step 2 — Write Lambda Code
Edit hello_world/app.py:
python
import json
def lambda_handler(event, context):
city = event.get("queryStringParameters", {}).get("city", "Unknown")
weather_data = {
"city": city,
"temperature": "25°C",
"description": "Sunny"
}
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps(weather_data)
}
Optional: for real API, install requests and call OpenWeatherMap.
Step 3 — Update SAM Template
Edit template.yaml:
yaml
AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31
Description: Simple Weather API using AWS SAM
Globals:
Function:
Timeout: 5
Resources:
WeatherFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: hello_world/
Handler: app.lambda_handler
Runtime: python3.10
Architectures:
- x86_64
Events:
WeatherApi:
Type: Api
Properties:
Path: /weather
Method: get
Outputs:
WeatherApiEndpoint:
Description: "API Gateway endpoint URL for Weather API"
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.${AWS::URLSuffix}/Prod/weather"
Step 4 — Add Dependencies
If using external packages, edit hello_world/requirements.txt:
requests
Then build:
bash
sam build
Step 5 — Test Locally
bash
sam local start-api
- API available at
http://127.0.0.1:3000/weather - Test:
bash
curl "http://127.0.0.1:3000/weather?city=Manila"
Expected output:
json
{"city": "Manila", "temperature": "25°C", "description": "Sunny"}
Step 6 — Deploy to AWS
bash
sam deploy --guided
Prompts:
- Stack name:
weather-api-stack - Region:
us-east-1 - S3 bucket: (SAM can create one)
- Allow SAM to create IAM roles: yes
- Save configuration: yes
After deployment, you get:
WeatherApiEndpoint: https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/weather
- Test live API:
bash
curl "https://xxxxxxxxxx.execute-api.us-east-1.amazonaws.com/Prod/weather?city=Tokyo"
Step 7 — Optional: Real Weather API
- Sign up for OpenWeatherMap API.
- Add API key as environment variable in
template.yaml:
yaml
Environment:
Variables:
WEATHER_API_KEY: "YOUR_API_KEY"
- Update
app.pyto call API withrequests. - Rebuild and redeploy:
bash
sam build
sam deploy
Step 8 — Summary Workflow
sam init→ Create projectapp.py→ Write Lambda codetemplate.yaml→ Define infrarequirements.txt→ Add dependenciessam build→ Build packagesam local start-api→ Test locallysam deploy --guided→ Deploy to AWS- API Gateway URL → Use API
✅ Tips
- Always match Python runtime locally with Lambda runtime.
- Use
sam local invokefor direct Lambda testing. - Update code →
sam build→sam deployto push changes. - AWS charges may apply for Lambda and API Gateway usage.