【问题标题】:How to specify content-type header in AWS API Gateway method_response using terraform?如何使用 terraform 在 AWS API Gateway method_response 中指定内容类型标头?
【发布时间】:2022-01-12 15:49:47
【问题描述】:

我想向我的aws_api_gateway_method_response 添加一个内容类型。它应该如下所示:

我怎样才能做到这一点?使用我当前的 terraform 代码:​​

resource "aws_api_gateway_method_response" "response_200" {
  rest_api_id = aws_api_gateway_rest_api.api.id
  resource_id = aws_api_gateway_resource.api_resource.id
  http_method = aws_api_gateway_method.mymethod.http_method
  status_code = "200"
}

看起来像这样:

如果我将此添加到我的 method_response:

response_parameters = {
    "Content-Type" = true
  }

我收到一个错误:

Error: BadRequestException: Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression specified: Content-Type]

在这里指定内容类型的正确方法是什么?

【问题讨论】:

    标签: amazon-web-services terraform aws-api-gateway terraform-provider-aws


    【解决方案1】:

    response_models 参数可以做到这一点,来自docs

    response_models -(可选)用于响应内容类型的 API 模型映射

    resource "aws_api_gateway_method_response" "response_200_kinesis" {
      rest_api_id = aws_api_gateway_rest_api.api_kinesis.id
      resource_id = aws_api_gateway_resource.api_resource_kinesis.id
      http_method = aws_api_gateway_method.post_json_files_kinesis.http_method
      status_code = "200"
      response_models = {
           "application/json" = "Empty"
      }
    }
    

    或者如果您希望在响应中提供标头 Content-Type

    resource "aws_api_gateway_method_response" "response_200_kinesis" {
      rest_api_id = aws_api_gateway_rest_api.api_kinesis.id
      resource_id = aws_api_gateway_resource.api_resource_kinesis.id
      http_method = aws_api_gateway_method.post_json_files_kinesis.http_method
      status_code = "200"
      response_parameters = { "method.response.header.Content-Type" = true }
    }
    

    【讨论】:

    • 我尝试了第一个但我收到一个错误BadRequestException: Invalid model identifier specified: Empty
    • 你使用哪个 terraform 版本 @Jbd ?
    • @Jbd 可能会添加一个aws_api_gateway_integration_response 作为example 和一个aws_api_gateway_integration 作为docs 注释。
    猜你喜欢
    • 2019-02-24
    • 1970-01-01
    • 2016-09-06
    • 2019-07-09
    • 2017-02-17
    • 2018-12-26
    • 2019-07-25
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多