mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-02-02 18:00:18 -05:00
I lost a few hours figuring out the right way to describe an ARN for an API Gateway resource. Specifically I translated the example poorly since I didn't realize I had to append the path onto the end of the ARN. Adds two links to an Amazon documentation page describing the format for API Gateway ARN's. Adds an additional path component to the ARN example so you can see you need to specify paths.
6.0 KiB
6.0 KiB
layout, page_title, sidebar_current, description
| layout | page_title | sidebar_current | description |
|---|---|---|---|
| aws | AWS: aws_api_gateway_integration | docs-aws-resource-api-gateway-integration | Provides an HTTP Method Integration for an API Gateway Resource. |
aws_api_gateway_integration
Provides an HTTP Method Integration for an API Gateway Resource.
Example Usage
resource "aws_api_gateway_rest_api" "MyDemoAPI" {
name = "MyDemoAPI"
description = "This is my API for demonstration purposes"
}
resource "aws_api_gateway_resource" "MyDemoResource" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
parent_id = "${aws_api_gateway_rest_api.MyDemoAPI.root_resource_id}"
path_part = "mydemoresource"
}
resource "aws_api_gateway_method" "MyDemoMethod" {
rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
resource_id = "${aws_api_gateway_resource.MyDemoResource.id}"
http_method = "GET"
authorization = "NONE"
}
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"
# Transforms the incoming XML request to JSON
request_templates {
"application/xml" = <<EOF
{
"body" : $input.json('$')
}
EOF
}
}
Lambda integration
# Variables
variable "myregion" {}
variable "accountId" {}
# API Gateway
resource "aws_api_gateway_rest_api" "api" {
name = "myapi"
}
resource "aws_api_gateway_method" "method" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${aws_api_gateway_rest_api.api.root_resource_id}"
http_method = "GET"
authorization = "NONE"
}
resource "aws_api_gateway_integration" "integration" {
rest_api_id = "${aws_api_gateway_rest_api.api.id}"
resource_id = "${aws_api_gateway_rest_api.api.root_resource_id}"
http_method = "${aws_api_gateway_method.method.http_method}"
integration_http_method = "POST"
type = "AWS"
uri = "arn:aws:apigateway:${var.myregion}:lambda:path/2015-03-31/functions/${aws_lambda_function.lambda.arn}/invocations"
}
# Lambda
resource "aws_lambda_permission" "apigw_lambda" {
statement_id = "AllowExecutionFromAPIGateway"
action = "lambda:InvokeFunction"
function_name = "${aws_lambda_function.lambda.arn}"
principal = "apigateway.amazonaws.com"
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html
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"
}
resource "aws_lambda_function" "lambda" {
filename = "lambda.zip"
function_name = "mylambda"
role = "${aws_iam_role.role.arn}"
handler = "lambda.lambda_handler"
runtime = "python2.7"
source_code_hash = "${base64sha256(file("lambda.zip"))}"
}
# IAM
resource "aws_iam_role" "role" {
name = "myrole"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
POLICY
}
Argument Reference
The following arguments are supported:
rest_api_id- (Required) The ID of the associated REST API.resource_id- (Required) The API resource ID.http_method- (Required) The HTTP method (GET,POST,PUT,DELETE,HEAD,OPTION,ANY) when calling the associated resource.integration_http_method- (Optional) The integration HTTP method (GET,POST,PUT,DELETE,HEAD,OPTION) specifying how API Gateway will interact with the back end. Required iftypeisAWS,AWS_PROXY,HTTPorHTTP_PROXY. Not all methods are compatible with allAWSintegrations. e.g. Lambda function can only be invoked viaPOST.type- (Required) The integration input's type (HTTP, MOCK, AWS, AWS_PROXY, HTTP_PROXY)uri- (Optional) The input's URI (HTTP, AWS). Required iftypeisHTTPorAWS. 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 formarn:aws:apigateway:{region}:{subdomain.service|service}:{path|action}/{service_api}.region,subdomainandserviceare used to determine the right endpoint. e.g.arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/arn:aws:lambda:eu-west-1:012345678901:function:my-func/invocationscredentials- (Optional) The credentials required for the integration. ForAWSintegrations, 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 stringarn:aws:iam::\*:user/\*.request_templates- (Optional) A map of the integration's request templates.request_parameters- (Optional) A map of request query string parameters and headers that should be passed to the backend responder. For example:request_parameters = { "integration.request.header.X-Some-Other-Header" = "method.request.header.X-Some-Header" }passthrough_behavior- (Optional) The integration passthrough behavior (WHEN_NO_MATCH,WHEN_NO_TEMPLATES,NEVER). Required ifrequest_templatesis used.request_parameters_in_json- Deprecated, userequest_parametersinstead.content_handling- (Optional) Specifies how to handle request payload content type conversions. Supported values areCONVERT_TO_BINARYandCONVERT_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.