forked from Element84/filmdrop-aws-tf-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.tf
More file actions
329 lines (279 loc) · 14.4 KB
/
Copy pathapi.tf
File metadata and controls
329 lines (279 loc) · 14.4 KB
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
locals {
is_private_endpoint = var.api_rest_type == "PRIVATE" ? true : false
}
resource "aws_lambda_function" "stac_server_api" {
filename = local.resolved_api_lambda_zip_filepath
function_name = "${local.name_prefix}-stac-server-api"
description = "stac-server API Lambda"
role = aws_iam_role.stac_api_lambda_role.arn
handler = var.api_lambda.handler
source_code_hash = filebase64sha256(local.resolved_api_lambda_zip_filepath)
runtime = var.api_lambda.runtime
timeout = var.api_lambda.timeout_seconds
memory_size = var.api_lambda.memory_mb
environment {
variables = {
STAC_ID = var.stac_id
STAC_TITLE = var.stac_title
STAC_DESCRIPTION = var.stac_description
LOG_LEVEL = var.log_level
REQUEST_LOGGING_ENABLED = var.request_logging_enabled
STAC_DOCS_URL = var.stac_docs_url
OPENSEARCH_HOST = (
var.opensearch_host != ""
? var.opensearch_host
: local.opensearch_endpoint
)
ENABLE_TRANSACTIONS_EXTENSION = var.enable_transactions_extension
ENABLE_COLLECTIONS_AUTHX = var.enable_collections_authx
ENABLE_INGEST_ACTION_TRUNCATE = var.enable_ingest_action_truncate
STAC_API_ROOTPATH = (
var.stac_api_rootpath != null
? var.stac_api_rootpath
: "/${var.stac_api_stage}"
)
PRE_HOOK = (
var.stac_server_auth_pre_hook_enabled && var.stac_server_pre_hook_lambda_arn == ""
? one(aws_lambda_function.stac_server_api_auth_pre_hook[*].arn)
: var.stac_server_pre_hook_lambda_arn
)
POST_HOOK = var.stac_server_post_hook_lambda_arn
OPENSEARCH_CREDENTIALS_SECRET_ID = var.deploy_stac_server_opensearch_serverless ? "" : aws_secretsmanager_secret.opensearch_stac_user_password_secret.arn
COLLECTION_TO_INDEX_MAPPINGS = var.collection_to_index_mappings
STAC_API_URL = var.stac_api_url
CORS_ORIGIN = var.cors_origin
CORS_CREDENTIALS = var.cors_credentials
CORS_METHODS = var.cors_methods
CORS_HEADERS = var.cors_headers
}
}
dynamic "vpc_config" {
for_each = { for i, j in [var.deploy_stac_server_outside_vpc] : i => j if var.deploy_stac_server_outside_vpc != true }
content {
subnet_ids = var.vpc_subnet_ids
security_group_ids = var.vpc_security_group_ids
}
}
}
resource "aws_security_group" "stac_server_api_gateway_private_vpce" {
count = local.is_private_endpoint ? 1 : 0
name_prefix = "${local.name_prefix}-apigw-vcpe-sg-"
description = "Allows TCP inbound on 443 from VPC private subnet CIDRs"
vpc_id = var.vpc_id
}
resource "aws_vpc_security_group_ingress_rule" "stac_server_api_gateway_private_vcpe" {
for_each = local.is_private_endpoint ? { for s in data.aws_subnet.selected : s.id => s } : {}
security_group_id = aws_security_group.stac_server_api_gateway_private_vpce[0].id
description = "Allow TCP on 443 for subnet ${each.value.id}"
cidr_ipv4 = each.value.cidr_block
ip_protocol = "tcp"
from_port = 443
to_port = 443
}
resource "aws_vpc_endpoint" "stac_server_api_gateway_private" {
count = local.is_private_endpoint ? 1 : 0
service_name = "com.amazonaws.${data.aws_region.current.name}.execute-api"
vpc_id = var.vpc_id
vpc_endpoint_type = "Interface"
ip_address_type = "ipv4"
subnet_ids = data.aws_subnet.selected[*].id
auto_accept = true
private_dns_enabled = false
security_group_ids = concat(
aws_security_group.stac_server_api_gateway_private_vpce[*].id,
coalesce(var.private_api_additional_security_group_ids, [])
)
dns_options {
dns_record_ip_type = "ipv4"
}
}
resource "aws_api_gateway_rest_api" "stac_server_api_gateway" {
name = "${local.name_prefix}-stac-server"
endpoint_configuration {
types = [var.api_rest_type]
vpc_endpoint_ids = local.is_private_endpoint ? aws_vpc_endpoint.stac_server_api_gateway_private[*].id : null
}
}
data "aws_iam_policy_document" "stac_server_api_gateway_private" {
count = local.is_private_endpoint ? 1 : 0
statement {
sid = "DenyApiInvokeForNonVpceTraffic"
effect = "Deny"
actions = ["execute-api:Invoke"]
resources = ["arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.stac_server_api_gateway.id}/*"]
principals {
type = "AWS"
identifiers = ["*"]
}
condition {
variable = "aws:SourceVpce"
test = "StringNotEquals"
values = [aws_vpc_endpoint.stac_server_api_gateway_private[0].id]
}
}
statement {
sid = "AllowApiInvoke"
effect = "Allow"
actions = ["execute-api:Invoke"]
resources = ["arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.stac_server_api_gateway.id}/*"]
principals {
type = "AWS"
identifiers = ["*"]
}
}
}
resource "aws_api_gateway_rest_api_policy" "stac_server_api_gateway_private" {
count = local.is_private_endpoint ? 1 : 0
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
policy = data.aws_iam_policy_document.stac_server_api_gateway_private[0].json
}
resource "aws_api_gateway_method" "stac_server_api_gateway_root_method" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
http_method = "ANY"
authorization = var.api_method_authorization_type
}
resource "aws_api_gateway_integration" "stac_server_api_gateway_root_method_integration" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
http_method = aws_api_gateway_method.stac_server_api_gateway_root_method.http_method
type = "AWS_PROXY"
uri = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/${aws_lambda_function.stac_server_api.arn}/invocations"
integration_http_method = "POST"
}
resource "aws_api_gateway_resource" "stac_server_api_gateway_proxy_resource" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
parent_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
path_part = "{proxy+}"
}
resource "aws_api_gateway_method" "stac_server_api_gateway_proxy_resource_method" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.id
http_method = "ANY"
authorization = var.api_method_authorization_type
}
resource "aws_api_gateway_method" "stac_root_options_method" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
http_method = "OPTIONS"
authorization = var.api_method_authorization_type
}
resource "aws_api_gateway_method_response" "stac_root_options_200" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
http_method = aws_api_gateway_method.stac_root_options_method.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" "stac_root_options_integration" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
http_method = aws_api_gateway_method.stac_root_options_method.http_method
type = "MOCK"
}
resource "aws_api_gateway_integration_response" "stac_root_options_integration_response" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_rest_api.stac_server_api_gateway.root_resource_id
http_method = aws_api_gateway_method.stac_root_options_method.http_method
status_code = aws_api_gateway_method_response.stac_root_options_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'",
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'",
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
}
resource "aws_api_gateway_method" "stac_options_method" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.id
http_method = "OPTIONS"
authorization = var.api_method_authorization_type
}
resource "aws_api_gateway_method_response" "stac_options_200" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.id
http_method = aws_api_gateway_method.stac_options_method.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" "stac_options_integration" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.id
http_method = aws_api_gateway_method.stac_options_method.http_method
type = "MOCK"
}
resource "aws_api_gateway_integration_response" "stac_options_integration_response" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.id
http_method = aws_api_gateway_method.stac_options_method.http_method
status_code = aws_api_gateway_method_response.stac_options_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'",
"method.response.header.Access-Control-Allow-Methods" = "'GET,OPTIONS,POST,PUT'",
"method.response.header.Access-Control-Allow-Origin" = "'*'"
}
}
resource "aws_api_gateway_integration" "stac_server_api_gateway_proxy_resource_method_integration" {
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
resource_id = aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.id
http_method = aws_api_gateway_method.stac_server_api_gateway_proxy_resource_method.http_method
type = "AWS_PROXY"
uri = "arn:aws:apigateway:${data.aws_region.current.name}:lambda:path/2015-03-31/functions/${aws_lambda_function.stac_server_api.arn}/invocations"
integration_http_method = "POST"
}
resource "aws_api_gateway_deployment" "stac_server_api_gateway" {
depends_on = [
aws_api_gateway_integration.stac_server_api_gateway_root_method_integration,
aws_api_gateway_integration.stac_server_api_gateway_proxy_resource_method_integration,
]
rest_api_id = aws_api_gateway_rest_api.stac_server_api_gateway.id
stage_name = var.stac_api_stage
stage_description = var.stac_api_stage_description
lifecycle {
create_before_destroy = true
}
}
resource "aws_cloudwatch_log_group" "stac_server_api_gateway_logs_group" {
name = "/aws/apigateway/${local.name_prefix}-stac-server-${aws_api_gateway_deployment.stac_server_api_gateway.rest_api_id}/${aws_api_gateway_deployment.stac_server_api_gateway.stage_name}"
}
locals {
access_log_format = "{\"requestId\":\"\\$context.requestId\",\"ip\":\"\\$context.identity.sourceIp\",\"caller\":\"\\$context.identity.caller\",\"user\":\"\\$context.identity.user\",\"requestTime\":\"\\$context.requestTime\",\"httpMethod\":\"\\$context.httpMethod\",\"resourcePath\":\"\\$context.resourcePath\",\"status\":\"\\$context.status\",\"protocol\":\"\\$context.protocol\",\"responseLength\":\"\\$context.responseLength\"}"
}
resource "null_resource" "enable_access_logs" {
triggers = {
stage_name = aws_api_gateway_deployment.stac_server_api_gateway.stage_name
rest_api_id = aws_api_gateway_deployment.stac_server_api_gateway.rest_api_id
apigw_access_logs_group = aws_cloudwatch_log_group.stac_server_api_gateway_logs_group.arn
access_log_format = local.access_log_format
}
provisioner "local-exec" {
interpreter = ["bash", "-ec"]
command = <<EOF
export AWS_DEFAULT_REGION=${data.aws_region.current.name}
export AWS_REGION=${data.aws_region.current.name}
echo "Update Access Logging on FilmDrop Stac Server API."
aws apigateway update-stage --rest-api-id ${aws_api_gateway_deployment.stac_server_api_gateway.rest_api_id} --stage-name ${aws_api_gateway_deployment.stac_server_api_gateway.stage_name} --patch-operations "[{\"op\": \"replace\",\"path\": \"/accessLogSettings/destinationArn\",\"value\": \"${aws_cloudwatch_log_group.stac_server_api_gateway_logs_group.arn}\"},{\"op\": \"replace\",\"path\": \"/accessLogSettings/format\",\"value\": \"${local.access_log_format}\"}]"
EOF
}
}
resource "aws_lambda_permission" "stac_server_api_gateway_lambda_permission_root_resource" {
statement_id = "AllowExecutionFromAPIGatewayRootResource"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stac_server_api.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.stac_server_api_gateway.id}/*/*"
}
resource "aws_lambda_permission" "stac_server_api_gateway_lambda_permission_proxy_resource" {
statement_id = "AllowExecutionFromAPIGatewayProxyResource"
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.stac_server_api.arn
principal = "apigateway.amazonaws.com"
source_arn = "arn:aws:execute-api:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.stac_server_api_gateway.id}/*/*${aws_api_gateway_resource.stac_server_api_gateway_proxy_resource.path}"
}