Opscraft
Serverless with AWS Lambda · Lesson 4 of 6 · 3 min read

Building and deploying functions

Packaging

  • Zip archives for most functions, with layers for shared dependencies (up to 5 layers, counting towards the 250 MB unzipped limit).
  • Container images (up to 10 GB) when you have big dependencies, native libraries, or want the same build tooling as your containers. The image must implement the Lambda Runtime API, easiest by starting from an AWS base image.
  • Pin runtimes and plan upgrades: AWS deprecates runtimes when the language version reaches end of life, after which you can't create or update functions on them.

Infrastructure as code, versions and aliases

Define functions with AWS SAM (a CloudFormation extension with sam build, sam local invoke, sam deploy), CDK, Terraform or the Serverless Framework, never by hand in the console.

  • A version is an immutable snapshot of code and configuration (my-fn:7). $LATEST is the mutable working copy.
  • An alias (prod, staging) is a named pointer to a version, and callers invoke the alias. Aliases can split traffic between two versions.
  • CodeDeploy uses alias weights for safe releases (Canary10Percent5Minutes, Linear10PercentEvery1Minute), watching CloudWatch alarms and rolling back automatically.
Enlarge diagramalias: livecallers use thisversion 690% of trafficversion 7 (new)10% for 5 minutesalarmserrors, p99
Figure 1. Safe releases: an alias splits traffic between versions while CodeDeploy watches alarms and rolls back on errors.
Swipe sideways to see all of it, or tap to open it full screen.
# SAM template excerpt
OrdersFunction:
  Type: AWS::Serverless::Function
  Properties:
    Handler: app.handler
    Runtime: python3.12
    Architectures: [arm64]
    MemorySize: 512
    Timeout: 10
    AutoPublishAlias: live
    DeploymentPreference:
      Type: Canary10Percent5Minutes
      Alarms: [!Ref OrdersErrorsAlarm]
    Events:
      Queue:
        Type: SQS
        Properties: { Queue: !GetAtt OrdersQueue.Arn, BatchSize: 10, FunctionResponseTypes: [ReportBatchItemFailures] }

Permissions, networking and secrets

Two different policies control a function:

  • The execution role is what the function can do (read this table, write to that bucket). Keep it least-privilege, one role per function.
  • The resource-based policy is who may invoke the function (API Gateway, S3 in this account, EventBridge rule X). Triggers set it up for you, scoped by source ARN and account to avoid the confused-deputy problem.

Secrets: don't put secrets in plain environment variables visible in the console. Read them from Secrets Manager or Parameter Store at init (cached), or use the AWS Parameters and Secrets Lambda Extension, and encrypt environment variables with a KMS key.

Databases: hundreds of concurrent functions each opening a connection can exhaust a relational database. Put RDS Proxy in front to pool connections, cap concurrency with reserved concurrency, or use DynamoDB, which has no connection limit.