我正在尝试修改 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,它将显示为status_code正在更改terraform plan
。
所以每次你检查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]
有人可以在这里帮助我吗?
这有点像逃避,但您可以制作自己的地图,其中可能存在错误情况以及默认状态代码。例如,我更改了地形中默认的 4xx 错误。但是,我希望所有其他 400 个响应都保留其原始状态代码和错误响应。我用地图和这样的循环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}"
}
}