在terraform中为aws_api_gateway_integration指定请求模板

Ben*_*ier 10 amazon-web-services aws-lambda terraform aws-api-gateway

AWS_API_GATEWAY_INTEGRATIONTerraform文档中,支持以下参数:

  • rest_api_id
  • RESOURCE_ID
  • http_method
  • 类型
  • URI
  • 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"
}
Run Code Online (Sandbox Code Playgroud)

但我想指定一个映射模板(以及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" => ""
Run Code Online (Sandbox Code Playgroud)

Ben*_*ier 16

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

(您必须使用PARAMS uri,type,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")}"
  }
}
Run Code Online (Sandbox Code Playgroud)

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"
}
Run Code Online (Sandbox Code Playgroud)


phi*_*son 5

如果您想将其内联而不是放在单独的模板中,您可以执行以下操作:

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
}
Run Code Online (Sandbox Code Playgroud)