Lesson 19 / 32

Infrastructure as Code for Serverless

Describe functions, triggers, permissions and tables in a reviewable template and deploy them repeatably.

By · 15 years in web development

Many small resources, one source of truth

A serverless app is many small cloud resources: functions, permissions, routes, queues, tables and schedules. Clicking them together in a web console is not reproducible, and nobody can review it. Infrastructure as code (IaC) describes them in files that live in Git, get reviewed in pull requests and deploy the same way every time. Options range from general tools (Terraform or OpenTofu, AWS CDK, Pulumi) to serverless-focused ones: AWS SAM adds concise resource types on top of CloudFormation, the Serverless Framework and SST offer higher-level abstractions, and Wrangler configures and deploys Cloudflare Workers. Whatever you choose, the template should declare each function's runtime, memory, timeout, environment variables, triggers and a least-privilege role. Then the permission to read one table is reviewed in the same pull request as the code that reads it, and staging and production are built from the same definition.

An AWS SAM template

One function with an HTTP route, a table, and a policy template that grants read-only access to that table alone. !Ref wires resources together, so no names are hard-coded.

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

Resources:
  WeatherFunction:
    Type: AWS::Serverless::Function
    Properties:
      Runtime: nodejs22.x
      Handler: index.handler
      CodeUri: src/
      MemorySize: 512
      Timeout: 10
      Environment:
        Variables:
          TABLE_NAME: !Ref ReadingsTable
      Policies:
        - DynamoDBReadPolicy:
            TableName: !Ref ReadingsTable
      Events:
        GetWeather:
          Type: HttpApi
          Properties:
            Path: /weather
            Method: get

  ReadingsTable:
    Type: AWS::Serverless::SimpleTable

From commit to a live version

Code is committed, built into a deployable package, deployed by the IaC tool, and published as a new version that traffic can be shifted to.

Four connected stages from left to right: source code, the build and package step, the infrastructure-as-code deployment, and a published version with an alias.
Figure 7.1: code -> build and package -> IaC deploy -> version and alias

Runtimes have an end-of-life date

Managed runtimes such as nodejs22.x are deprecated some time after the language version reaches end of life, after which you can no longer create or update functions on them. Keep runtime versions in the template, track the provider's deprecation schedule, and upgrade like any other dependency.

Quick check: What is the main benefit of declaring a function's IAM permissions in the same IaC template as the function?

  • Permissions become unnecessary once they are in a template
  • The function runs faster
  • Permission changes are versioned and reviewed together with the code that needs them
  • The template automatically grants administrator access during development
Answer

Permission changes are versioned and reviewed together with the code that needs them — IaC puts permissions under version control and code review, so least privilege can actually be checked. It does not change performance or remove the need for permissions.