Serverless CLI and SAM Snippets

Copy-paste snippets for Serverless on AWS.

SAM workflow

sam validate --lint
sam build
sam local invoke ProducerFunction --event events/create-order.json
sam local start-api
sam deploy --guided
sam deploy   # after samconfig.toml exists

Combined template excerpt

AWSTemplateFormatVersion: "2010-09-09"
Transform: AWS::Serverless-2016-10-31

Globals:
  Function:
    Runtime: python3.12
    Timeout: 30
    MemorySize: 256
    Tracing: Active

Resources:
  OrderHttpApi:
    Type: AWS::Serverless::HttpApi

  OrdersTable:
    Type: AWS::DynamoDB::Table
    Properties:
      BillingMode: PAY_PER_REQUEST
      AttributeDefinitions:
        - AttributeName: orderId
          AttributeType: S
      KeySchema:
        - AttributeName: orderId
          KeyType: HASH

  OrderQueue:
    Type: AWS::SQS::Queue
    Properties:
      VisibilityTimeout: 60

ECS CPU target tracking policy

{
  "TargetValue": 70.0,
  "PredefinedMetricSpecification": {
    "PredefinedMetricType": "ECSServiceAverageCPUUtilization"
  },
  "ScaleInCooldown": 120,
  "ScaleOutCooldown": 60
}

ECS SQS backlog policy

{
  "TargetValue": 100.0,
  "CustomizedMetricSpecification": {
    "MetricName": "ApproximateNumberOfMessagesVisible",
    "Namespace": "AWS/SQS",
    "Dimensions": [
      { "Name": "QueueName", "Value": "order-events" }
    ],
    "Statistic": "Average"
  },
  "ScaleInCooldown": 120,
  "ScaleOutCooldown": 60
}

Lambda alias and concurrency

aws lambda publish-version --function-name order-serverless-ProducerFunction
aws lambda create-alias \
  --function-name order-serverless-ProducerFunction \
  --name prod \
  --function-version 1

aws lambda put-function-concurrency \
  --function-name order-serverless-WorkerFunction \
  --reserved-concurrent-executions 25

CodeDeploy canary deploy group

aws deploy create-deployment-group \
  --application-name order-serverless \
  --deployment-group-name prod \
  --service-role-arn arn:aws:iam::ACCOUNT:role/CodeDeployLambdaRole \
  --deployment-config-name CodeDeployDefault.LambdaCanary10Percent5Minutes