github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/website/source/docs/providers/aws/r/api_gateway_integration.html.markdown (about)

     1  ---
     2  layout: "aws"
     3  page_title: "AWS: aws_api_gateway_integration"
     4  sidebar_current: "docs-aws-resource-api-gateway-integration"
     5  description: |-
     6    Provides an HTTP Method Integration for an API Gateway Resource.
     7  ---
     8  
     9  # aws\_api\_gateway\_integration
    10  
    11  Provides an HTTP Method Integration for an API Gateway Resource.
    12  
    13  ## Example Usage
    14  
    15  ```
    16  resource "aws_api_gateway_rest_api" "MyDemoAPI" {
    17    name        = "MyDemoAPI"
    18    description = "This is my API for demonstration purposes"
    19  }
    20  
    21  resource "aws_api_gateway_resource" "MyDemoResource" {
    22    rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
    23    parent_id   = "${aws_api_gateway_rest_api.MyDemoAPI.root_resource_id}"
    24    path_part   = "mydemoresource"
    25  }
    26  
    27  resource "aws_api_gateway_method" "MyDemoMethod" {
    28    rest_api_id   = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
    29    resource_id   = "${aws_api_gateway_resource.MyDemoResource.id}"
    30    http_method   = "GET"
    31    authorization = "NONE"
    32  }
    33  
    34  resource "aws_api_gateway_integration" "MyDemoIntegration" {
    35    rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
    36    resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
    37    http_method = "${aws_api_gateway_method.MyDemoMethod.http_method}"
    38    type        = "MOCK"
    39  
    40    # Transforms the incoming XML request to JSON
    41    request_templates {
    42      "application/xml" = <<EOF
    43  {
    44     "body" : $input.json('$')
    45  }
    46  EOF
    47    }
    48  }
    49  ```
    50  
    51  ## Lambda integration
    52  
    53  ```
    54  # Variables
    55  variable "myregion" {}
    56  variable "accountId" {}
    57  
    58  # API Gateway
    59  resource "aws_api_gateway_rest_api" "api" {
    60    name = "myapi"
    61  }
    62  
    63  resource "aws_api_gateway_method" "method" {
    64    rest_api_id   = "${aws_api_gateway_rest_api.api.id}"
    65    resource_id   = "${aws_api_gateway_rest_api.api.root_resource_id}"
    66    http_method   = "GET"
    67    authorization = "NONE"
    68  }
    69  
    70  resource "aws_api_gateway_integration" "integration" {
    71    rest_api_id             = "${aws_api_gateway_rest_api.api.id}"
    72    resource_id             = "${aws_api_gateway_rest_api.api.root_resource_id}"
    73    http_method             = "${aws_api_gateway_method.method.http_method}"
    74    integration_http_method = "POST"
    75    type                    = "AWS"
    76    uri                     = "arn:aws:apigateway:${var.myregion}:lambda:path/2015-03-31/functions/${aws_lambda_function.lambda.arn}/invocations"
    77  }
    78  
    79  # Lambda
    80  resource "aws_lambda_permission" "apigw_lambda" {
    81    statement_id  = "AllowExecutionFromAPIGateway"
    82    action        = "lambda:InvokeFunction"
    83    function_name = "${aws_lambda_function.lambda.arn}"
    84    principal     = "apigateway.amazonaws.com"
    85  
    86    # More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
    87    source_arn = "arn:aws:execute-api:${var.myregion}:${var.accountId}:${aws_api_gateway_rest_api.api.id}/*/${aws_api_gateway_method.method.http_method}/resourcepath/subresourcepath"
    88  }
    89  
    90  resource "aws_lambda_function" "lambda" {
    91    filename         = "lambda.zip"
    92    function_name    = "mylambda"
    93    role             = "${aws_iam_role.role.arn}"
    94    handler          = "lambda.lambda_handler"
    95    runtime          = "python2.7"
    96    source_code_hash = "${base64sha256(file("lambda.zip"))}"
    97  }
    98  
    99  # IAM
   100  resource "aws_iam_role" "role" {
   101    name = "myrole"
   102  
   103    assume_role_policy = <<POLICY
   104  {
   105    "Version": "2012-10-17",
   106    "Statement": [
   107      {
   108        "Action": "sts:AssumeRole",
   109        "Principal": {
   110          "Service": "lambda.amazonaws.com"
   111        },
   112        "Effect": "Allow",
   113        "Sid": ""
   114      }
   115    ]
   116  }
   117  POLICY
   118  }
   119  ```
   120  
   121  ## Argument Reference
   122  
   123  The following arguments are supported:
   124  
   125  * `rest_api_id` - (Required) The ID of the associated REST API.
   126  * `resource_id` - (Required) The API resource ID.
   127  * `http_method` - (Required) The HTTP method (`GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTION`, `ANY`)
   128    when calling the associated resource.
   129  * `integration_http_method` - (Optional) The integration HTTP method
   130    (`GET`, `POST`, `PUT`, `DELETE`, `HEAD`, `OPTION`) specifying how API Gateway will interact with the back end.
   131    **Required** if `type` is `AWS`, `AWS_PROXY`, `HTTP` or `HTTP_PROXY`.
   132    Not all methods are compatible with all `AWS` integrations.
   133    e.g. Lambda function [can only be invoked](https://github.com/awslabs/aws-apigateway-importer/issues/9#issuecomment-129651005) via `POST`.
   134  * `type` - (Required) The integration input's type (HTTP, MOCK, AWS, AWS_PROXY, HTTP_PROXY)
   135  * `uri` - (Optional) The input's URI (HTTP, AWS). **Required** if `type` is `HTTP` or `AWS`.
   136    For HTTP integrations, the URI must be a fully formed, encoded HTTP(S) URL according to the RFC-3986 specification . For AWS integrations, the URI should be of the form `arn:aws:apigateway:{region}:{subdomain.service|service}:{path|action}/{service_api}`. `region`, `subdomain` and `service` are used to determine the right endpoint.
   137    e.g. `arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:012345678901:function:my-func/invocations`
   138  * `credentials` - (Optional) The credentials required for the integration. For `AWS` integrations, 2 options are available. To specify an IAM Role for Amazon API Gateway to assume, use the role's ARN. To require that the caller's identity be passed through from the request, specify the string `arn:aws:iam::\*:user/\*`.
   139  * `request_templates` - (Optional) A map of the integration's request templates.
   140  * `request_parameters` - (Optional) A map of request query string parameters and headers that should be passed to the backend responder.
   141    For example: `request_parameters = { "integration.request.header.X-Some-Other-Header" = "method.request.header.X-Some-Header" }`
   142  * `passthrough_behavior` - (Optional) The integration passthrough behavior (`WHEN_NO_MATCH`, `WHEN_NO_TEMPLATES`, `NEVER`).  **Required** if `request_templates` is used.
   143  * `request_parameters_in_json` - **Deprecated**, use `request_parameters` instead.
   144  * `content_handling` - (Optional) Specifies how to handle request payload content type conversions. Supported values are `CONVERT_TO_BINARY` and `CONVERT_TO_TEXT`. If this property is not defined, the request payload will be passed through from the method request to integration request without modification, provided that the passthroughBehaviors is configured to support payload pass-through.