9

指定请求模板用于aws_api_gateway_integration在Terraform documentation for AWS_API_GATEWAY_INTEGRATION,以下参数被支持:在terraform

  • 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" 
} 

但我想指定一个映射模板(以及一个lambda整合),你可以与UI:

enter image description here

然而Terraform没有办法做到这一点。有可能吗?

注:什么我目前做的是荷兰国际集团apply的其余配置(拉姆达,S3,IAM等...),然后添加映射模板之后手动(以及拉姆达的整合型) 。

但是,每次我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" => "" 

回答

11

基于this issue,这里是一个可行的配置:

(您必须使用PARAMS uritypeintegration_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")}" 
    } 
} 

And api_gate的内容way_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" 
} 
+0

我一直在到处找这个,非常感谢你。我正要详尽地列举头文件。 – jstlaurent

1

如果您正在使用Lambda函数作为终点,集成类型将是“AWS”。

这里是the AWS documentation,它解释了创建Lambda集成。

这里是a GitHub post,它显示了如何使用Terraform完成这项工作。

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

1

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

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 
}