1
votes

I have a serverless web API (API Gateway + Lambda) that I have built in C# and deployed via Visual Studio. This is achieved via a serverless.yml file that auto-creates a CloudFormation template, then that template is applied to create the API stack.

Once my stack is deployed, I have gone into the AWS Console to enable caching on one of the path parameters, but get this error:

!https://ibb.co/B4wmRRj

I'm aware of this post https://forums.aws.amazon.com/thread.jspa?messageID=711315&#711315 which details a similar but different issue where the user can't uncheck caching. My issue is I can't enable it to begin with. I also don't understand the steps provided to resolve the issue within that post. There is mention of using the AWS CLI, but not what commands to use, or what to do exactly. I have also done some reading on how to enable caching through the serverless.yml template itself, or cloud formation, but the examples I find online don't seem to match up in any way to the structure of my serverless file or resulting CF template. (I can provide examples if required). I just want to be able to enable caching on path parameters. I have been able to enable caching globally on the API stage, but that won't help me unless I can get the caching to be sensitive to different path parameters.

serverless.yml

    "GetTableResponse" : {
      "Type" : "AWS::Serverless::Function",
      "Properties": {
        "Handler": "AWSServerlessInSiteDataGw::AWSServerlessInSiteDataGw.Functions::GetTableResponse",
        "Runtime": "dotnetcore2.0",
        "CodeUri": "",
        "MemorySize": 256,
        "Timeout": 30,
        "Role": null,
        "Policies": [ "AWSLambdaBasicExecutionRole","AWSLambdaVPCAccessExecutionRole","AmazonSSMFullAccess"],
        "Events": {
          "PutResource": {
            "Type": "Api",
            "Properties": {
              "Path": "kata/table/get/{tableid}",
              "Method": "GET"
            }
          }
        }
      }
    }
  },

  "Outputs" : {
    "ApiURL" : {
        "Description" : "API endpoint URL for Prod environment",
        "Value" : { "Fn::Sub" : "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/" }
    }
  }
1
Can you please share your snippet of serverless.yml file?. I can give you answer on how to do it via cli but want to see if its possible with serverless.yml. - Imran
Added to original question. the whole template is really just repeats of methods similar to the above, with the single output at the end of the url. - JamesMatson

1 Answers

1
votes

--Update Start--

The reason, you are getting Invalid cache key parameter specified error because you did not explicitly highlighted the path parameters section.

This is because, although the UI somehow extrapolated that there is a path parameter, it has not been explicitly called out in the API Gateway configuration.

I tested with below and was able to replicate the behavior on console. To resolve this, follow my Point 1 section full answer.

functions:
  katatable:
    handler: handler.katatable
    events:
      - http:
          method: get
          path: kata/table/get/{tableid}

enter image description here

--Update End--

Here you go. I still don't have your exact serverless.yml so I created a sample of mine similar to yours and tested it.

serverless.yml

functions:
  katatable:
    handler: handler.katatable
    events:
      - http:
          method: get
          path: kata/table/get/{tableid}
          request:
            parameters:
              paths:
                tableid: true

resources:
  Resources:
    ApiGatewayMethodKataTableGetTableidVarGet:
      Properties:
        Integration:
          CacheKeyParameters:
            - method.request.path.tableid

Above should make tableid path parameter is cached.

Explanation:

Point 1. You have to make sure in your events after your method and path, below section is created otherwise next resources section of CacheKeyParameters will fail. Note - boolean true means path parameter is required. Once you explicitly highlight path parameter, you should be able to enable caching via console as well without resources section.

request:
    parameters:
        paths:
            tableid: true

Point 2. The resources section tells API Gateway to enable caching on tableid path parameter. This is nothing but serverless interpretation of CloudFormation template syntax. How did I get that I have to use ApiGatewayMethodKataTableGetTableidVarGet to make it working?. Just read below guidelines and tip to get the name.

https://serverless.com/framework/docs/providers/aws/guide/resources/

Tip: If you are unsure how a resource is named, that you want to reference from your custom resources, you can issue a serverless package. This will create the CloudFormation template for your service in the .serverless folder (it is named cloudformation-template-update-stack.json). Just open the file and check for the generated resource name.

What does above mean? - First run serverless package without resources section and find .serverless folder in directory and open above mentioned json file. Look for AWS::ApiGateway::Method. you will get exact normalized name(ApiGatewayMethodKataTableGetTableidVarGet) syntax you can use in resources section.

Here are some references I used.

https://medium.com/@dougmoscrop/i-set-up-api-gateway-caching-here-are-some-things-that-surprised-me-7526d954fbe6

https://serverless.com/framework/docs/providers/aws/events/apigateway#request-parameters

PS - If you still need CLI steps to enable it, let me know.