-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
401 lines (313 loc) · 12.6 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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#############################################################
# Locals
#############################################################
locals {
lambda_zip_archive_path = "${path.module}/generated/lambda.zip"
// Master user password
master_password_in_ssm_param = var.db_master_password_ssm_param != null ? true : false
master_password_ssm_param_ecnrypted = var.db_master_password_ssm_param_kms_key != null ? true : false
# Replace null with empty string so that the following regexall will work.
db_master_password_ssm_param = var.db_master_password_ssm_param == null ? "" : var.db_master_password_ssm_param
master_password_in_secretsmanager = length(regexall("/aws/reference/secretsmanager/", local.db_master_password_ssm_param)) > 0
// Provisioned user password
user_password_in_ssm_param = var.db_user_password_ssm_param != null ? true : false
user_password_ssm_param_ecnrypted = var.db_user_password_ssm_param_kms_key != null ? true : false
# Replace null with empty string so that the following regexall will work.
db_user_password_ssm_param = var.db_user_password_ssm_param == null ? "" : var.db_user_password_ssm_param
user_password_in_secretsmanager = length(regexall("/aws/reference/secretsmanager/", local.db_user_password_ssm_param)) > 0
}
#############################################################
# Datasources
#############################################################
data "aws_db_instance" "default" {
count = var.enabled ? 1 : 0
db_instance_identifier = var.db_instance_id
}
data "aws_ssm_parameter" "master_password" {
count = var.enabled && local.master_password_in_ssm_param ? 1 : 0
name = var.db_master_password_ssm_param
}
data "aws_secretsmanager_secret" "master_password" {
count = var.enabled && local.master_password_in_secretsmanager ? 1 : 0
name = trimprefix(var.db_master_password_ssm_param, "/aws/reference/secretsmanager/")
}
data "aws_kms_key" "master_password" {
count = var.enabled && local.master_password_in_ssm_param && local.master_password_ssm_param_ecnrypted ? 1 : 0
key_id = var.db_master_password_ssm_param_kms_key
}
data "aws_ssm_parameter" "user_password" {
count = var.enabled && local.user_password_in_ssm_param ? 1 : 0
name = var.db_user_password_ssm_param
}
data "aws_secretsmanager_secret" "user_password" {
count = var.enabled && local.user_password_in_secretsmanager ? 1 : 0
name = trimprefix(var.db_user_password_ssm_param, "/aws/reference/secretsmanager/")
}
data "aws_kms_key" "user_password" {
count = var.enabled && local.user_password_in_ssm_param && local.user_password_ssm_param_ecnrypted ? 1 : 0
key_id = var.db_user_password_ssm_param_kms_key
}
data "aws_kms_key" "lambda" {
count = var.enabled && var.kms_key != null ? 1 : 0
key_id = var.kms_key
}
#############################################################
# Label
#############################################################
module "default_label" {
enabled = var.enabled
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.16.0"
attributes = compact(concat(var.attributes, ["db", "provisioner"]))
delimiter = var.delimiter
name = var.name
namespace = var.namespace
stage = var.stage
tags = var.tags
}
#############################################################
# ZIP Archive
#############################################################
data "archive_file" "default" {
type = "zip"
output_path = local.lambda_zip_archive_path
source_dir = "${path.module}/source-code/"
}
#############################################################
# Lambda Function
#############################################################
resource "aws_lambda_function" "default" {
count = var.enabled ? 1 : 0
depends_on = [
aws_cloudwatch_log_group.lambda
]
function_name = module.default_label.id
description = "Provisions database [${var.db_name}] in RDS Instance [${var.db_instance_id}]"
filename = local.lambda_zip_archive_path
source_code_hash = data.archive_file.default.output_base64sha256
role = join("", aws_iam_role.lambda.*.arn)
handler = "main.lambda_handler"
runtime = "python3.7"
timeout = var.timeout
memory_size = var.memory
kms_key_arn = var.kms_key
vpc_config {
subnet_ids = var.vpc_config.subnet_ids
security_group_ids = compact(concat(var.vpc_config.security_group_ids, [join("", aws_security_group.default.*.id)]))
}
environment {
variables = {
DB_INSTANCE_ID = var.db_instance_id
DB_MASTER_PASSWORD = var.db_master_password
DB_MASTER_PASSWORD_SSM_PARAM = var.db_master_password_ssm_param
PROVISION_DB_NAME = var.db_name
PROVISION_USER = var.db_user
PROVISION_USER_PASSWORD = var.db_user_password
PROVISION_USER_PASSWORD_SSM_PARAM = var.db_user_password_ssm_param
}
}
tags = module.default_label.tags
}
resource "aws_lambda_alias" "default" {
count = var.enabled ? 1 : 0
name = "default"
description = "Use latest version as default"
function_name = join("", aws_lambda_function.default.*.function_name)
function_version = "$LATEST"
}
data "aws_lambda_invocation" "default" {
count = var.enabled && var.invoke ? 1 : 0
depends_on = [
aws_iam_role_policy_attachment.default_permissions,
aws_iam_role_policy_attachment.basic_execution,
aws_iam_role_policy_attachment.vpc_access,
aws_security_group_rule.egress_from_lambda_to_db_instance,
aws_security_group_rule.ingress_to_db_instance_from_lambda,
aws_security_group_rule.egress_blocks,
]
function_name = join("", aws_lambda_function.default.*.function_name)
input = ""
}
#############################################################
# Security Groups
#############################################################
resource "aws_security_group" "default" {
count = var.enabled ? 1 : 0
name = module.default_label.id
description = "Controls access of Lambda DB Provisioner function [${module.default_label.id}] to VPC resources"
vpc_id = var.vpc_config.vpc_id
tags = module.default_label.tags
}
resource "aws_security_group_rule" "egress_from_lambda_to_db_instance" {
count = var.enabled ? 1 : 0
description = "Allow outbound traffic from Lambda to DB Instance"
type = "egress"
from_port = join("", data.aws_db_instance.default.*.port)
to_port = join("", data.aws_db_instance.default.*.port)
protocol = "tcp"
source_security_group_id = var.db_instance_security_group_id
security_group_id = join("", aws_security_group.default.*.id)
}
resource "aws_security_group_rule" "ingress_to_db_instance_from_lambda" {
count = var.enabled ? 1 : 0
description = "Allow inbound traffic to DB Instance from Lambda"
type = "ingress"
from_port = join("", data.aws_db_instance.default.*.port)
to_port = join("", data.aws_db_instance.default.*.port)
protocol = "tcp"
source_security_group_id = join("", aws_security_group.default.*.id)
security_group_id = var.db_instance_security_group_id
}
resource "aws_security_group_rule" "egress_blocks" {
count = (var.enabled ? 1 : 0) * (length(var.allowed_egress_cidr_blocks) > 0 ? 1 : 0)
security_group_id = join("", aws_security_group.default.*.id)
type = "egress"
from_port = 0
to_port = 65535
protocol = -1
cidr_blocks = var.allowed_egress_cidr_blocks
}
#############################################################
# IAM
#############################################################
data "aws_iam_policy_document" "assume" {
count = var.enabled ? 1 : 0
statement {
effect = "Allow"
actions = ["sts:AssumeRole"]
principals {
identifiers = ["lambda.amazonaws.com"]
type = "Service"
}
}
}
data "aws_iam_policy_document" "default_permissions" {
count = var.enabled ? 1 : 0
statement {
effect = "Allow"
actions = [
"rds:DescribeDBInstances",
]
resources = ["*"]
}
}
data "aws_iam_policy_document" "lambda_kms_permissions" {
count = var.enabled && var.kms_key != null ? 1 : 0
statement {
effect = "Allow"
actions = [
"kms:Decrypt",
]
resources = [join("", data.aws_kms_key.lambda.*.arn)]
}
}
data "aws_iam_policy_document" "master_password_ssm_permissions" {
count = var.enabled && local.master_password_in_ssm_param ? 1 : 0
statement {
effect = "Allow"
actions = [
"ssm:GetParameter",
]
resources = [join("", data.aws_ssm_parameter.master_password.*.arn)]
}
}
data "aws_iam_policy_document" "master_password_secretsmanager_permissions" {
count = var.enabled && local.master_password_in_secretsmanager ? 1 : 0
statement {
effect = "Allow"
actions = [
"secretsmanager:GetSecretValue",
]
resources = [join("", data.aws_secretsmanager_secret.master_password.*.arn)]
}
}
data "aws_iam_policy_document" "master_password_kms_permissions" {
count = var.enabled && local.master_password_in_ssm_param && local.master_password_ssm_param_ecnrypted ? 1 : 0
statement {
effect = "Allow"
actions = [
"kms:Decrypt",
]
resources = [join("", data.aws_kms_key.master_password.*.arn)]
}
}
data "aws_iam_policy_document" "user_password_ssm_permissions" {
count = var.enabled && local.user_password_in_ssm_param ? 1 : 0
statement {
effect = "Allow"
actions = [
"ssm:GetParameter",
]
resources = [join("", data.aws_ssm_parameter.user_password.*.arn)]
}
}
data "aws_iam_policy_document" "user_password_secretsmanager_permissions" {
count = var.enabled && local.user_password_in_secretsmanager ? 1 : 0
statement {
effect = "Allow"
actions = [
"secretsmanager:GetSecretValue",
]
resources = [join("", data.aws_secretsmanager_secret.user_password.*.arn)]
}
}
data "aws_iam_policy_document" "user_password_kms_permissions" {
count = var.enabled && local.user_password_in_ssm_param && local.user_password_ssm_param_ecnrypted ? 1 : 0
statement {
effect = "Allow"
actions = [
"kms:Decrypt",
]
resources = [join("", data.aws_kms_key.user_password.*.arn)]
}
}
module "aggregated_policy" {
source = "git::https://github.com/cloudposse/terraform-aws-iam-policy-document-aggregator.git?ref=tags/0.2.0"
source_documents = compact([
join("", data.aws_iam_policy_document.default_permissions.*.json),
join("", data.aws_iam_policy_document.lambda_kms_permissions.*.json),
join("", data.aws_iam_policy_document.master_password_ssm_permissions.*.json),
join("", data.aws_iam_policy_document.master_password_kms_permissions.*.json),
join("", data.aws_iam_policy_document.master_password_secretsmanager_permissions.*.json),
join("", data.aws_iam_policy_document.user_password_ssm_permissions.*.json),
join("", data.aws_iam_policy_document.user_password_kms_permissions.*.json),
join("", data.aws_iam_policy_document.user_password_secretsmanager_permissions.*.json),
])
}
resource "aws_iam_role" "lambda" {
count = var.enabled ? 1 : 0
name = module.default_label.id
assume_role_policy = join("", data.aws_iam_policy_document.assume.*.json)
tags = module.default_label.tags
}
resource "aws_iam_policy" "default" {
count = var.enabled ? 1 : 0
name = module.default_label.id
path = "/"
description = "IAM policy to control access of Lambda function to AWS resources"
policy = module.aggregated_policy.result_document
}
resource "aws_iam_role_policy_attachment" "default_permissions" {
count = var.enabled ? 1 : 0
role = join("", aws_iam_role.lambda.*.name)
policy_arn = join("", aws_iam_policy.default.*.arn)
}
resource "aws_iam_role_policy_attachment" "basic_execution" {
count = var.enabled ? 1 : 0
role = join("", aws_iam_role.lambda.*.name)
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "vpc_access" {
count = var.enabled ? 1 : 0
role = join("", aws_iam_role.lambda.*.name)
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
}
#############################################################
# Cloudwatch
#############################################################
resource "aws_cloudwatch_log_group" "lambda" {
count = var.enabled ? 1 : 0
name = "/aws/lambda/${module.default_label.id}"
retention_in_days = var.logs_retention_days
kms_key_id = var.logs_kms_key_id
tags = module.default_label.tags
}