Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Stage environment (Caching) for APIGateway using CloudFormation

I have setup APIGateway using CloudFormation which has exposed one method as /customers/{customerId}. The method calls dynamodb service rather using any lambda and sends back HTTP 200 with an empty object if mapping is missing or a customer object with HTTP 200.

Now, I want to setup caching for my prod stage. I have an existing api which i created using the AWS APIGateway Web UI and created the prod stage. I want to replicate those properties in CF. Here is what i have in my old api

Cache Settings

Cache Status: AVAILABLE

Cache Capacity: 0.5GB

Cache time-to-live (TTL): 300

Per-key cache invalidation

Require authorization: checked Handle unauthorized requests: Ignore cache control header; Add a warning in response header

Default Method Throttling

Enable Throttling: checked

Rate: 1000

Burst: 200

I tried setting up the first part (cache settings) like this but it didn't result in desired prod stage settings as i was expecting. How can i acheive desired above output using CloudFormation?

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Parameters":{
        "EnvType": {
            "Type": "String",
            "Default": "test",
            "AllowedValues": ["test", "prod"],
            "Description": "Select what stage need to be created"
        }
    },
    "Conditions":{
        "CreateProdStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "prod"]},
        "CreateTestStage" : {"Fn::Equals": [{"Ref":"EnvType"}, "test"]}
    },
    "Resources": {
        "MyAPI": {
            ...
        },
        "MyAPIResource": {
            ...
        },
        "GetMethod":{
            ...
        },
        "ApiDeployment":{
            "Type":"AWS::ApiGateway::Deployment",
            "Properties":{
                "RestApiId":{"Ref":"MyAPI"}
            },
            "DependsOn":["GetMethod"]
        },
        "TestStage" : {
            "Type" : "AWS::ApiGateway::Stage",
            "Condition":"CreateTestStage",
            "Properties" : {
                "DeploymentId" : {"Ref":"ApiDeployment"},
                "Description" : "Test Stage",
                "RestApiId" : {"Ref":"MyAPI"},
                "StageName" : "test"
            }
        },
        "ProdStage" : {
            "Type" : "AWS::ApiGateway::Stage",
            "Condition":"CreateProdStage",
            "Properties" : {
                "DeploymentId" : {"Ref":"ApiDeployment"},
                "Description" : "Prod Stage",
                "RestApiId" : {"Ref":"MyAPI"},
                "StageName" : "prod",
                "MethodSettings":[{
                    "CachingEnabled":true,
                    "HttpMethod":"*",
                    "ResourcePath":"/*",
                    "CacheTtlInSeconds":300,
                    "ThrottlingBurstLimit" : 2000,
                    "ThrottlingRateLimit" : 1000

                }]
            }
        }

    }
}
like image 944
Em Ae Avatar asked Mar 19 '26 22:03

Em Ae


1 Answers

In the properties of each stage you need to set "CacheClusterEnabled": true, i.e.:

"TestStage" : {
        "Type" : "AWS::ApiGateway::Stage",
        "Condition":"CreateTestStage",
        "Properties" : {
            "DeploymentId" : {"Ref":"ApiDeployment"},
            "Description" : "Test Stage",
            "RestApiId" : {"Ref":"MyAPI"},
            "StageName" : "test",
            "CacheClusterEnabled": "true"
        }
    },

Here are the docs: API Gateway Stage (CacheClusterEnabled): http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-stage.html#cfn-apigateway-stage-cacheclusterenabled

API Gateway MethodSetting (note is says caching must be enabled on the stage first): http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html#cfn-apigateway-stage-methodsetting-cachingenabled

like image 187
Justin Kruse Avatar answered Mar 24 '26 23:03

Justin Kruse