3.1.6 SAM Commands & Deployment

SAM Commands & Deployment

SAM Deployment Workflow

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?

SAM Commands

CommandPurpose
sam buildBuild application, prepare artifacts
sam packageZip code artifacts, upload to S3, produce packaged template
sam deployDeploy application to AWS
sam local invokeTest function locally
sam local start-apiRun API Gateway locally

SAM Package Command

sam package does:

  1. Zips your code artifacts
  2. Uploads them to S3
  3. Produces packaged SAM template file ready for 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

sam deploy does:

  • Deploy application to AWS
  • Create/update CloudFormation stack
  • Deploy Lambda functions, API Gateway, DynamoDB, etc.
# First time deployment with prompts
sam deploy --guided

# Subsequent deployments
sam deploy

# With parameters
sam deploy --parameter-overrides Stage=prod

Transform Section for SAM

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

Multi-Region Deployment Scenario

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

SAM Build

# Build application
sam build

# Build with container (for native dependencies)
sam build --use-container

# Build specific function
sam build MyFunction

SAM Local Testing

# 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

Complete SAM Workflow

# 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

SAM vs CloudFormation

AspectSAMCloudFormation
SyntaxSimplified for serverlessFull AWS resources
TransformRequired AWS::Serverless-2016-10-31Optional
ResourcesServerless-specific + CFNAll AWS resources
Deploymentsam deployaws cloudformation deploy
Local testingsam localNot available

Common Exam Scenarios

Scenario 1: Zip artifacts, upload S3, produce template

  • Answer: sam package

Scenario 2: Specify SAM version trong template

  • Answer: Transform section với AWS::Serverless-2016-10-31

Scenario 3: Deploy SAM app to multiple regions

  • Answer: Use Parameters, deploy với different --region

Scenario 4: Build Lambda với native dependencies

  • Answer: sam build --use-container

Scenario 5: Test Lambda locally before deploy

  • Answer: sam local invoke

Exam Tip:

  • sam package = zip + upload S3 + produce packaged template
  • sam deploy = deploy to AWS
  • Transform section = specify SAM version (AWS::Serverless-2016-10-31)
  • sam build --use-container = build với native dependencies
  • SAM template = CloudFormation template + Transform + Serverless resources
  • Multi-region: Use Parameters + deploy với different --region