在terraform中为aws_api_gateway_integration指定请求模板

时间:2016-03-29 15:27:38

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

Terraform documentation for AWS_API_GATEWAY_INTEGRATION中,支持以下参数:

  • rest_api_id
  • RESOURCE_ID
  • http_method
  • URI
  • integration_http_method

他们举了这个例子:

public class MyAllergy
{
    public string Allergen { get; set; }
    public string AllergenType { get; set; }
    public string AllergyType { get; set; }
    public string Reactions { get; set; }
    public string TouchedWhen { get; set; }
    public Boolean IsChecked { get; set; }
    public Boolean IsEnabled { get; set; }
    public string ApplicationSourceName { get; set; }
    public string AllergyCategory { get; set; }
    public string ConfidenceLevel { get; set; }
    public PartialDate OnsetDate { get; set; }
    public string Status { get; set; }
    public string CreatedWhen { get; set; }
    public string InformationSource { get; set; }
    public string Text { get; set; }
    public string ConfirmedBy { get; set; }
    public long AllergyGUID { get; set; }
}

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

enter image description here

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

注意:我目前正在做的是 <DataGrid AlternatingRowBackground="#FFCDDAEB" AutoGenerateColumns="False" Background="White" Height="160" HorizontalAlignment="Stretch" Margin="18,355,6,0" Name="dgUnityAllergies" VerticalAlignment="Top" Width="704" CanUserAddRows="false" FontSize="12"> <DataGrid.Columns> <DataGridTemplateColumn> <DataGridTemplateColumn.HeaderTemplate> <DataTemplate> <CheckBox x:Name="all" Click="SelectAllCheckBox_Click" IsChecked="{Binding IsChecked}"/> </DataTemplate> </DataGridTemplateColumn.HeaderTemplate> <DataGridTemplateColumn.CellTemplate> <DataTemplate> <CheckBox IsChecked="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsEnabled="{Binding IsEnabled}"/> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTextColumn CanUserSort="True" Header="Category" Width ="Auto" Binding="{Binding AllergyCategory}" IsReadOnly="True" SortMemberPath="AllergyCategory"/> <DataGridTextColumn CanUserSort="True" Header="Type" Width ="Auto" Binding="{Binding AllergenType}" IsReadOnly="True" SortMemberPath="AllergenType"/> <DataGridTextColumn CanUserSort="True" Header="Allergen" Width ="Auto" Binding="{Binding Allergen}" IsReadOnly="True" SortMemberPath="Allergen"/> <DataGridTextColumn CanUserSort="True" Header="Campus" Width ="Auto" Binding="{Binding ApplicationSourceName}" IsReadOnly="True" SortMemberPath="ApplicationSourceName"/> <DataGridTextColumn CanUserSort="True" Header="Reactions" Width ="*" Binding="{Binding Reactions}" IsReadOnly="True" SortMemberPath="Reactions"/> <DataGridTextColumn CanUserSort="True" Header="Entered On" Width ="Auto" Binding="{Binding TouchedWhen}" IsReadOnly="True" SortMemberPath="TouchedWhen"/> </DataGrid.Columns> </DataGrid> 配置的其余部分(lambda,s3,iam等...),然后手动添加映射模板(如以及lambda的整合类型。

但是每次我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" } 应用其他配置(例如:s3)时,Terraform都会恢复映射模板和集成类型。

&#34;还原&#34;计划看起来像这样:

apply

4 个答案:

答案 0 :(得分:13)

基于this issue,这是一个有效的配置:

(您必须使用参数std::list<std::pair<int, int>>uritypeintegration_http_method

request_templates

api_gateway_body_mapping.template 的内容:

# 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")}"
  }
}

答案 1 :(得分:1)

如果您使用Lambda函数作为端点,则集成类型将为&#34; AWS&#34;。

以下是解释创建Lambda集成的the AWS documentation

以下是a GitHub post,其中显示了如何使用Terraform完成此操作。

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

答案 2 :(得分: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
}

答案 3 :(得分:0)

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

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

您可以在此处详细了解Integration types