简体   繁体   English

AWS API 网关集成和 Terraform

[英]AWS API Gateway Integration and Terraform

I'm struggling to translate my API Gateway Integration request into Terraform code, I'm trying to pass a multipart/form-data request to a Lambda for storage.我正在努力将我的 API 网关集成请求转换为 Terraform 代码,我正在尝试将多部分/表单数据请求传递给 Lambda 进行存储。

I've been able to set up the API gateway manually from scratch, but when I try the terraform I recieve an Internal server error and Cloudwatch tells me the gateway has been unable to transform the request.我已经能够从头开始手动设置 API 网关,但是当我尝试 terraform 时,我收到内部服务器错误并且 Cloudwatch 告诉我网关无法转换请求。

Execution failed due to configuration error: Unable to transform request由于配置错误导致执行失败:无法转换请求

The problem seems to be in the Integration request, because if I do the terraform deployment I can get the whole thing to work by changing the Integration Type in the UI to Lambda Proxy, changing it back again and adding re-adding the Mapping Template.问题似乎出在集成请求中,因为如果我执行 terraform 部署,我可以通过将 UI 中的集成类型更改为 Lambda 代理,再次将其更改回来并重新添加映射模板来使整个工作正常进行。

Terraform block for the Integration; Terraform 集成块;

resource "aws_api_gateway_integration" "docuemnt-api-method-integration" {
  rest_api_id = aws_api_gateway_rest_api.document-api.id
  resource_id = aws_api_gateway_resource.document-resource.id
  http_method = aws_api_gateway_method.document-post-method.http_method
  type = "AWS"
  uri = aws_lambda_function.document_function.invoke_arn
  integration_http_method = "POST"
  passthrough_behavior = "WHEN_NO_TEMPLATES"
  request_templates = {
    "multipart/form-data" = file("${path.module}/mapping/mapping_template.json")
  }
}

The Mapping template映射模板

{
  "body":"$input.body",
  "content-type": "$util.escapeJavaScript($input.params().header.get('Content-Type'))"
}

On the AWS console you are NOT able to set the Integration Request's content_handling and it is only an Optional parameter in terraform as well.在 AWS 控制台上,您无法设置集成请求的 content_handling,它也只是 terraform 中的可选参数。 When you are changing the Integration Type in the UI to Lambda Proxy, the integration request's content_handling will be set to CONVERT_TO_TEXT.当您将 UI 中的集成类型更改为 Lambda 代理时,集成请求的 content_handling 将设置为 CONVERT_TO_TEXT。

So you need to add this row to the aws_api_gateway_integration:因此,您需要将此行添加到 aws_api_gateway_integration:

content_handling = "CONVERT_TO_TEXT" 

And normally it will solve your problem.通常它会解决你的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Terraform 无服务器应用程序与 AWS Lambda 和 API 网关未知令牌? - Terraform Serverless Applications with AWS Lambda and API Gateway unknown token? terraform - 从文件定义 aws api 网关请求参数? - terraform - define aws api gateway request parameters from file? Localstack - AWS API 网关使用 Terraform 启用二进制支持 - Localstack - AWS API Gateway enabling binary support using Terraform AWS API 网关缓慢 - AWS API Gateway Slowness AWS API Gateway Lambda 集成(非代理)- 如何发送自定义标头和二进制数据 - AWS API Gateway Lambda Integration (NOT Proxy) - How to Send Custom Headers and Binary Data AWS Api Gateway Lambda代理集成,如何从客户端请求中获取源端口 - AWS Api Gateway Lambda proxy integration, how to get source port from client request 如何使用 API 网关集成访问 Node js AWS Lambda 中的 POST 参数? - How do I access a POST parameter in Node js AWS Lambda with API Gateway integration? AWS API 网关:如何删除/替换 HTTP 代理直通集成中的查询字符串参数? - AWS API Gateway: How to remove/replace query string parameter in HTTP Proxy Passthrough integration? API 网关上的 AWS 资源策略 - AWS Resource policy on API Gateway AWS API 网关 Websocket 未知错误 - AWS API Gateway Websocket UnknownError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM