Scenario thi: Built serverless application using SAM, using CloudFormation as underlying deployment mechanism. Application ready for deployment. Cần zip CodeArtifacts, upload to S3, produce package template file. Dùng CLI như thế nào?
| Command | Purpose |
|---|---|
sam build | Build application, prepare artifacts |
sam package | Zip code artifacts, upload to S3, produce packaged template |
sam deploy | Deploy application to AWS |
sam local invoke | Test function locally |
sam local start-api | Run API Gateway locally |
sam package does:
sam deploy# Package application
sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket my-deployment-bucket
# Deploy packaged application
sam deploy \
--template-file packaged.yaml \
--stack-name my-app \
--capabilities CAPABILITY_IAM
sam deploy does:
# First time deployment with prompts
sam deploy --guided
# Subsequent deployments
sam deploy
# With parameters
sam deploy --parameter-overrides Stage=prod
Scenario thi: Using SAM to build/deploy serverless infrastructure. Tasked to create CloudFormation template includes SAM script + other service configurations. Template will launch similar infrastructure trong another Region. Add gì vào template?
Answer: Transform section
Transform section specifies version of SAM to use:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31 # Required for SAM
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
Scenario: Create template với SAM + other services, deploy to another Region
Template structure:
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Parameters:
Region:
Type: String
Default: us-east-1
Resources:
# SAM resources
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
Runtime: python3.12
# Other CloudFormation resources
MyBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub "my-bucket-${Region}"
MyDynamoDBTable:
Type: AWS::DynamoDB::Table
Properties:
TableName: !Sub "my-table-${Region}"
BillingMode: PAY_PER_REQUEST
AttributeDefinitions:
- AttributeName: id
AttributeType: S
KeySchema:
- AttributeName: id
KeyType: HASH
Deploy to different regions:
# Deploy to us-east-1
sam deploy --region us-east-1 --parameter-overrides Region=us-east-1
# Deploy to eu-west-1
sam deploy --region eu-west-1 --parameter-overrides Region=eu-west-1
# Build application
sam build
# Build with container (for native dependencies)
sam build --use-container
# Build specific function
sam build MyFunction
# Invoke function locally
sam local invoke MyFunction -e events/event.json
# Start API Gateway locally
sam local start-api
# Start Lambda locally
sam local start-lambda
# 1. Initialize project
sam init
# 2. Build application
sam build
# 3. Test locally
sam local invoke -e events/test.json
# 4. Package (zip + upload to S3)
sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket my-deployment-bucket
# 5. Deploy
sam deploy \
--template-file packaged.yaml \
--stack-name my-app \
--capabilities CAPABILITY_IAM \
--parameter-overrides Stage=prod
# Or use guided deployment (first time)
sam deploy --guided
| Aspect | SAM | CloudFormation |
|---|---|---|
| Syntax | Simplified for serverless | Full AWS resources |
| Transform | Required AWS::Serverless-2016-10-31 | Optional |
| Resources | Serverless-specific + CFN | All AWS resources |
| Deployment | sam deploy | aws cloudformation deploy |
| Local testing | sam local | Not available |
Scenario 1: Zip artifacts, upload S3, produce template
sam packageScenario 2: Specify SAM version trong template
AWS::Serverless-2016-10-31Scenario 3: Deploy SAM app to multiple regions
--regionScenario 4: Build Lambda với native dependencies
sam build --use-containerScenario 5: Test Lambda locally before deploy
sam local invokeExam Tip:
sam package = zip + upload S3 + produce packaged templatesam deploy = deploy to AWSAWS::Serverless-2016-10-31)sam build --use-container = build với native dependencies--region