Setting default value for the field status_code in aws_api_gateway_gateway_response

Viewed 238

I'm trying to modify the template of the api gateway error response. Following are the possible error cases in AWS API gateway,

REQUEST_TOO_LARGE
RESOURCE_NOT_FOUND
AUTHORIZER_CONFIGURATION_ERROR
MISSING_AUTHENTICATION_TOKEN
BAD_REQUEST_BODY
INVALID_SIGNATURE
INVALID_API_KEY
BAD_REQUEST_PARAMETERS
AUTHORIZER_FAILURE
UNAUTHORIZED
INTEGRATION_TIMEOUT
ACCESS_DENIED
DEFAULT_4XX
DEFAULT_5XX
WAF_FILTERED
QUOTA_EXCEEDED
THROTTLED
API_CONFIGURATION_ERROR
UNSUPPORTED_MEDIA_TYPE
INTEGRATION_FAILURE
EXPIRED_TOKEN

Here is my resource,

resource "aws_api_gateway_gateway_response" "api_gateway_response" {
  count         = length(var.api_gateway_response_types)
  rest_api_id   = aws_api_gateway_rest_api.api_gateway.id
  response_type = element(values(var.api_gateway_response_types), count.index) 

  response_templates = {
    "application/json" = "{\"errors\": [{\"errorCode\": \"${element(keys(var.api_gateway_response_types), count.index)}\", \"message\": $context.error.messageString}]}"
  }
}

Here I would like to change only the response template and leave the status_code as it is. status_code is the optional field for the resource aws_api_gateway_gateway_response but if you don't pass the status_code, it will show as status_code being changed in terraform plan.

So every time you check terraform plan. It will show as follows,

# aws_api_gateway_gateway_response.api_gateway_response[0] will be updated in-place
  ~ resource "aws_api_gateway_gateway_response" "api_gateway_response" {
        id                  = "aggr-gohnlccgwh-REQUEST_TOO_LARGE"
        response_parameters = {}
        response_templates  = {
            "application/json" = "{\"errors\": [{\"errorCode\": \"4001\", \"message\": $context.error.messageString}]}"
        }
        response_type       = "REQUEST_TOO_LARGE"
        rest_api_id         = "gohnlccgwh"
      - status_code         = "413" -> null
    }
  # aws_api_gateway_gateway_response.api_gateway_response[1] will be updated in-place
  ~ resource "aws_api_gateway_gateway_response" "api_gateway_response" {
        id                  = "aggr-gohnlccgwh-RESOURCE_NOT_FOUND"
        response_parameters = {}
        response_templates  = {
            "application/json" = "{\"errors\": [{\"errorCode\": \"4002\", \"message\": $context.error.messageString}]}"
        }
        response_type       = "RESOURCE_NOT_FOUND"
        rest_api_id         = "gohnlccgwh"
      - status_code         = "404" -> null
    }

So I want to set the default status_code by fetching it from API gateway. so I tried it as follows,

resource "aws_api_gateway_gateway_response" "api_gateway_response" {
  count         = length(var.api_gateway_response_types)
  rest_api_id   = aws_api_gateway_rest_api.api_gateway.id
  response_type = element(values(var.api_gateway_response_types), count.index)
  status_code = aws_api_gateway_gateway_response.api_gateway_response[count.index].status_code

  response_templates = {
    "application/json" = "{\"errors\": [{\"errorCode\": \"${element(keys(var.api_gateway_response_types), count.index)}\", \"message\": $context.error.messageString}]}"
  }
}

Here I tried to set the current code status_code = aws_api_gateway_gateway_response.api_gateway_response[count.index].status_code. But it resulted in Cycle error.

Error: Cycle: aws_api_gateway_gateway_response.api_gateway_response[16], aws_api_gateway_gateway_response.api_gateway_response[12], aws_api_gateway_gateway_response.api_gateway_response[11], aws_api_gateway_gateway_response.api_gateway_response[7], aws_api_gateway_gateway_response.api_gateway_response[19], aws_api_gateway_gateway_response.api_gateway_response[3], aws_api_gateway_gateway_response.api_gateway_response[9], aws_api_gateway_gateway_response.api_gateway_response[18], aws_api_gateway_gateway_response.api_gateway_response[10], aws_api_gateway_gateway_response.api_gateway_response[13], aws_api_gateway_gateway_response.api_gateway_response[14], aws_api_gateway_gateway_response.api_gateway_response[17], aws_api_gateway_gateway_response.api_gateway_response[0], aws_api_gateway_gateway_response.api_gateway_response[15], aws_api_gateway_gateway_response.api_gateway_response[8], aws_api_gateway_gateway_response.api_gateway_response[1], aws_api_gateway_gateway_response.api_gateway_response[2], aws_api_gateway_gateway_response.api_gateway_response[20], aws_api_gateway_gateway_response.api_gateway_response[4], aws_api_gateway_gateway_response.api_gateway_response[6], aws_api_gateway_gateway_response.api_gateway_response[5]

Can someone help me out here?

1 Answers

This is a little bit of a cop-out, but you can make your own map where it has the possible error case as well as the default status code. For example, I changed the default 4xx error in terraform. However, I wanted all the other 400 responses keep their original status codes and error responses. I did this with a map and for_each loop like this

resource "aws_api_gateway_gateway_response" "400_responses" {
  for_each = {
    ACCESS_DENIED          = 403
    BAD_REQUEST_BODY       = 400
    BAD_REQUEST_PARAMETERS = 400
    EXPIRED_TOKEN          = 403
    INVALID_API_KEY        = 403
    INVALID_SIGNATURE      = 403
    QUOTA_EXCEEDED         = 429
    REQUEST_TOO_LARGE      = 413
    RESOURCE_NOT_FOUND     = 404
    THROTTLED              = 429
    UNAUTHORIZED           = 401
    UNSUPPORTED_MEDIA_TYPE = 415
    WAF_FILTERED           = 403
  }
  response_type = each.key
  status_code   = each.value
  rest_api_id   = aws_api_gateway_rest_api.example_api.id
  response_templates = {
    "application/json" = "{\"message\":$context.error.messageString}"
  }
}
Related