【发布时间】: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