-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_gateway.tf
302 lines (219 loc) · 9.44 KB
/
api_gateway.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
resource "aws_api_gateway_rest_api" "mercury_rest_api" {
name = "${var.environment}-mercury-rest-api"
description = "Mercury Parser REST API"
}
# /parse-html
resource "aws_api_gateway_resource" "mercury_rest_api_parse_html" {
parent_id = aws_api_gateway_rest_api.mercury_rest_api.root_resource_id
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
path_part = "parse-html"
}
resource "aws_api_gateway_method" "mercury_rest_api_parse_html_options" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parse_html.id
http_method = "OPTIONS"
authorization = "NONE"
}
resource "aws_api_gateway_method" "mercury_rest_api_parse_html_post" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parse_html.id
http_method = "POST"
authorization = "NONE"
api_key_required = true
}
# /parser
resource "aws_api_gateway_resource" "mercury_rest_api_parser" {
parent_id = aws_api_gateway_rest_api.mercury_rest_api.root_resource_id
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
path_part = "parser"
}
resource "aws_api_gateway_method" "mercury_rest_api_parser_options" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parser.id
http_method = "OPTIONS"
authorization = "NONE"
}
resource "aws_api_gateway_method" "mercury_rest_api_parser_get" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parser.id
http_method = "GET"
authorization = "NONE"
api_key_required = true
}
// Options method request
resource "aws_api_gateway_integration" "mercury_rest_api_options_integration" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_options.http_method
resource_id = aws_api_gateway_resource.mercury_rest_api_parse_html.id
type = "MOCK"
uri = module.mercury_lambda_function.lambda_function_invoke_arn
depends_on = [
aws_api_gateway_rest_api.mercury_rest_api,
aws_api_gateway_method.mercury_rest_api_parser_options,
aws_api_gateway_resource.mercury_rest_api_parse_html
]
}
resource "aws_api_gateway_method_response" "mercury_rest_api_options_response_200" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parse_html.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_options.http_method
status_code = 200
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = true
"method.response.header.Access-Control-Allow-Methods" = true
"method.response.header.Access-Control-Allow-Origin" = true
}
}
resource "aws_api_gateway_integration_response" "mercury_rest_api_options_integration_response" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parse_html.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_options.http_method
status_code = aws_api_gateway_method_response.mercury_rest_api_options_response_200.status_code
response_parameters = {
"method.response.header.Access-Control-Allow-Headers" = "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent'"
"method.response.header.Access-Control-Allow-Methods" = "'OPTIONS,POST'"
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
depends_on = [
aws_api_gateway_method.mercury_rest_api_parser_options
]
}
// post
resource "aws_api_gateway_integration" "mercury_rest_api_parse_html_post_integration" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
http_method = aws_api_gateway_method.mercury_rest_api_parse_html_post.http_method
resource_id = aws_api_gateway_resource.mercury_rest_api_parse_html.id
type = "AWS_PROXY"
integration_http_method = "POST"
uri = module.mercury_lambda_function.lambda_function_invoke_arn
}
// parser
resource "aws_api_gateway_integration" "mercury_rest_api_parser_get_integration" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_get.http_method
resource_id = aws_api_gateway_resource.mercury_rest_api_parser.id
type = "AWS_PROXY"
integration_http_method = "POST"
uri = module.mercury_lambda_function.lambda_function_invoke_arn
}
resource "aws_api_gateway_integration" "mercury_rest_api_parser_options_integration" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_options.http_method
resource_id = aws_api_gateway_resource.mercury_rest_api_parser.id
type = "MOCK"
uri = module.mercury_lambda_function.lambda_function_invoke_arn
}
resource "aws_api_gateway_method_response" "mercury_rest_api_parser_options_method_response" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_options.http_method
resource_id = aws_api_gateway_resource.mercury_rest_api_parser.id
status_code = 200
}
resource "aws_api_gateway_integration_response" "mercury_rest_api_parser_options_method_integration_response" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
resource_id = aws_api_gateway_resource.mercury_rest_api_parser.id
http_method = aws_api_gateway_method.mercury_rest_api_parser_options.http_method
status_code = aws_api_gateway_method_response.mercury_rest_api_parser_options_method_response.status_code
response_templates = {
"application/json" = <<EOT
#set($origin = $input.params("Origin"))
#if($origin == "") #set($origin = $input.params("origin"))
#end
#if($origin.matches(".+")) #set($context.responseOverride.header.Access-Control-Allow-Origin = $origin)
#end
EOT
}
response_parameters = {
}
}
resource "aws_api_gateway_deployment" "mercury_rest_api_deployment" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
// export TF_VAR_deployed_at=$(date +%s)
# variables = {
# deployed_at = var.deployed_at
# }
#
triggers = {
redeployment = sha1(jsonencode(aws_api_gateway_rest_api.mercury_rest_api.body))
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_api_gateway_stage" "mercury_rest_api_prod_stage" {
rest_api_id = aws_api_gateway_rest_api.mercury_rest_api.id
deployment_id = aws_api_gateway_deployment.mercury_rest_api_deployment.id
stage_name = "Prod"
# access_log_settings {
# destination_arn = module.mercury_rest_api_access_log_group.cloudwatch_log_group_arn
#
# format = jsonencode(
# {
# caller = "$context.identity.caller"
# httpMethod = "$context.httpMethod"
# ip = "$context.identity.sourceIp"
# protocol = "$context.protocol"
# requestId = "$context.requestId"
# requestTime = "$context.requestTime"
# resourcePath = "$context.resourcePath"
# responseLength = "$context.responseLength"
# status = "$context.status"
# user = "$context.identity.user"
# message = "$context.error.message"
# error = "$context.integration.error"
# integrationstatus = "$context.integration.status"
# integrationErrorMessage = "$context.integrationErrorMessage"
# }
# )
# }
}
// API KEY
resource "aws_api_gateway_api_key" "mercury_rest_api_api_key" {
name = "${var.environment} mercury rest api API Key"
}
// Usage plan
resource "aws_api_gateway_usage_plan" "mercury_rest_api_usage_plan" {
name = "${var.environment}-mercury-client"
description = "Mercury API Usage Plan"
api_stages {
api_id = aws_api_gateway_rest_api.mercury_rest_api.id
stage = aws_api_gateway_stage.mercury_rest_api_prod_stage.stage_name
}
quota_settings {
limit = 10000
offset = 0
period = "MONTH"
}
throttle_settings {
burst_limit = 30
rate_limit = 60
}
}
resource "aws_api_gateway_usage_plan_key" "mercury_rest_api_plan_key" {
key_id = aws_api_gateway_api_key.mercury_rest_api_api_key.id
key_type = "API_KEY"
usage_plan_id = aws_api_gateway_usage_plan.mercury_rest_api_usage_plan.id
}
// Mercury API Access Log
module "mercury_rest_api_access_log_group" {
source = "terraform-aws-modules/cloudwatch/aws//modules/log-group"
version = "3.0.0"
name = "mercury_rest_api_access_log_group_${random_id.random_id.hex}"
retention_in_days = 7
}
# resource "aws_api_gateway_domain_name" "mercury_parser_api_domain" {
# domain_name = "parser.reca.st"
# regional_certificate_arn = var.recast_ssl_cert_arn
#
# endpoint_configuration {
# types = ["REGIONAL"]
# }
#
# }
#
# resource "aws_api_gateway_base_path_mapping" "mercury_parser_rest_api_prod_stage_mapping" {
# api_id = aws_api_gateway_rest_api.mercury_rest_api.id
# stage_name = aws_api_gateway_stage.mercury_rest_api_prod_stage.stage_name
# domain_name = aws_api_gateway_domain_name.mercury_parser_api_domain.domain_name
#
# }