Understanding Amazon API Gateway Lambda Authorizer Caching (REST)
The API Gateway Lambda authorizer cache operates per stage, not per method. Here's why that causes surprise access denials, and two ways to fix it
When you protect an API Gateway REST API with an AWS Lambda authorizer (formerly known as a custom authorizer), you can turn on the built-in authorization cache to save on Lambda invocations and cut latency. It's a great feature, but the way the cache is scoped trips a lot of people up.
In this post I want to explain how the cache actually applies across the resources and methods in a REST API, because it behaves differently from what most people assume. Getting it wrong can silently turn an allow into a deny.
This post is about REST APIs. HTTP APIs use a different, simpler Lambda authorizer model with a separate caching story, so the details below don't map directly onto them.
Setting up a Lambda authorizer cache
There are two types of Lambda authorizer:
-
TOKEN— the simplest option. You specify a header (normallyAuthorization) that carries the caller's identity, typically a bearer JWT or OAuth token. When caching is on, the value of that header becomes the cache key.
-
REQUEST— receives the caller's identity from a combination of headers, query string parameters,stageVariables, and$contextvariables. When caching is on, all of the identity sources you configure are concatenated (in order) to form the cache key. We'll see later why that matters.

Comparison of API Gateway Lambda authorizers (TOKEN vs REQUEST)
Note: the AWS documentation recommends to use aREQUESTauthorizer where you can. It supports multiple identity sources and lets you build a fine-grained cache key, whereas aTOKENauthorizer is limited to a single header. That recommendation is directly relevant to the caching problem below.
Both types let you enable authorization caching with a TTL of 0–3600 seconds. 3600 seconds (60 minutes) is a hard limit that can't be increased. Setting the TTL to
0 disables caching entirely.What is stored in the cache?
Quite simply, the cache stores the Lambda authorizer function output. This includes the
policyDocument API Gateway uses to decide whether the caller is allowed or denied:{ "principalId": "yyyyyyyy", "policyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "execute-api:Invoke", "Effect": "Allow|Deny", "Resource": "arn:aws:execute-api:{regionId}:{accountId}:{apiId}/{stage}/{httpVerb}/[{resource}/[{child-resources}]]" } ] }, "context": { "stringKey": "value", "numberKey": "1", "booleanKey": "true" }, "usageIdentifierKey": "{api-key}"}For example, the following output denies calls to the
GET method for the dev stage of API ymy8tbxw7b in account 123456789012 :{ "principalId": "user", "policyDocument": { "Version": "2012-10-17", "Statement": [ { "Action": "execute-api:Invoke", "Effect": "Deny", "Resource": "arn:aws:execute-api:us-west-2:123456789012:ymy8tbxw7b/dev/GET/" } ] }}What level is the cache applied at?
This is the common misunderstanding. The cache operates per API stage — not per resource, and not per method. (More precisely, per stage per cache key — hold that thought.)
So with a plain
TOKEN authorizer, or a REQUEST authorizer whose only identity source is a token header, the following calls all return the same cached authorizer response for the prod stage:GET https://myapi.com/prod/pets/cats [trigger Lambda, generate policy, store in cache for TTL]GET https://myapi.com/prod/pets/cats [return policy from cache]GET https://myapi.com/prod/pets/dogs [return policy from cache]POST https://myapi.com/prod/pets/cats [return policy from cache]Because it's cached at the stage level, you can't selectively evict one resource. To clear it you flush the entire stage's authorizer cache using the FlushStageAuthorizersCache API.
Using the AWS CLI:
aws apigateway flush-stage-authorizers-cache \ --rest-api-id 1234123412 \ --stage-name devWhat's the common mistake?
If you enable caching, your authorizer must return a policy that is applicable to all methods across an API. To enforce a method-specific policy, use the context variables$context.pathand$context.httpMethod.
The trap is that people write an authorizer that returns an allow only for the specific resource and method being called at that moment. Because the policy is cached for the whole stage, that same narrow policy is then replayed for subsequent calls to different resources and methods, where it results in an implicit deny. So where you expected an allow, the caller gets denied.
The example Lambda code in the docs builds its policy from
event.methodArn — the ARN of the single method that triggered the authorizer. That's fine with caching off, but it causes exactly this problem when caching is on. Consider a user who should be allowed to reach both the cats and dogs resources:- User calls
https://myapi.com/prod/pets/cats.
- The generated policy allows the user and is stored in the cache:
{ "Version": "2012-10-17", "Statement": [ { "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:eu-west-1:123456789012:s2gse9wr12/prod/GET/pets/cats" } ]}- User calls
https://myapi.com/prod/pets/dogs.
- The user is implicitly denied by the cached policy, because it only allows
/pets/cats.
With caching disabled, the user would be allowed correctly, because the authorizer would regenerate the policy from
methodArn on every call. But then you lose the cost and latency benefits — so let's fix it properly instead.How do I handle the cache correctly?
There are two good approaches:
- Return a policy that covers the whole stage. Include all the resources and methods the caller should reach in the returned
policyDocument, using wildcards where appropriate.
- Make the cache key more specific. For
REQUESTauthorizers, add$context.path(or$context.resourcePath) and$context.httpMethodas identity sources so you effectively get a cache entry per method, per resource.
Let's look at both.
More on the $context cache key
REQUEST authorizers let you use $context variables as identity sources. Because every configured identity source becomes part of the cache key, adding $context.httpMethod and the request path means each method/resource combination gets its own cache entry.A note on which path variable to use:$context.resourcePathgives you the resource template (for example/pets/{petId}), while$context.pathincludes the stage and the resolved path (for example/prod/pets/123). Prefer$context.resourcePathwith$context.httpMethodif you want one cache entry per method + resource definition, rather than a separate entry per unique path value.

Adding
httpMethod and resourcePath as $context identity sources on a REQUEST authorizerWith those context variables added as identity sources, the earlier sequence behaves the way you'd expect:
GET https://myapi.com/prod/pets/cats [trigger Lambda, generate policy, store in cache for TTL]GET https://myapi.com/prod/pets/cats [return policy from cache]GET https://myapi.com/prod/pets/dogs [trigger Lambda, generate policy, store in cache for TTL]POST https://myapi.com/prod/pets/cats [trigger Lambda, generate policy, store in cache for TTL]One thing to be aware of: when caching is on for a
REQUEST authorizer, API Gateway requires every configured identity source to be present in the request. If one is missing, null, or empty, API Gateway returns 401 Unauthorized without even invoking your Lambda function.Can I only do that with REQUEST authorizers?
Yes. The
$context cache-key technique is only available for REQUEST authorizers. If you're using a TOKEN authorizer, you have to return a policyDocument that covers all the resources and methods the client might call within the cache TTL.One option is to explicitly list every allowed (or denied) action:
{ "Version": "2012-10-17", "Statement": [ { "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:eu-west-1:123456789012:s2gse9wr12/prod/GET/pets/cats" }, { "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:eu-west-1:123456789012:s2gse9wr12/prod/GET/pets/dogs" }, { "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:eu-west-1:123456789012:s2gse9wr12/prod/POST/pets/cats" } ]}Another option is to use wildcards in the resource ARNs. This is less secure than enumerating each resource and method explicitly, because you're no longer deliberately deciding what the caller can reach — you could unintentionally grant access to a resource added to the API later. It does, however, keep the logic simpler and helps you stay under the policy size limit.
{ "Version": "2012-10-17", "Statement": [ { "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:eu-west-1:123456789012:s2gse9wr12/prod/GET/*" }, { "Action": "execute-api:Invoke", "Effect": "Allow", "Resource": "arn:aws:execute-api:eu-west-1:123456789012:s2gse9wr12/prod/POST/pets/cats" } ]}Wrapping up
The key mental model to keep: a REST API Lambda authorizer's policy is cached per stage, per cache key — not per method. If your cache key doesn't distinguish between methods and resources, then whatever policy the first call produces is what every subsequent caller gets until the TTL expires.
So you have two levers:
- Broaden the policy to cover the whole stage (explicit statements or wildcards), or
- Narrow the cache key using
$context.httpMethodand the request path (REQUESTauthorizers only).
Whichever you choose, make the decision deliberately. The convenient default of building the policy from a single
methodArn is exactly what causes the surprise denials.For the full rules on valid execute-api policies, see Statement reference of IAM policies for executing an API in API Gateway .
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article