【问题标题】:Setting default value for the field status_code in aws_api_gateway_gateway_response为 aws_api_gateway_gateway_response 中的字段 status_code 设置默认值
【发布时间】:2022-04-01 02:03:58
【问题描述】:

我正在尝试修改 api 网关错误响应的模板。以下是 AWS API 网关中可能出现的错误情况,

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

这是我的资源,

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}]}"
  }
}

在这里我只想更改响应模板并保留status_code 原样。 status_code 是资源 aws_api_gateway_gateway_response 的可选字段,但如果您不传递 status_code,它将显示为在 terraform plan 中更改的 status_code。

所以每次你检查terraform plan。如下所示,

# 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
    }

所以我想通过从 API 网关获取它来设置默认的 status_code。所以我尝试如下,

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}]}"
  }
}

这里我尝试设置当前代码status_code = aws_api_gateway_gateway_response.api_gateway_response[count.index].status_code。但它导致了循环错误。

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]

有人可以帮我吗?

【问题讨论】:

    标签: terraform terraform-provider-aws


    【解决方案1】:

    这有点逃避,但您可以制作自己的地图,其中包含可能的错误情况以及默认状态代码。例如,我更改了 terraform 中默认的 4xx 错误。但是,我希望所有其他 400 个响应都保留其原始状态代码和错误响应。我用这样的 map 和 for_each 循环来做到这一点

    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}"
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-21
      • 1970-01-01
      相关资源
      最近更新 更多