Sub topic 10
Stages and deploying the API
This chapter assumes you can read a SAM template, have the AWS CLI and SAM CLI installed, and know what a route and a Lambda integration are. You will learn what a stage is, how deployment works for HTTP APIs, and how to deploy and call your API.
What a stage is
A stage is a named, deployable snapshot of an API, such as dev or prod. Each stage has its own invoke URL, its own throttling settings, and its own access log settings. Routes and integrations belong to the API, and a stage exposes them to callers.
HTTP APIs support a special stage named $default. Its URL has no stage name in the path, so https://abc123.execute-api.us-east-1.amazonaws.com/items works without a prefix. A named stage adds its name: https://abc123.execute-api.us-east-1.amazonaws.com/prod/items.
Auto-deploy
REST APIs require you to create a deployment each time you change them. HTTP APIs can use auto-deploy on a stage. With auto-deploy on, every change to routes or integrations goes live on that stage without a separate deployment step. SAM turns this on for the implicit $default stage that it creates, so the template from the previous chapter needs no stage resource.
If you want a named stage, add StageName to the API:
ItemsApi:
Type: AWS::Serverless::HttpApi
Properties:
StageName: prodThe template from the previous chapter then serves routes under /prod. Any client must include that prefix.
Deploying with SAM
Run these commands in the folder that holds template.yaml:
sam build
sam deploy --guidedsam build packages your code from CodeUri. The first sam deploy --guided asks for a stack name, a Region, and whether to save the answers to samconfig.toml. Later runs need only sam deploy. When the stack finishes, SAM prints the ApiUrl output from the template.
To read the output again later:
aws cloudformation describe-stacks \
--stack-name items-api \
--query "Stacks[0].Outputs"Then call the API with the curl commands from earlier in this tutorial, replacing API_URL with the printed value. If you used a named stage, append /prod.
Common errors
| Symptom | Likely cause |
|---|---|
| `{"message":"Not Found"}` from API Gateway | The URL is missing the stage prefix, or no route matches the method and path |
| `{"message":"Forbidden"}` | An authorizer rejected the call, or the route does not exist for that method |
| Changes do not appear | Auto-deploy is off on the stage, or the stack update has not finished |
A "Not Found" body in this exact shape comes from API Gateway itself. Your function returns its own JSON, such as {"message": "Not found"} from the handler, so the shape tells you which layer replied.
Exercise
You change the template to set StageName: prod and redeploy. Your old curl call to $API_URL/items/42 now returns {"message":"Not Found"}, and the function logs show no invocation. What is the most likely cause and the fix?
Show answer
The API no longer uses the $default stage, so the path needs the stage prefix. The request never matched a route, so Lambda was not invoked. Call $API_URL/prod/items/42 instead.