如何在Terraform中为AWS API Gateway集成请求添加请求头与映射模板
解决方案
你需要在现有的aws_api_gateway_integration资源中新增request_parameters(配置静态Content-Type请求头)和request_templates(配置请求映射模板)两个配置块,修改后的完整配置如下:
resource "aws_api_gateway_integration" "kinesis_integration" { rest_api_id = aws_api_gateway_rest_api.shippeo_api_kinesis.id resource_id = aws_api_gateway_resource.shippeo_api_resource_kinesis.id http_method = aws_api_gateway_method.post_json_files_kinesis.http_method credentials = aws_iam_role.shippeo_integration_role.arn type = "AWS" uri = "arn:aws:apigateway:eu-central-1:kinesis:action/PutRecord" integration_http_method = "POST" depends_on = [ aws_api_gateway_resource.shippeo_api_resource_kinesis ] # 新增:配置Content-Type请求头映射 request_parameters = { "integration.request.header.Content-Type" = "'application/x-amz-json-1.1'" } # 新增:配置请求映射模板 request_templates = { "application/json" = jsonencode({ "StreamName" = "$input.params('stream-name')" "Data" = "$util.base64Encode($input.json('$.Data'))" "PartitionKey" = "$input.path('$.PartitionKey')" }) } }
配置说明
request_parameters中的键固定为integration.request.header.Content-Type,表示要给发往Kinesis的集成请求添加Content-Type头,值外层用单引号包裹表示这是静态固定值,和你截图中控制台配置的映射规则完全对应,符合Kinesis API的要求。request_templates的键对应请求的Content-Type,这里匹配客户端发送的application/json类型请求,值为你需要的映射模板内容,用jsonencode包裹可以避免手动转义JSON特殊字符。- 你提到的
selection_pattern和response_templates是集成响应侧的配置参数,用于自定义API Gateway返回给客户端的响应格式,本次你需要配置的是集成请求规则,因此这两个参数不需要使用。
注意:配置修改后需要重新发布API Gateway的部署阶段,新增的配置才会生效。
内容的提问来源于stack exchange,提问作者x89
相关产品推荐
相关产品推荐

