forked from amancevice/terraform-aws-slackbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
379 lines (318 loc) · 11 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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
terraform {
required_version = "~> 1.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 3.29"
}
archive = {
source = "hashicorp/archive"
version = "~> 2.0"
}
}
}
locals {
base_path = var.base_path
aws = {
account_id = data.aws_caller_identity.current.account_id
partition = data.aws_partition.current.partition
region = data.aws_region.current.name
}
events = {
source = var.event_source
bus = {
arn = var.event_bus_arn == null ? "arn:${local.aws.partition}:events:${local.aws.region}:${local.aws.account_id}:event-bus/default" : var.event_bus_arn
}
post = {
rule_name = var.event_post_rule_name
rule_description = var.event_post_rule_description
}
}
http_api = {
execution_arn = var.http_api_execution_arn
id = var.http_api_id
integration_description = var.http_api_integration_description
}
kms_key = {
alias = coalesce(var.kms_key_alias, "alias/${local.secret.name}")
deletion_window_in_days = var.kms_key_deletion_window_in_days
description = var.kms_key_description
enable_key_rotation = var.kms_key_enable_key_rotation
is_enabled = var.kms_key_is_enabled
policy_document = var.kms_key_policy_document
tags = var.kms_key_tags
usage = var.kms_key_usage
}
lambda = {
post = {
description = var.lambda_post_description
function_name = var.lambda_post_function_name
memory_size = var.lambda_post_memory_size
publish = var.lambda_post_publish
runtime = var.lambda_post_runtime
timeout = var.lambda_post_timeout
}
proxy = {
description = var.lambda_proxy_description
function_name = var.lambda_proxy_function_name
memory_size = var.lambda_proxy_memory_size
publish = var.lambda_proxy_publish
runtime = var.lambda_proxy_runtime
timeout = var.lambda_proxy_timeout
}
environment_variables = {
SECRET_ID = aws_secretsmanager_secret.secret.name
EVENTS_BUS_NAME = aws_cloudwatch_event_rule.post.event_bus_name
}
permissions = [
"${local.http_api.execution_arn}/*/*/health",
"${local.http_api.execution_arn}/*/*/install",
"${local.http_api.execution_arn}/*/*/oauth",
"${local.http_api.execution_arn}/*/*/oauth/v2",
"${local.http_api.execution_arn}/*/*/callbacks",
"${local.http_api.execution_arn}/*/*/events",
"${local.http_api.execution_arn}/*/*/slash/*",
]
tags = var.lambda_tags
}
log_group = {
retention_in_days = var.log_group_retention_in_days
tags = var.log_group_tags
}
role = {
description = var.role_description
name = var.role_name
path = var.role_path
tags = var.role_tags
}
secret = {
description = var.secret_description
name = var.secret_name
tags = var.secret_tags
}
}
# AWS
data "aws_caller_identity" "current" {}
data "aws_partition" "current" {}
data "aws_region" "current" {}
# EVENTS
resource "aws_cloudwatch_event_rule" "post" {
event_bus_name = split("/", local.events.bus.arn)[1]
name = local.events.post.rule_name
description = local.events.post.rule_description
event_pattern = jsonencode({
detail-type = ["post"]
source = [local.events.source]
})
}
resource "aws_cloudwatch_event_target" "post" {
arn = aws_lambda_function.post.arn
event_bus_name = aws_cloudwatch_event_rule.post.event_bus_name
input_path = "$.detail"
rule = aws_cloudwatch_event_rule.post.name
target_id = "slack-post"
}
# HTTP API
resource "aws_apigatewayv2_integration" "proxy" {
api_id = local.http_api.id
connection_type = "INTERNET"
description = local.http_api.integration_description
integration_method = "POST"
integration_type = "AWS_PROXY"
integration_uri = aws_lambda_function.proxy.invoke_arn
payload_format_version = "2.0"
timeout_milliseconds = 3000
}
resource "aws_apigatewayv2_route" "post_callbacks" {
api_id = local.http_api.id
route_key = "POST /callbacks"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "post_events" {
api_id = local.http_api.id
route_key = "POST /events"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "get_health" {
api_id = local.http_api.id
route_key = "GET /health"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "get_install" {
api_id = local.http_api.id
route_key = "GET /install"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "head_install" {
api_id = local.http_api.id
route_key = "HEAD /install"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "head_health" {
api_id = local.http_api.id
route_key = "HEAD /health"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "get_oauth" {
api_id = local.http_api.id
route_key = "GET /oauth"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "get_oauth_v2" {
api_id = local.http_api.id
route_key = "GET /oauth/v2"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
resource "aws_apigatewayv2_route" "post_slash_cmd" {
api_id = local.http_api.id
route_key = "POST /slash/{cmd}"
authorization_type = "NONE"
target = "integrations/${aws_apigatewayv2_integration.proxy.id}"
}
# IAM
data "aws_iam_policy_document" "assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "inline" {
statement {
sid = "DecryptKmsKey"
actions = ["kms:Decrypt"]
resources = [aws_kms_key.key.arn]
}
statement {
sid = "GetSecretValue"
actions = ["secretsmanager:GetSecretValue"]
resources = [aws_secretsmanager_secret.secret.arn]
}
statement {
sid = "PublishEvents"
actions = ["events:PutEvents"]
resources = [local.events.bus.arn]
}
statement {
sid = "WriteLambdaLogs"
actions = [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents",
]
resources = ["*"]
}
statement {
sid = "SendTaskStatus"
actions = ["states:SendTask*"]
resources = ["*"]
}
}
resource "aws_iam_role" "role" {
assume_role_policy = data.aws_iam_policy_document.assume_role.json
description = local.role.description
name = local.role.name
path = local.role.path
tags = local.role.tags
}
resource "aws_iam_role_policy" "api" {
name = "api"
role = aws_iam_role.role.id
policy = data.aws_iam_policy_document.inline.json
}
resource "aws_lambda_permission" "invoke_api" {
count = length(local.lambda.permissions)
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.proxy.arn
principal = "apigateway.amazonaws.com"
source_arn = element(local.lambda.permissions, count.index)
statement_id = "AllowAPIGatewayV2-${count.index}"
}
resource "aws_lambda_permission" "invoke_post" {
action = "lambda:InvokeFunction"
function_name = aws_lambda_function.post.function_name
principal = "events.amazonaws.com"
source_arn = aws_cloudwatch_event_rule.post.arn
statement_id = "AllowExecutionFromEventBridge"
}
# LAMBDA FUNCTIONS
data "archive_file" "package" {
source_dir = "${path.module}/src"
output_path = "${path.module}/package.zip"
type = "zip"
}
resource "aws_lambda_function" "post" {
description = local.lambda.post.description
filename = data.archive_file.package.output_path
function_name = local.lambda.post.function_name
handler = "index.post"
kms_key_arn = aws_kms_key.key.arn
memory_size = local.lambda.post.memory_size
publish = local.lambda.post.publish
role = aws_iam_role.role.arn
runtime = local.lambda.post.runtime
source_code_hash = data.archive_file.package.output_base64sha256
tags = local.lambda.tags
timeout = local.lambda.post.timeout
environment {
variables = local.lambda.environment_variables
}
}
resource "aws_lambda_function" "proxy" {
description = local.lambda.proxy.description
filename = data.archive_file.package.output_path
function_name = local.lambda.proxy.function_name
handler = "index.proxy"
kms_key_arn = aws_kms_key.key.arn
memory_size = local.lambda.proxy.memory_size
publish = local.lambda.proxy.publish
role = aws_iam_role.role.arn
runtime = local.lambda.proxy.runtime
source_code_hash = data.archive_file.package.output_base64sha256
tags = local.lambda.tags
timeout = local.lambda.proxy.timeout
environment {
variables = local.lambda.environment_variables
}
}
# LOG GROUPS
resource "aws_cloudwatch_log_group" "post_logs" {
name = "/aws/lambda/${aws_lambda_function.post.function_name}"
retention_in_days = local.log_group.retention_in_days
tags = local.log_group.tags
}
resource "aws_cloudwatch_log_group" "proxy_logs" {
name = "/aws/lambda/${aws_lambda_function.proxy.function_name}"
retention_in_days = local.log_group.retention_in_days
tags = local.log_group.tags
}
# SECRETS
resource "aws_kms_key" "key" {
deletion_window_in_days = local.kms_key.deletion_window_in_days
description = local.kms_key.description
enable_key_rotation = local.kms_key.enable_key_rotation
is_enabled = local.kms_key.is_enabled
key_usage = local.kms_key.usage
policy = local.kms_key.policy_document
tags = local.kms_key.tags
}
resource "aws_kms_alias" "alias" {
name = local.kms_key.alias
target_key_id = aws_kms_key.key.key_id
}
resource "aws_secretsmanager_secret" "secret" {
description = local.secret.description
kms_key_id = aws_kms_key.key.key_id
name = local.secret.name
tags = local.secret.tags
}