Skip to content

Complete Guide: Creating a SAM Project and Deploying to AWS


Step 0 — Prerequisites

  1. AWS Account

  2. AWS CLI Installed & Configured

    bash
    aws configure
    
    • Enter Access Key ID, Secret Access Key, region (e.g., us-east-1), output (json).
  3. Python 3.10+ Installed

    bash
    python --version
    
  4. SAM CLI Installed

  5. 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:

  1. Stack name: weather-api-stack
  2. Region: us-east-1
  3. S3 bucket: (SAM can create one)
  4. Allow SAM to create IAM roles: yes
  5. 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

  1. Sign up for OpenWeatherMap API.
  2. Add API key as environment variable in template.yaml:
yaml
Environment:
  Variables:
    WEATHER_API_KEY: "YOUR_API_KEY"
  1. Update app.py to call API with requests.
  2. Rebuild and redeploy:
bash
sam build
sam deploy

Step 8 — Summary Workflow

  1. sam init → Create project
  2. app.py → Write Lambda code
  3. template.yaml → Define infra
  4. requirements.txt → Add dependencies
  5. sam build → Build package
  6. sam local start-api → Test locally
  7. sam deploy --guided → Deploy to AWS
  8. API Gateway URL → Use API

Tips

  • Always match Python runtime locally with Lambda runtime.
  • Use sam local invoke for direct Lambda testing.
  • Update code → sam buildsam deploy to push changes.
  • AWS charges may apply for Lambda and API Gateway usage.