How to pass parameters into a nested stack in Amazon CloudFormation

amazon-api-gatewayamazon-cloudformationamazon-web-services

I'm using CloudFormation to manage an Amazon API Gateway stack, and trying to (re)use a nested stack to add an OPTIONS method to each of my HTTP endpoint methods so I can respond with CORS headers.

Here's the CloudFormation snippet that refers to the nested stack:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS CloudFormation template for example HTTP endpoint",
  "Resources": {
    "MyRestApi": {
      "Type": "AWS::ApiGateway::RestApi",
      "Properties": {
        "Name": "api.example.com"
      }
    },
    "HelloResource": {
      "Type": "AWS::ApiGateway::Resource",
      "Properties": {
        "RestApiId": { "Ref": "MyRestApi" },
        "ParentId": { "Fn::GetAtt": [ "MyRestApi", "RootResourceId" ] },
        "PathPart": "hello"
      }
    },
    "GETHello": {
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "RestApiId": { "Ref": "MyRestApi" },
        "ResourceId": { "Ref": "HelloResource" },
        "HttpMethod": "GET",
        "AuthorizationType": "NONE",
        "Integration": {
          "Type": "HTTP",
          "IntegrationHttpMethod": "GET",
          "Uri": "https://my-api-server.example.com/hello",
          "IntegrationResponses": [ { "StatusCode": "200" } ],
          "RequestParameters": { "integration.request.header.Authorization": "method.request.header.Authorization" }
        },
        "MethodResponses": [
          {
            "StatusCode": "200",
            "ResponseModels": { "text/html": "Empty" }
          }
        ],
        "RequestParameters": { "method.request.header.Authorization": true }
      }
    },
    "OPTIONSHello": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "Parameters": {
          "RestApiId": "MyRestApi",
          "ResourceId": "HelloResource"
        },
        "TemplateURL": "https://s3-eu-west-1.amazonaws.com/my-cloudformation-bucket/api-gateway-cors-headers.json"
      }
    }
  }
}

and then the CloudFormation template it refers to – api-gateway-cors-headers.json – looks like this:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "AWS CloudFormation template for CORS headers,
  "Parameters": {
    "RestApiId": { "Type": "String" },
    "ResourceId": { "Type": "String" }
  },
  "Resources": {
    "CORSHeader": {
      "Type": "AWS::ApiGateway::Method",
      "Properties": {
        "AuthorizationType": "NONE",
        "RestApiId": { "Ref": "RestApiId" },
        "ResourceId": { "Ref": "ResourceId" },
        "HttpMethod": "OPTIONS",
        "Integration": {
          "IntegrationResponses": [
            {
              "StatusCode": 200,
              "ResponseParameters": {
                "method.response.header.Access-Control-Allow-Headers": "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'",
                "method.response.header.Access-Control-Allow-Methods": "'POST,OPTIONS'",
                "method.response.header.Access-Control-Allow-Origin": "'*'"
              },
              "ResponseTemplates": {
                "application/json": ""
              }
            }
          ],
          "PassthroughBehavior": "WHEN_NO_MATCH",
          "RequestTemplates": { "application/json": "{\"statusCode\": 200}" },
          "Type": "MOCK"
        },
        "MethodResponses": [
          {
            "StatusCode": 200,
            "ResponseModels": {
              "application/json": "Empty"
            },
            "ResponseParameters": {
              "method.response.header.Access-Control-Allow-Headers": false,
              "method.response.header.Access-Control-Allow-Methods": false,
              "method.response.header.Access-Control-Allow-Origin": false
            }
          }
        ]
      }
    }
  }
}

The problem is, I cannot work out how to pass the RestApiId and ResourceId parameters from the parent stack into the nested stack. Depending what syntax I try, I'm getting three or four different error messages – most recently Template format error: Unresolved resource dependencies [RestApiId, ResourceId] in the Resources block of the template – but cannot find any examples of how to pass the REST API ID and Resource ID into the nested stack template. What am I doing wrong?

Best Answer

You are currently passing the Strings "MyRestApi" and "HelloResource" - not the resource references. Pass them instead

"OPTIONSHello": {
      "Type": "AWS::CloudFormation::Stack",
      "Properties": {
        "Parameters": {
          "RestApiId": { "Ref": "MyRestApi" },
          "ResourceId": { "Ref": "HelloResource"}
        },
        "TemplateURL": "https://s3-eu-west-1.amazonaws.com/my-cloudformation-bucket/api-gateway-cors-headers.json"
      }
    }