AWS
SAM template
stage name
AWS Lambda
serverless applications

How to set a stage name in a SAM template

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In AWS SAM, stage naming affects URL structure, deployment isolation, and environment management. If stage configuration is unclear, teams often deploy to unexpected paths or overwrite shared environments. The clean solution is to define stage explicitly in your AWS::Serverless::Api resource and bind functions to that API.

Define Stage Name in AWS::Serverless::Api

Create an API resource with StageName and reference it from function events. This ensures predictable endpoint URLs and consistent environment separation.

yaml
1AWSTemplateFormatVersion: '2010-09-09'
2Transform: AWS::Serverless-2016-10-31
3Description: Example SAM template with explicit stage
4
5Resources:
6  HttpApi:
7    Type: AWS::Serverless::Api
8    Properties:
9      Name: OrdersApi
10      StageName: prod
11
12  GetOrderFunction:
13    Type: AWS::Serverless::Function
14    Properties:
15      Runtime: python3.12
16      Handler: app.handler
17      CodeUri: src/
18      Events:
19        GetOrder:
20          Type: Api
21          Properties:
22            RestApiId: !Ref HttpApi
23            Path: /orders/{id}
24            Method: get

With this pattern, endpoint paths include /prod unless custom domain mapping removes stage from public URL.

Parameterize Stage for Multi-Environment Deployment

For dev, test, and prod workflows, parameterize stage name. This enables one template to deploy to many environments safely.

yaml
1Parameters:
2  StageName:
3    Type: String
4    Default: dev
5    AllowedValues:
6      - dev
7      - test
8      - prod
9
10Resources:
11  HttpApi:
12    Type: AWS::Serverless::Api
13    Properties:
14      Name: OrdersApi
15      StageName: !Ref StageName

Deploy with parameter override:

bash
sam deploy \
  --stack-name orders-api-prod \
  --parameter-overrides StageName=prod

This avoids template duplication and keeps environment differences explicit.

Understand samconfig.toml Interaction

samconfig.toml can define default parameters per environment profile. Keep stage parameter values there for repeatable deployments.

A practical setup uses separate deploy profiles such as dev and prod with different stack names, regions, and parameter overrides. This reduces command-line mistakes during release operations.

Validate Stage and Routing After Deployment

After deployment, inspect API Gateway stage configuration and call a health endpoint. Confirm expected stage path and Lambda integration status.

bash
aws apigateway get-stages --rest-api-id YOUR_API_ID
curl https://YOUR_API_ID.execute-api.us-east-1.amazonaws.com/prod/orders/123

Use automated smoke checks in CI to verify stage and route availability before promoting traffic.

Deployment Workflow Across Environments

A stable SAM workflow uses one template, parameterized stage names, and separate pipeline jobs per environment. Each pipeline job should pass explicit StageName, stack name, and AWS account context. This avoids accidental deployment of development configuration to production. Store these values in CI variables or environment-specific config files and keep them versioned. After deployment, run smoke tests that hit a known health route under the expected stage path. If custom domains are used, test both execute-api URL and custom domain route so mapping issues are caught early. For rollbacks, keep previous stack artifacts available and make rollback commands part of the runbook. Teams that operationalize these steps rarely struggle with stage confusion in SAM-managed APIs.

bash
sam build
sam deploy --config-env dev
sam deploy --config-env prod

Verification Checklist

Include a pipeline step that prints deployed stack outputs and resolved API URL, then runs a stage-specific health check. This ensures stage name configuration, deployment target, and route wiring are validated together.

Common Pitfalls

  • Defining function Api events without binding to a shared AWS::Serverless::Api resource.
  • Forgetting to override stage parameter in production deployments.
  • Assuming custom domain mapping always includes stage path.
  • Reusing same stack name across environments and causing configuration overlap.

Also verify CloudWatch logs for the deployed stage alias so runtime traffic is landing in the expected environment.

Keep stage naming conventions short and consistent so URLs, logs, and alarms remain easy to scan during incidents.

When using custom domains, document whether base-path mapping removes the stage segment to avoid confusion during client integration.

Summary

  • Set stage explicitly in AWS::Serverless::Api.
  • Bind function events through RestApiId for consistent routing.
  • Parameterize stage for multi-environment workflows.
  • Use samconfig.toml profiles to reduce deployment mistakes.
  • Validate stage and route health after each deploy.

Course illustration
Course illustration

All Rights Reserved.