forked from lone-cyprus/terraform-api-gateway-method-module
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
145 lines (128 loc) · 5.06 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/**
* # Terraform API Gateway Method Module
*
* Terraform module for creating a serverless architecture in API Gateway. This module can be used to resource methods attached to your API Gateway resources to call lambda functions with a few variables exposed see [variables.tf](https://github.com/carrot//terraform-api-gateway-method-module/blob/master/variables.tf).
*
* ## Caveats
*
* This module makes a few assumptions for simplicity:
*
* 1. You are resourcing API gateway to be used to access Lambda functions as part of a serverless API.
* 2. Your requests/responses are all in JSON
* 3. You only require one error response template across your entire API Gateway instance.
*
* ## Example Useage
* ```
*
* # Create an API Gateway REST API
*
* resource "aws_api_gateway_rest_api" "MyDemoAPI" {
* name = "MyDemoAPI"
* description = "This is my API for demonstration purposes"
* }
*
* # Create a resource
*
* resource "aws_api_gateway_resource" "users" {
* rest_api_id = "${aws_api_gateway_rest_api.MyDemoAPI.id}"
* parent_id = "${aws_api_gateway_rest_api.MyDemoAPI.root_resource_id}"
* path_part = "users"
* }
*
* # Call the module to attach a method along with its request/response/integration templates
* # This one creates a user.
*
* module "UsersPost" {
* source = "github.com/carrot/terraform-api-gateway-method-module"
* rest_api_id = "${aws_api_gateway_rest_api.<API_NAME>.id}"
* resource_id = "${aws_api_gateway_resource.users.id}"
* http_method = "POST"
* lambda_name = "create_user_lambda_funciton"
* account_id = "1234567890"
* region = "us-east-1"
* integration_request_template = "#set($inputRoot = $input.path('$')){}"
* request_model = "Empty"
* integration_response_template = "#set($inputRoot = $input.path('$')){}"
* response_model = "Empty"
* integration_error_template = "#set ($errorMessageObj = $util.parseJson($input.path('$.errorMessage')) {\"message\" :\"$errorMessageObj.message\"}"
* authorization = "AWS_IAM"
* }
* ```
*
* Need CORS enabled? check out [https://github.com/carrot/terraform-api-gateway-cors-module](https://github.com/carrot/terraform-api-gateway-cors-module)
*
* ## More info
* [Terraform - API Gateway](https://www.terraform.io/docs/providers/aws/r/api_gateway_rest_api.html)
*
*/
resource "aws_api_gateway_method" "ResourceMethod" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${var.http_method}"
authorization = "${var.authorization}"
request_parameters = "${var.request_parameters}"
request_models = {
"application/json" = "${var.request_model}"
}
}
resource "aws_api_gateway_integration" "ResourceMethodIntegration" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.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_name}/invocations"
integration_http_method = "POST"
request_templates = {
"application/json" = "${var.integration_request_template}"
}
}
resource "aws_api_gateway_integration_response" "ResourceMethodIntegration200" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
status_code = "${aws_api_gateway_method_response.ResourceMethod200.status_code}"
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
response_templates = {
"application/json" = "${var.integration_response_template}"
}
depends_on = ["aws_api_gateway_integration.ResourceMethodIntegration"]
}
resource "aws_api_gateway_integration_response" "ResourceMethodIntegration400" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
status_code = "${aws_api_gateway_method_response.ResourceMethod400.status_code}"
response_templates = {
"application/json" = "${var.integration_error_template}"
}
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
depends_on = ["aws_api_gateway_integration.ResourceMethodIntegration"]
}
resource "aws_api_gateway_method_response" "ResourceMethod200" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
status_code = "200"
response_models = {
"application/json" = "${var.response_model}"
}
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = true
}
}
resource "aws_api_gateway_method_response" "ResourceMethod400" {
rest_api_id = "${var.rest_api_id}"
resource_id = "${var.resource_id}"
http_method = "${aws_api_gateway_method.ResourceMethod.http_method}"
status_code = "400"
response_models = {
"application/json" = "Error"
}
response_parameters = {
"method.response.header.Access-Control-Allow-Origin" = true
}
}