Configuring Method Level Caching for API Gateway for Individual Endpoint with Path Variable is Not Working
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/*
Have now also tried:
ResourcePath: "/content/{language}"
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.
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 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.
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 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.