This repository was archived by the owner on Apr 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathmain.tf
84 lines (69 loc) · 2.4 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
variable "rest_api_id" {
description = "The ID of the associated REST API"
}
variable "resource_id" {
description = "The API resource ID"
}
variable "method" {
description = "The HTTP method"
default = "GET"
}
variable "path" {
description = "The API resource path"
}
variable "lambda" {
description = "The lambda name to invoke"
}
variable "region" {
description = "The AWS region, e.g., eu-west-1"
}
variable "account_id" {
description = "The AWS account ID"
}
# Example: request for GET /hello
resource "aws_api_gateway_method" "request_method" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${var.method}"
authorization = "NONE"
}
# Example: GET /hello => POST lambda
resource "aws_api_gateway_integration" "request_method_integration" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.request_method.http_method}"
type = "AWS"
uri = "arn:aws:apigateway:${var.region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${var.region}:${var.account_id}:function:${var.lambda}/invocations"
# AWS lambdas can only be invoked with the POST method
integration_http_method = "POST"
}
# lambda => GET response
resource "aws_api_gateway_method_response" "response_method" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_integration.request_method_integration.http_method}"
status_code = "200"
response_models = {
"application/json" = "Empty"
}
}
# Response for: GET /hello
resource "aws_api_gateway_integration_response" "response_method_integration" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method_response.response_method.http_method}"
status_code = "${aws_api_gateway_method_response.response_method.status_code}"
response_templates = {
"application/json" = ""
}
}
resource "aws_lambda_permission" "allow_api_gateway" {
function_name = "${var.lambda}"
statement_id = "AllowExecutionFromApiGateway"
action = "lambda:InvokeFunction"
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${var.region}:${var.account_id}:${var.rest_api_id}/*/${var.method}${var.path}"
}
output "http_method" {
value = "${aws_api_gateway_integration_response.response_method_integration.http_method}"
}