AWS SAM (Serverless Application Model) makes it easy to deploy Lambda, API Gateway, and DynamoDB. This section guides you through backend deployment with SAM.
SAM deployment follows these steps:
# macOS
brew install aws-sam-cli
# Linux
pip install aws-sam-cli
# Windows
choco install aws-sam-cli
# Verify
sam --version
cd lexi-be
# Build
sam build
# Result: .aws-sam/build/ directory
SAM build will: Compile Python code, install dependencies, package Lambda functions, prepare CloudFormation template
# Deploy with guided mode
sam deploy --guided
Answer prompts:
Stack Name [lexi-be]: lexi-be
AWS Region [ap-southeast-1]: ap-southeast-1
Parameter AuthBaseStackName [lexi-auth-base]: lexi-auth-base
Parameter DatabaseStackName [lexi-database]: lexi-database
Confirm changes before deploy [Y/n]: Y
Allow SAM CLI IAM role creation [Y/n]: Y
Disable rollback [y/N]: N
Save arguments to configuration file [Y/n]: Y
SAM configuration file [samconfig.toml]: samconfig.toml
SAM configuration environment [default]: default
# Deploy with saved config
sam deploy
# Or deploy specific stack
sam deploy --stack-name lexi-be --region ap-southeast-1
# List stacks
aws cloudformation list-stacks \
--region <REGION> \
--query 'StackSummaries[?StackStatus!=`DELETE_COMPLETE`]'
# Describe stack
aws cloudformation describe-stacks \
--stack-name lexi-be \
--region <REGION>
# Get outputs
sam list stack-outputs \
--stack-name lexi-be \
--region <REGION>
# Get API URL
API_URL=$(aws cloudformation describe-stacks \
--stack-name lexi-be \
--region <REGION> \
--query 'Stacks[0].Outputs[?OutputKey==`ApiUrl`].OutputValue' \
--output text)
# Test health endpoint
curl $API_URL/health
# Test with auth
curl -H "Authorization: Bearer <TOKEN>" \
$API_URL/profile
# After code changes
sam build
sam deploy
# SAM will:
# 1. Detect changes
# 2. Create changeset
# 3. Execute changeset
# 4. Update resources
# Rollback stack
aws cloudformation cancel-update-stack \
--stack-name lexi-be \
--region <REGION>
# Or delete and redeploy
sam delete --stack-name lexi-be --region <REGION>
sam deploy
File samconfig.toml stores configuration:
version = 0.1
[default.deploy.parameters]
stack_name = "lexi-be"
region = "ap-southeast-1"
capabilities = "CAPABILITY_IAM"
parameter_overrides = "AuthBaseStackName=lexi-auth-base DatabaseStackName=lexi-database"
confirm_changeset = true
resolve_s3 = true
# Check Python version
python --version # Should be 3.12+
# Check requirements.txt
cat requirements.txt
# Update existing stack
sam deploy --no-confirm-changeset
# Or delete and recreate
sam delete --stack-name lexi-be
sam deploy --guided
# View logs
sam logs -n <FUNCTION_NAME> --stack-name lexi-be --tail
# Invoke function locally
sam local invoke <FUNCTION_NAME> -e events/event.json
Continue to Amplify Deploy to deploy the frontend.