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 
}