-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.tf
270 lines (223 loc) · 8.18 KB
/
db.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
locals {
db_subnet_ids = [for subnet in aws_subnet.db : subnet.id]
app_db_user_func_binary = "bootstrap"
app_db_user_func_dir = "${path.module}/app_db_user_func"
master_db_user_secret_arn = aws_rds_cluster.db.master_user_secret[0].secret_arn
app_db_triggers = {
index = var.app_db_pw_index
}
}
resource "aws_db_subnet_group" "db" {
name = var.db_tags.Name
subnet_ids = local.db_subnet_ids
tags = var.db_tags
}
resource "aws_rds_cluster" "db" {
cluster_identifier = var.db_tags.Name
engine = "aurora-postgresql"
engine_mode = "provisioned"
engine_version = "15.2"
db_subnet_group_name = aws_db_subnet_group.db.name
database_name = var.db_name
master_username = "dbadmin"
manage_master_user_password = true
storage_encrypted = true
kms_key_id = aws_kms_key.app.arn
port = var.db_port
backup_retention_period = 30
copy_tags_to_snapshot = true
enabled_cloudwatch_logs_exports = ["postgresql"]
vpc_security_group_ids = [aws_security_group.db.id]
# final_snapshot_identifier reflects the date the cluster was created, not the date it was deleted
final_snapshot_identifier = "${var.db_tags.Name}-${formatdate("YYYYMMDD'T'hhmmssZZZ", timestamp())}"
# scale these values appropriately for production workloads
serverlessv2_scaling_configuration {
max_capacity = 4
min_capacity = 0.5
}
tags = var.db_tags
lifecycle {
ignore_changes = [final_snapshot_identifier]
}
}
resource "aws_rds_cluster_instance" "db" {
cluster_identifier = aws_rds_cluster.db.id
engine = aws_rds_cluster.db.engine
engine_version = aws_rds_cluster.db.engine_version
instance_class = "db.serverless"
db_subnet_group_name = aws_rds_cluster.db.db_subnet_group_name
monitoring_interval = 15
monitoring_role_arn = aws_iam_role.db_monitoring.arn
performance_insights_enabled = true
performance_insights_kms_key_id = aws_kms_key.app.arn
performance_insights_retention_period = 31
copy_tags_to_snapshot = true
tags = var.db_tags
depends_on = [aws_iam_role_policy_attachment.db_monitoring]
}
resource "aws_lambda_invocation" "app_db_user_func" {
function_name = aws_lambda_function.app_db_user_func.function_name
input = jsonencode({
admin_secret_arn = local.master_db_user_secret_arn
db_host = aws_rds_cluster.db.endpoint
db_name = aws_rds_cluster.db.database_name
port = aws_rds_cluster.db.port
username = "app"
pw_secret_arn = aws_secretsmanager_secret.app_db_pw.arn
})
depends_on = [aws_rds_cluster_instance.db]
triggers = local.app_db_triggers
}
output "dbname" {
value = aws_rds_cluster.db.database_name
}
data "archive_file" "app_db_user_func" {
type = "zip"
source_file = "${local.app_db_user_func_dir}/${local.app_db_user_func_binary}"
output_path = "${local.app_db_user_func_dir}/bin/app_db_user_func.zip"
output_file_mode = "0755"
}
resource "aws_lambda_function" "app_db_user_func" {
function_name = "${var.app_name}-db-user"
description = "creates the app database user"
filename = data.archive_file.app_db_user_func.output_path
source_code_hash = data.archive_file.app_db_user_func.output_base64sha256
role = aws_iam_role.app_db_user_func.arn
handler = local.app_db_user_func_binary
architectures = ["arm64"]
memory_size = 256
runtime = "provided.al2023"
timeout = 30
environment {
variables = {
AWS_ENABLE_ENDPOINT_DISCOVERY = true
AWS_ENDPOINT_URL_SECRETS_MANAGER = "secretsmanager.us-east-2.amazonaws.com"
}
}
vpc_config {
subnet_ids = local.db_subnet_ids
security_group_ids = [aws_security_group.db.id]
}
tags = var.db_tags
depends_on = [
aws_iam_role_policy_attachment.app_db_user_func_master_secret,
aws_iam_role_policy_attachment.app_db_user_func_secret,
aws_iam_role_policy_attachment.app_db_user_func_vpc_access,
aws_iam_policy.app_db_pw_secret,
aws_vpc_endpoint.sm
]
}
resource "aws_vpc_endpoint" "sm" {
vpc_id = aws_vpc.app.id
service_name = "com.amazonaws.${data.aws_region.current.name}.secretsmanager"
vpc_endpoint_type = "Interface"
subnet_ids = local.web_subnet_ids
security_group_ids = [aws_security_group.endpoints.id]
private_dns_enabled = true
auto_accept = true
}
resource "aws_iam_role" "app_db_user_func" {
name = "${var.app_name}-db-user-func"
assume_role_policy = data.aws_iam_policy_document.func_assume_role_policy.json
tags = var.db_tags
}
data "aws_iam_policy_document" "func_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "app_db_user_func_secret" {
role = aws_iam_role.app_db_user_func.name
policy_arn = aws_iam_policy.app_db_pw_secret.arn
}
resource "aws_iam_role_policy_attachment" "app_db_user_func_master_secret" {
role = aws_iam_role.app_db_user_func.name
policy_arn = aws_iam_policy.master_db_pw_secret.arn
}
resource "aws_iam_role_policy_attachment" "app_db_user_func_vpc_access" {
role = aws_iam_role.app_db_user_func.name
policy_arn = data.aws_iam_policy.func_vpc_access.arn
}
data "aws_iam_policy" "func_vpc_access" {
name = "AWSLambdaVPCAccessExecutionRole"
}
resource "aws_iam_policy" "app_db_pw_secret" {
name = "${var.app_name}-db-user-secret"
description = "Allows retrieval of the app DB user password from Secrets Manager"
policy = data.aws_iam_policy_document.app_db_pw_secret.json
tags = var.db_tags
}
data "aws_iam_policy_document" "app_db_pw_secret" {
statement {
actions = ["secretsmanager:GetSecretValue"]
resources = [aws_secretsmanager_secret.app_db_pw.arn]
}
}
resource "aws_iam_policy" "master_db_pw_secret" {
name = "${var.app_name}-db-master-pw-secret"
description = "Allows retrieval of the DB master password from Secrets Manager"
policy = data.aws_iam_policy_document.master_db_pw_secret.json
tags = var.db_tags
}
data "aws_iam_policy_document" "master_db_pw_secret" {
statement {
actions = ["secretsmanager:GetSecretValue"]
resources = [local.master_db_user_secret_arn]
}
}
resource "aws_iam_role" "db_monitoring" {
name = var.db_tags.Name
assume_role_policy = data.aws_iam_policy_document.db_monitoring_assume_role_policy.json
tags = var.db_tags
}
resource "aws_iam_role_policy_attachment" "db_monitoring" {
role = aws_iam_role.db_monitoring.name
policy_arn = data.aws_iam_policy.rds_enhanced_monitoring.arn
}
data "aws_iam_policy_document" "db_monitoring_assume_role_policy" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["monitoring.rds.amazonaws.com"]
}
}
}
data "aws_iam_policy" "rds_enhanced_monitoring" {
name = "AmazonRDSEnhancedMonitoringRole"
}
resource "aws_secretsmanager_secret" "app_db_pw" {
name = "${var.app_name}-db-pw"
description = "app db password for ${var.app_name}"
# allows overwriting a secret that was scheduled for deletion
force_overwrite_replica_secret = true
tags = var.db_tags
}
resource "aws_secretsmanager_secret_version" "app_db_pw" {
secret_id = aws_secretsmanager_secret.app_db_pw.id
secret_string = random_password.app_db_pw.result
}
resource "random_password" "app_db_pw" {
length = 20
min_lower = 1
min_upper = 1
min_numeric = 1
special = false
keepers = local.app_db_triggers
}
output "rds_cluster_arn" {
value = aws_rds_cluster.db.arn
}
output "db_endpoint" {
value = aws_rds_cluster.db.endpoint
}
output "master_db_user_secret_arn" {
value = local.master_db_user_secret_arn
}
output "app_db_pw_secret_arn" {
value = aws_secretsmanager_secret.app_db_pw.arn
}