Can you use a path parameter as an identity source for an AWS API Gateway?

Viewed 1025

I have an AWS API Gateway (v2/HTTP) set-up with a custom authoriser lambda.

The route key for the gateway looks something like:

ANY /{project_id}/{uri+}

My authorizer lambda checks if the given user is entitled for permission for the provided project_id. Unfortunately this means I can't cache the result, as a user authorized for one project ID would then have that grant cached for other project IDs.

I saw that AWS provides an 'Identity Source' parameter for the gateway which it will use as a caching key:

You can enable caching for a Lambda authorizer by specifying an authorizerResultTtlInSeconds. When caching is enabled for an authorizer, API Gateway uses the authorizer's identity sources as the cache key. If a client specifies the same parameters in identity sources within the configured TTL, API Gateway uses the cached authorizer result, rather than invoking your Lambda function.

I would therefore like to use my 'project_id' path parameter, but it's not obvious to me if this is possible with the supported identity sources:

screenshot showing supported identity sources

Is it possible to refer to the project_id path parameter using these expressions?

1 Answers

Is it possible to refer to the project_id path parameter using these expressions?

Yes and no.

While you can't specifically target the path parameter, you can specify the route key which looks like the exact value of project_id to me in your example.

In that case, use $context.routeKey, which is one of the supported context variables as your identity source.

Related