-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
executable file
·379 lines (301 loc) · 9.85 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
locals {
name = coalesce(module.this.name, var.name, "cognito-custom-message-sender")
enabled = module.this.enabled
aws_account_id = try(coalesce(var.aws_account_id, data.aws_caller_identity.current[0].account_id), "")
aws_region_name = try(coalesce(var.aws_region_name, data.aws_region.current[0].name), "")
email_sender_enabled = local.enabled && var.email_sender_enabled
email_sender_policy_path = "./policy.rego"
email_sender_policy_content = var.email_sender_policy_content
sms_sender_enabled = local.enabled && var.sms_sender_enabled
sms_sender_policy_path = "./policy.wasm"
sms_sender_policy_content = var.sms_sender_policy_content
sms_sender_throttle_period_in_minutes = 15
}
data "aws_caller_identity" "current" {
count = local.enabled ? 1 : 0
}
data "aws_region" "current" {
count = local.enabled ? 1 : 0
}
# ============================================================ message-sender ===
module "message_sender_label" {
source = "cloudposse/label/null"
version = "0.25.0"
name = local.name
context = module.this.context
}
# ---------------------------------------------------------------------- kms ---
module "kms_key" {
source = "cloudposse/kms-key/aws"
version = "0.12.2"
alias = "${var.kms_key_alias_prefix}/${module.message_sender_label.name}"
deletion_window_in_days = 7
context = module.message_sender_label.context
}
# ---------------------------------------------------------------------- iam ---
resource "aws_iam_role" "this" {
count = local.enabled ? 1 : 0
name = module.message_sender_label.id
description = ""
assume_role_policy = jsonencode({
Version = "2012-10-17",
Statement = [{
Effect = "Allow"
Principal = { "Service" : "lambda.amazonaws.com" }
Action = ["sts:AssumeRole", "sts:TagSession"]
}]
})
managed_policy_arns = [
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
]
inline_policy {
name = "message-sender-access"
policy = data.aws_iam_policy_document.this[0].json
}
tags = module.message_sender_label.tags
}
data "aws_iam_policy_document" "this" {
count = local.enabled ? 1 : 0
statement {
effect = "Allow"
actions = [
"kms:Decrypt"
]
resources = [
module.kms_key.key_arn,
]
}
dynamic "statement" {
for_each = local.email_sender_enabled ? [true] : []
content {
effect = "Allow"
actions = [
"ses:GetTemplate",
"ses:SendEmail",
"ses:SendTemplatedEmail",
]
resources = [
"*"
]
}
}
dynamic "statement" {
for_each = local.sms_sender_enabled ? [true] : []
content {
effect = "Allow"
actions = [
"sns:Publish"
]
resources = [
"*"
]
}
}
dynamic "statement" {
for_each = local.sms_sender_enabled ? [true] : []
content {
effect = "Allow"
actions = [
"dynamodb:Scan",
"dynamodb:Query",
"dynamodb:*Item*",
]
resources = [
aws_dynamodb_table.sms_history[0].arn,
"${aws_dynamodb_table.sms_history[0].arn}/*"
]
}
}
depends_on = [
module.kms_key
]
}
# =============================================================== sms-sender ===
resource "terraform_data" "sms_msg_sender_policy" {
count = local.enabled && local.sms_sender_enabled ? 1 : 0
input = base64encode(local.sms_sender_policy_content)
lifecycle {
precondition {
condition = startswith(var.sms_sender_policy_content, "package cognito_custom_sender_sms_policy")
error_message = "The email sender policy content must include 'package cognito_custom_sender_sms_policy'."
}
}
}
module "sms_msg_sender_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["sms"]
context = module.message_sender_label.context
}
module "sms_msg_sender_code" {
source = "cruxstack/artifact-packager/docker"
version = "1.3.6"
count = local.sms_sender_enabled ? 1 : 0
artifact_src_path = "/tmp/package.zip"
docker_build_context = abspath("${path.module}/assets/custom-message-sender")
docker_build_target = "package"
docker_build_args = {
SERVICE_OPA_POLICY_ENCODED = terraform_data.sms_msg_sender_policy[0].output
}
context = module.sms_msg_sender_label.context
}
resource "aws_cloudwatch_log_group" "sms_msg_sender" {
count = local.sms_sender_enabled ? 1 : 0
name = "/aws/lambda/${module.sms_msg_sender_label.id}"
retention_in_days = 90
tags = module.sms_msg_sender_label.tags
}
resource "aws_lambda_function" "sms_msg_sender" {
count = local.sms_sender_enabled ? 1 : 0
function_name = module.sms_msg_sender_label.id
filename = module.sms_msg_sender_code[0].artifact_package_path
handler = "index.handler"
runtime = "nodejs18.x"
timeout = 45
role = aws_iam_role.this[0].arn
layers = []
tracing_config {
mode = "Active"
}
environment {
variables = {
LOG_LEVEL = var.service_log_level
KMS_KEY_ID = module.kms_key.key_arn
DDB_TABLE_HISTORY_NAME = aws_dynamodb_table.sms_history[0].name
DDB_TABLE_HISTORY_TTL = 43200
EMAIL_SENDER_ENABLED = local.email_sender_enabled
SMS_SENDER_ENABLED = local.sms_sender_enabled
SMS_SENDER_POLICY_PATH = local.sms_sender_policy_path
SMS_THROTTLE_PERIOD_IN_MINUTES = local.sms_sender_throttle_period_in_minutes
}
}
tags = module.sms_msg_sender_label.tags
depends_on = [
module.sms_msg_sender_code,
aws_cloudwatch_log_group.sms_msg_sender,
]
}
resource "aws_lambda_permission" "sms_msg_sender" {
count = local.sms_sender_enabled ? 1 : 0
statement_id = "allow-cognito-trigger"
function_name = aws_lambda_function.sms_msg_sender[0].function_name
action = "lambda:InvokeFunction"
principal = "cognito-idp.amazonaws.com"
source_arn = "arn:aws:cognito-idp:${local.aws_region_name}:${local.aws_account_id}:userpool/*"
}
# ---------------------------------------------------------------------- ddb ---
module "sms_history_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["sms-history"]
context = module.sms_msg_sender_label.context
}
resource "aws_dynamodb_table" "sms_history" {
count = local.sms_sender_enabled ? 1 : 0
name = module.sms_history_label.id
billing_mode = "PAY_PER_REQUEST"
hash_key = "historyId"
range_key = "sentAtEpoch"
attribute {
name = "historyId"
type = "S"
}
attribute {
name = "sentAtEpoch"
type = "N"
}
attribute {
name = "userId"
type = "S"
}
attribute {
name = "userPhoneNumber"
type = "S"
}
ttl {
attribute_name = "ttl"
enabled = true
}
global_secondary_index {
name = "userSid-index"
hash_key = "userId"
range_key = "sentAtEpoch"
projection_type = "ALL"
}
global_secondary_index {
name = "userPhoneNumber-index"
hash_key = "userPhoneNumber"
range_key = "sentAtEpoch"
projection_type = "ALL"
}
tags = module.sms_history_label.tags
}
# ============================================================= email-sender ===
resource "terraform_data" "email_msg_sender_policy" {
count = local.enabled && local.email_sender_enabled ? 1 : 0
input = base64encode(local.email_sender_policy_content)
lifecycle {
precondition {
condition = startswith(var.email_sender_policy_content, "package cognito_custom_sender_email_policy")
error_message = "The email sender policy content must include 'package cognito_custom_sender_email_policy'."
}
}
}
module "email_msg_sender_label" {
source = "cloudposse/label/null"
version = "0.25.0"
attributes = ["email"]
context = module.message_sender_label.context
}
module "email_msg_sender_code" {
source = "cruxstack/artifact-packager/docker"
version = "1.3.6"
count = local.email_sender_enabled ? 1 : 0
artifact_src_path = "/tmp/package.zip"
docker_build_context = abspath("${path.module}/assets/custom-email-sender")
docker_build_target = "package"
docker_build_args = {
SERVICE_OPA_POLICY_ENCODED = terraform_data.email_msg_sender_policy[0].output
}
context = module.email_msg_sender_label.context
}
resource "aws_cloudwatch_log_group" "email_msg_sender" {
count = local.email_sender_enabled ? 1 : 0
name = "/aws/lambda/${module.email_msg_sender_label.id}"
retention_in_days = 90
tags = module.email_msg_sender_label.tags
}
resource "aws_lambda_function" "email_msg_sender" {
count = local.email_sender_enabled ? 1 : 0
function_name = module.email_msg_sender_label.id
filename = module.email_msg_sender_code[0].artifact_package_path
handler = "bootstrap"
runtime = "provided.al2023"
timeout = 10
role = aws_iam_role.this[0].arn
layers = []
tracing_config {
mode = "Active"
}
environment {
variables = {
LOG_LEVEL = var.service_log_level
KMS_KEY_ID = module.kms_key.key_arn
EMAIL_SENDER_ENABLED = local.email_sender_enabled
EMAIL_SENDER_POLICY_PATH = local.email_sender_policy_path
}
}
tags = module.email_msg_sender_label.tags
depends_on = [
module.email_msg_sender_code,
aws_cloudwatch_log_group.email_msg_sender,
]
}
resource "aws_lambda_permission" "email_msg_sender" {
count = local.email_sender_enabled ? 1 : 0
statement_id = "allow-cognito-trigger"
function_name = aws_lambda_function.email_msg_sender[0].function_name
action = "lambda:InvokeFunction"
principal = "cognito-idp.amazonaws.com"
source_arn = "arn:aws:cognito-idp:${local.aws_region_name}:${local.aws_account_id}:userpool/*"
}