serverless-application-model icon indicating copy to clipboard operation
serverless-application-model copied to clipboard

Configuring Method Level Caching for API Gateway for Individual Endpoint with Path Variable is Not Working

Open ccheatham opened this issue 8 years ago • 6 comments

I'm trying to use SAM to configure API gateway caching for a specific end point with a path variable. My endpoint is /content. It works perfectly if I leave my endpoint as just /content (without a path variable) but, what I want to do is /content/{language}. The SAM template config for my API GW look like this

MyApi:
    Type: AWS::Serverless::Api
    Properties:
      CacheClusterEnabled: true
      CacheClusterSize: "0.5"
      MethodSettings: [{
        CachingEnabled: true,
        CacheTtlInSeconds: 300,
        ResourcePath: "/content/*",
        HttpMethod: "GET"
      }]
      ...

The goal is to cache the same content for several languages. For whatever reason the wild card is not working...I have tried all of the following:

ResourcePath: "/content/*"
ResourcePath: "/~1content~1*"
ResourcePath: "~1content~1*"

I have used this link as guidance in configuring: https://github.com/awslabs/serverless-application-model/releases

To determine whether this works, I take a look at the API GW after deploying to CF and see that the basic top level setting is correct but the Override for this method radio button is not selected. Again this does acutally work when the endpoint is simply /content (method is overridden etc) just not when I try to configure the ResourcePath as /content/*

ccheatham avatar May 03 '18 21:05 ccheatham

Have now also tried:

ResourcePath: "/content/{language}"

ccheatham avatar May 04 '18 15:05 ccheatham

Looks like this needs to be: ~1content~1{language}/POST. You can obviously swap POST out for any method you need. You may also be able to specify * in place of POST but I haven't confirmed this.

brettstack avatar Jun 29 '18 20:06 brettstack

How to enable api gateway cache ttl using methodSettings ? I am using this in template yaml but it does not work, it is not enabling caching at method level. Any thing missing here. Please help.

  CacheClusterEnabled: true
  CacheClusterSize: "1.6"
  MethodSettings: 
     - ResourcePath: "/*"
       CachingEnabled: true
       CacheTtlInSeconds: 600 
       HttpMethod: "GET"

sjain2015 avatar Nov 21 '18 09:11 sjain2015

@sjain2015 Maybe try setting the resource path specifically for endpoints. Although thats not practical if you have a lot of API endpoints. But for us specifying the endpoint in the resourcepath attribute worked, and as methodsettings is a list you can add more than one endpoint.

revolutionisme avatar Nov 21 '18 15:11 revolutionisme

Thanks, this has worked for me. I wish to enable api gateway caching for all the endpoints. Is it possible to do in some way using sam template ?

sjain2015 avatar Nov 23 '18 09:11 sjain2015

@sjain2015 I can't comment about when (or even how) you can do it with the sam template. Although if you want to do it without manually writing it for each endpoint, I would suggest you to use a templating engine like jinja2. It should ease the manual job, although I would also like to have something like the wildcard to apply to all endpoints. But unless that's done, we are using jinja for it.

revolutionisme avatar Nov 26 '18 09:11 revolutionisme