Search code examples
amazon-web-servicesaws-api-gatewayterraformterraform-template-file

Terraform - response_parameters: should be a map


I have a terraform script for api_gateway which is working fine. I have a lot of templates duplicated. I want to extract all the templates using "data" "template_file".

Working solution:

resource "aws_api_gateway_integration_response" "ApiResponse" {
   //something goes here
     response_parameters  = {
        "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" = "'GET,DELETE,PUT,OPTIONS'"
        "method.response.header.Access-Control-Allow-Origin" = "'*'"
      }
}

After refactoring:

resource "aws_api_gateway_integration_response" "ApiResponse" {
   //something goes here
     response_parameters = "${data.template_file.response_parameters.template}"
}

data "template_file" "response_parameters" {
   template = "${file("${path.module}/response_parameters.tptl")}"
}

response_parameters.tptl:

{
    "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" = "'GET,DELETE,PUT,OPTIONS'"
    "method.response.header.Access-Control-Allow-Origin" = "'*'"
}

Error:

* aws_api_gateway_integration_response.ApiResponse: response_parameters: should be a map

Since the response parameters are common for all my aws_api_gateway_integration_response, i want to have a common template and reuse in all resources.

Why am i getting this error?


Solution

  • It's working with a variable of type map instead of "data" "template_file"

    resource "aws_api_gateway_integration_response" "ApiResponse" {
    //something goes here 
       response_parameters = "${var.integration_response_parameters}"
    }
    
    variable "integration_response_parameters" {
       type = "map"
       default = {
            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 = "'GET,OPTIONS'"
            method.response.header.Access-Control-Allow-Origin = "'*'"
         }
    }