13

AWS_API_GATEWAY_INTEGRATION 的 Terraform 文档中,支持以下参数:

  • rest_api_id
  • resource_id
  • http_method
  • 类型
  • 乌里
  • integration_http_method

他们举了这个例子:

resource "aws_api_gateway_integration" "MyDemoIntegration" {
  rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
  resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
  http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}"
  type = "MOCK"
}

但我想指定一个映射模板(以及 Lambda 集成),就像您可以使用 UI 一样:

在此处输入图像描述

但是,我认为无法使用 Terraform 做到这一点。有可能吗?

注意:我目前正在做的是apply配置其余部分(lambda、s3、iam 等),然后手动添加映射模板(以及 lambda 的集成类型)。

但是每次我terraform apply应用其他一些配置(例如:s3)时,Terraform 都会恢复映射模板和集成类型。

“还原”计划如下所示:

~ aws_api_gateway_integration.post_hit_integration
    request_templates.#:                "1" => "0"
    request_templates.application/json: "{\n  \"body\" : $input.json('$'),\n  \"headers\": {\n    #foreach($param in $input.params().header.keySet())\n    \"$param\": \"$util.escapeJavaScript($input.params().header.get($param))\" #if($foreach.hasNext),#end\n    \n    #end  \n  },\n  \"stage\" : \"$context.stage\"\n}" => ""
    uri:                                "arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:000000000000:function:create_hit/invocations" => ""
4

4 回答 4

23

基于这个问题,这是一个有效的配置:

(您必须使用参数uritype和)integration_http_methodrequest_templates

# API
resource "aws_api_gateway_rest_api" "my_api" {
  name = "my_api"
  description = "My Api for adding pets"
}

# Resource
resource "aws_api_gateway_resource" "pets_resource" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  parent_id = "${aws_api_gateway_rest_api.my_api.root_resource_id}"
  path_part = "pets"
}

# Method
resource "aws_api_gateway_method" "post_pet_method" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id = "${aws_api_gateway_resource.pets_resource.id}"
  http_method = "POST"
  authorization = "NONE"
}

# Integration
resource "aws_api_gateway_integration" "post_pet_integration" {
  rest_api_id = "${aws_api_gateway_rest_api.my_api.id}"
  resource_id = "${aws_api_gateway_resource.pets_resource.id}"
  http_method = "${aws_api_gateway_method.post_pet_method.http_method}"
  uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/${aws_lambda_function.create_pet.arn}/invocations"
  type = "AWS"                           # Documentation not clear
  integration_http_method = "POST"       # Not documented
  request_templates = {                  # Not documented
    "application/json" = "${file("api_gateway_body_mapping.template")}"
  }
}

以及api_gateway_body_mapping.template的内容:

{
  "body" : $input.json('$'),
  "headers": {
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end

    #end  
  },
  "stage" : "$context.stage"
}
于 2016-03-29T17:18:59.037 回答
4

如果你想让它内联而不是在一个单独的模板中,你可以这样做:

request_templates = {                  
  "application/json" =  <<REQUEST_TEMPLATE
  {
  "body" : $input.json('$'),
  "headers": {
    #foreach($param in $input.params().header.keySet())
    "$param": "$util.escapeJavaScript($input.params().header.get($param))" #if($foreach.hasNext),#end

    #end  
  },
  "stage" : "$context.stage"
  }
  REQUEST_TEMPLATE
}
于 2017-01-19T18:04:21.307 回答
3

如果您使用 Lambda 函数作为终端节点,则集成类型将为“AWS”。

这是解释创建 Lambda 集成的 AWS 文档。

这是一篇 GitHub 帖子,展示了如何使用 Terraform 完成此操作。

希望有帮助!如果你有任何疑问,请告诉我们。

于 2016-03-29T17:23:33.393 回答
1

我有类似的问题,我无法添加映射模板,因为我的集成类型是“AWS_PROXY”。我将其更改为“AWS”并且它有效。

如果集成类型是“AWS_PROXY”,Terraform 不会考虑映射模板

您可以在此处了解有关集成类型的更多信息。

于 2019-06-12T04:09:54.953 回答