Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Caching with custom authorizers lambda] #87

Closed
truongleeuet opened this issue Feb 24, 2021 · 2 comments
Closed

[Caching with custom authorizers lambda] #87

truongleeuet opened this issue Feb 24, 2021 · 2 comments

Comments

@truongleeuet
Copy link

truongleeuet commented Feb 24, 2021

Please help. I have the api and I want to cache API base on roles in requestContext return from custom authorizers(picture below). Thanks
Screen Shot 2021-02-24 at 10 42 34

@DianaIonita
Copy link
Owner

Hi @truongleeuet,

Thanks for your question. I'm not sure you can cache requests based on the result of the authorizer. I haven't encountered anything about how one might do that. Some quick tests also show that API Gateway doesn't accept mapping expressions like method.request.requestContext.authorizer.roles.
However, if your authorization is based on the value of a header, you can cache based on that value. For example, if your app sends a header like Authorization: Bearer <token>, then you can configure caching like this:

mySecureLambda:
  handler: path-to-handler/handler.handle
  events:
    - http:
        path: /the-path
        method: get
        caching:
          enabled: true
          ttlInSeconds: 3600 # caches response of lambda for 1 hour
          cacheKeyParameters:
            - name: request.header.Authorization

That means that each request should be cached based on the value of the Authorization header. If the user's role changes, you also have the option of invalidating the cache when that happens, which would mean sending a request that contains the Cache-Control: max-age=0 header along with any other cache key parameters you have configured, so API Gateway knows which key to invalidate.

Hope this helps.

@DianaIonita
Copy link
Owner

Closing the issue, please reopen if there are any updates.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants