Lesson 3 of 7 Amazon API Gateway

Deploying APIs to Stages with Stage Variables, Canaries and Caching

This lesson explains how changes to an API go live. Changes take effect only when they are deployed to a stage, and stages let one API definition serve several environments, such as dev, test and prod.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain why REST API changes are not visible to clients until they are deployed to a stage.
  • Describe how stage variables select a different backend, such as a Lambda alias, for each stage.
  • Explain how a canary release tests a new deployment on a share of real traffic.
  • Describe how stage-level response caching reduces latency and backend load.

Deployments and stages

In a REST API, editing resources, methods or integrations changes only a working copy of the API definition. Clients see nothing until the API is deployed. A deployment is an immutable snapshot of the API definition at that moment. A stage, such as dev, test or prod, is a named pointer to one deployment, with its own invoke URL, for example https://abc123.execute-api.eu-west-1.amazonaws.com/prod. Deploying a new snapshot to dev leaves prod unchanged, and rolling back a stage means pointing it at an earlier deployment.

Each stage also has its own settings. The following list describes the most important ones.

  • Throttling limits for the stage and for individual methods.
  • Response caching, for REST APIs, described below.
  • Logging and tracing: execution logs of each request's processing, access logs in a custom format, detailed per-method CloudWatch metrics and AWS X-Ray tracing.

Execution logging needs an IAM role that allows API Gateway to write to CloudWatch Logs, set once in the account-level API Gateway settings; without it, logging cannot be enabled. When changes made in the console are not visible to clients, the most likely reason is that the API has not been redeployed to the stage. HTTP APIs can instead use automatic deployment, which deploys every change to a stage immediately.

True or false

Deployments and stages

Exam skills 3.1.5 · 3.2.4

Check three facts about how REST API changes reach a stage.

Log in to try this activity Interactive activities are free with an account.

Stage variables

Stage variables work like environment variables for a stage. A common pattern is to select a Lambda alias through a stage variable, so each stage calls a different version of the same function.

Example. The integration URI uses ${stageVariables.lambdaAlias}. The dev stage sets lambdaAlias=dev and the prod stage sets lambdaAlias=live.

arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:111122223333:function:orders-api:${stageVariables.lambdaAlias}/invocations

API Gateway needs permission to invoke each alias, so the function's resource-based policy must include a statement for every alias the variable can name. A missing statement is a common cause of 500 errors on just one stage.

Canary releases

A stage can run a canary release, which tests a new deployment on a small share of real traffic before it replaces the current one. The developer deploys the new version to the stage as a canary and sets the percentage of requests it should receive, such as 10%. The remaining requests continue to use the current deployment. The canary has its own logs and metrics, and can have its own stage variable values, so its behaviour can be compared with the current version.

If the canary performs well, it is promoted, which makes its deployment the stage's main deployment for all traffic. If it does not, the canary is deleted, and all traffic returns to the existing deployment. Canary releases apply to REST APIs; for Lambda-backed APIs, traffic can also be shifted gradually between function versions with Lambda aliases and AWS CodeDeploy.

The following diagram shows how a stage divides traffic during a canary release and the two ways it ends.

A canary release on a REST API stage. The current deployment receives 90% of requests and the canary deployment 10%, with its own logs, metrics and stage variable values. Promoting the canary makes it the stage's main deployment; deleting it returns all traffic to the current deployment.
Both deployments serve the same stage URL, so clients need no change while the canary is tested.
Put in order

Run a canary release

Exam skills 3.3.6

Put the steps of a canary release on a REST API stage in order.

Log in to try this activity Interactive activities are free with an account.

Response caching

REST APIs can cache responses for each stage, so that repeated identical requests are answered by API Gateway without calling the backend, which reduces both latency and back-end load. The cache is provisioned with a chosen capacity and is charged per hour. Cached responses are kept for a time to live (TTL) between 0 and 3,600 seconds, 300 seconds by default.

API Gateway decides whether two requests are the same by their cache keys. By default, the key is the resource path; the developer chooses which headers, path parameters and query strings also form part of it. If a response depends on a query string that is not part of the key, different users would wrongly receive each other's cached responses.

Clients can ask API Gateway to bypass the cache by sending the header Cache-Control: max-age=0. Unless the stage is configured to require authorisation for this, anyone can force every request through to the backend, which removes the protection the cache was meant to provide.

300s

Default cache TTL

3,600s

Maximum cache TTL

Scenario questions

Stages, variables and caching in practice

Exam skills 3.1.5 · 4.3.4

Practise the stage settings that exam scenarios turn on.

Log in to try this activity Interactive activities are free with an account.
Worked example

"my change isn't live"

A developer adds a new /refunds resource in the console, tests it in the console's test panel successfully, but clients receive 403 errors. The reason is that REST API changes take effect only when deployed to a stage.

StepAction
1Create a deployment to the dev stage and test with the dev URL
2Create a deployment to prod with canary settings sending 10% of traffic to the new deployment
3Compare canary errors in CloudWatch, then promote the canary

The stage variable lambdaAlias is dev on the dev stage and live on prod, so one API definition calls the right function alias in each environment. Stage caching is enabled on prod only, with a 300-second TTL for product lookups.

Check yourself

Which value can a stage variable supply so that the same REST API calls a different Lambda alias in each stage?

Show answer

The integration's function ARN, for example ...:function:orders-api:${stageVariables.lambdaAlias}. Each stage sets lambdaAlias to its own value, such as dev or live, and each alias needs a resource-based policy that allows API Gateway to invoke it.

Exam Tips

Remember for the exam

  • "My changes are not visible" usually means the API has not been redeployed to the stage.
  • To route each environment to a different backend from one API, use stage variables.
  • 500 errors on only one stage that uses a stage-variable alias usually mean a missing Lambda invoke permission for that alias.
  • Execution logging requires the CloudWatch Logs role ARN in the API Gateway account settings.
  • The default cache TTL is 300 seconds and the maximum is 3,600 seconds.