Skip to content

Set up cache for only one API method in api gateway stage in CloudFormation

0

I have an API in CloudFormation with around 60 endpoints on 1 stage. i want to add caching to this stage, but i need it only for one method. how can i create a CF resource for this stage that will allow caching for only 1 stage? I have this CF template already,Enter image description here but ut does not create an override for all the methods exept startupdata. do i need to provide CachingEnabled setting for every method or it is possible to disable caching for all of them using "*" and then enbale it for 1 method?

1 Answer
0

Hi there, Aliaksei! 👋

Clarifying the Issue

You’ve mentioned wanting to set up caching in API Gateway for only one specific method in a stage while ensuring caching remains disabled for the rest of the methods. Your current CloudFormation (CF) template applies caching globally, except for one path (/startupdata). Let’s solve this step-by-step.

Key Terms

  • MethodSettings: Used to configure caching, throttling, and logging for individual methods.
  • HttpMethod: Refers to HTTP verbs like GET, POST, or the wildcard * to configure all methods.
  • CachingEnabled: When true, it enables caching for a particular method.

Our Solution (the Recipe)

Here’s a clear recipe to enable caching for one method (/startupdata) while disabling it for the rest:

  1. Disable Caching Globally: Use HttpMethod: "*" and CachingEnabled: false in the MethodSettings block to set the default configuration.
  2. Override for a Specific Path: Add another MethodSettings block for the desired method (/startupdata) and enable caching there.

Here’s how the updated CloudFormation snippet will look:

ApiGatewayStage:
  Type: "AWS::ApiGateway::Stage"
  Properties:
    StageName: !Sub "${ApiStageName}"
    DeploymentId: !GetAtt ApiGatewayCommonDeployment.id
    RestApiId: !Ref ApiGatewayRestApi
    CacheClusterEnabled: true
    CacheClusterSize: "0.5"
    MethodSettings:
      - HttpMethod: "*"           # Apply to all methods
        ResourcePath: "/*"        # Apply to all resources
        CachingEnabled: false     # Disable caching globally
        CacheDataEncrypted: false
        CacheTtlInSeconds: 300

      - HttpMethod: "GET"         # Target specific method
        ResourcePath: "/startupdata" # Specific resource path
        CachingEnabled: true      # Enable caching here
        CacheDataEncrypted: false
        CacheTtlInSeconds: 300

Why This Works

The MethodSettings array allows you to define multiple rules. The first rule disables caching globally using wildcards, and the second rule overrides the setting for the specific GET method on /startupdata. The more specific rule takes precedence.

Closing Thoughts

With this approach, caching remains off by default and gets enabled only for the desired method. Keep an eye on the order of settings to avoid unintended overrides.

Hope this clears things up! Let me know if you need any more help. Happy building! 🚀😊

Cheers! Aaron

answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.