如何在AWS API Gateway的HTTP_PROXY集成中无需request_parameters全量代理流量?
解决方案
你之前遇到的错误是因为仅在集成URI中使用了{proxy+},但未在API Gateway中创建对应的通配符资源,导致路径解析失败。要实现无需手动配置request_parameters的全路径转发,需按以下步骤调整:
- 创建通配符资源({proxy+}):该资源会匹配父路径下的所有子路径,是实现全路径转发的核心。
- 集成URI使用
{proxy}而非{proxy+}:API Gateway会自动将通配符资源捕获到的路径填充到这个变量中。 - 设置
passthrough_behavior = "WHEN_NO_MATCH":确保客户端发送的请求头、请求体等内容完整转发到NLB。 - 移除不必要的
request_parameters和request_templates:HTTP_PROXY模式下默认支持全量转发,手动配置模板反而可能干扰逻辑。
完整Terraform配置示例
# 创建通配符资源,匹配目标父路径下的所有子路径 resource "aws_api_gateway_resource" "rest_api_proxy_resource" { rest_api_id = aws_api_gateway_rest_api.rest_api.id parent_id = aws_api_gateway_resource.rest_api_destination_resource.id # 替换为你的目标父资源ID path_part = "{proxy+}" } # 配置通配符资源的GET方法(如需支持所有HTTP方法,可改用ANY) resource "aws_api_gateway_method" "rest_api_proxy_get_method" { rest_api_id = aws_api_gateway_rest_api.rest_api.id resource_id = aws_api_gateway_resource.rest_api_proxy_resource.id http_method = "GET" authorization = "NONE" } # 配置HTTP_PROXY集成,全量转发流量到NLB resource "aws_api_gateway_integration" "rest_api_proxy_integration" { rest_api_id = aws_api_gateway_rest_api.rest_api.id resource_id = aws_api_gateway_resource.rest_api_proxy_resource.id http_method = aws_api_gateway_method.rest_api_proxy_get_method.http_method integration_http_method = "GET" type = "HTTP_PROXY" uri = "http://${module.nlb.lb_dns_name}/{proxy}" connection_type = "VPC_LINK" connection_id = aws_api_gateway_vpc_link.eb_vpc_link.id passthrough_behavior = "WHEN_NO_MATCH" # 确保请求头、请求体完整转发 depends_on = [ aws_api_gateway_resource.rest_api_proxy_resource, aws_api_gateway_vpc_link.eb_vpc_link ] }
扩展支持所有HTTP方法
如果需要转发POST、PUT、DELETE等所有类型请求,可将方法配置为ANY:
resource "aws_api_gateway_method" "rest_api_proxy_any_method" { rest_api_id = aws_api_gateway_rest_api.rest_api.id resource_id = aws_api_gateway_resource.rest_api_proxy_resource.id http_method = "ANY" authorization = "NONE" } resource "aws_api_gateway_integration" "rest_api_proxy_integration" { # 保留其他配置不变 http_method = aws_api_gateway_method.rest_api_proxy_any_method.http_method integration_http_method = "ANY" # 保留其他配置不变 }
内容的提问来源于stack exchange,提问作者user2650277
相关产品推荐
相关产品推荐

