forked from umotif-public/terraform-aws-rds-aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.tf
426 lines (333 loc) · 15.1 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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
#####
# Security Group resources
#####
resource "aws_security_group" "main" {
count = var.create_security_group ? 1 : 0
name_prefix = "${var.name_prefix}-sg-"
vpc_id = var.vpc_id
description = var.security_group_description == "" ? "Control traffic to/from RDS Aurora ${var.name_prefix}" : var.security_group_description
tags = merge(var.tags,
{
Name = "${var.name_prefix}-sg"
}
)
revoke_rules_on_delete = true
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "main_egress" {
count = var.create_security_group ? 1 : 0
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.main.*.id)
}
resource "aws_security_group_rule" "main_default_ingress" {
count = var.create_security_group ? length(var.allowed_security_groups) : 0
description = "Ingress allowed from SGs"
type = "ingress"
from_port = var.enable_global_cluster ? aws_rds_cluster.global[0].port : aws_rds_cluster.main[0].port
to_port = var.enable_global_cluster ? aws_rds_cluster.global[0].port : aws_rds_cluster.main[0].port
protocol = "tcp"
source_security_group_id = element(var.allowed_security_groups, count.index)
security_group_id = join("", aws_security_group.main.*.id)
}
resource "aws_security_group_rule" "main_cidr_ingress" {
count = var.create_security_group && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
description = "Ingress allowed from CIDRs"
type = "ingress"
from_port = var.enable_global_cluster ? aws_rds_cluster.global[0].port : aws_rds_cluster.main[0].port
to_port = var.enable_global_cluster ? aws_rds_cluster.global[0].port : aws_rds_cluster.main[0].port
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
security_group_id = join("", aws_security_group.main.*.id)
}
#####
# RDS Aurora Resources
#####
resource "random_password" "master_password" {
length = 12
special = false
}
resource "random_id" "snapshot_identifier" {
keepers = {
id = var.name_prefix
}
byte_length = 4
}
resource "aws_db_subnet_group" "main" {
count = var.db_subnet_group_name == "" ? 1 : 0
name_prefix = "${var.name_prefix}-"
description = "DB Subnet Group For Aurora cluster ${var.name_prefix}"
subnet_ids = var.subnets
tags = merge(
var.tags,
{
Name = var.name_prefix
}
)
}
#####
# Standard RDS cluster
#####
resource "aws_rds_cluster" "main" {
count = var.enable_global_cluster ? 0 : 1
global_cluster_identifier = var.global_cluster_identifier
cluster_identifier = var.name_prefix
replication_source_identifier = var.replication_source_identifier
source_region = var.source_region
engine = var.engine
engine_mode = var.engine_mode
engine_version = var.engine_mode == "serverless" ? null : var.engine_version
enable_http_endpoint = var.enable_http_endpoint
kms_key_id = var.kms_key_id
database_name = var.database_name
master_username = var.username
master_password = var.password == "" ? random_password.master_password.result : var.password
final_snapshot_identifier = "${var.final_snapshot_identifier_prefix}-${var.name_prefix}-${random_id.snapshot_identifier.hex}"
skip_final_snapshot = var.skip_final_snapshot
snapshot_identifier = var.snapshot_identifier
copy_tags_to_snapshot = var.copy_tags_to_snapshot
deletion_protection = var.deletion_protection
backup_retention_period = var.backup_retention_period
preferred_backup_window = var.preferred_backup_window
preferred_maintenance_window = var.preferred_cluster_maintenance_window
allow_major_version_upgrade = var.allow_major_version_upgrade
apply_immediately = var.apply_immediately
port = var.port == "" ? var.engine == "aurora-postgresql" ? "5432" : "3306" : var.port
db_subnet_group_name = var.db_subnet_group_name == "" ? join("", aws_db_subnet_group.main.*.name) : var.db_subnet_group_name
vpc_security_group_ids = compact(concat(aws_security_group.main.*.id, var.vpc_security_group_ids))
storage_encrypted = var.storage_encrypted
db_cluster_parameter_group_name = var.create_parameter_group ? aws_rds_cluster_parameter_group.main[0].id : var.db_cluster_parameter_group_name
iam_database_authentication_enabled = var.iam_database_authentication_enabled
backtrack_window = (var.engine == "aurora-mysql" || var.engine == "aurora") && var.engine_mode != "serverless" ? var.backtrack_window : 0
iam_roles = var.iam_roles
enabled_cloudwatch_logs_exports = [for log in var.enabled_cloudwatch_logs_exports : log.name]
dynamic "restore_to_point_in_time" {
for_each = length(keys(var.restore_to_point_in_time)) == 0 ? [] : [var.restore_to_point_in_time]
content {
source_cluster_identifier = lookup(restore_to_point_in_time.value, "source_cluster_identifier", null)
restore_type = lookup(restore_to_point_in_time.value, "restore_type", null)
use_latest_restorable_time = lookup(restore_to_point_in_time.value, "use_latest_restorable_time", null)
restore_to_time = lookup(restore_to_point_in_time.value, "restore_to_time", null)
}
}
dynamic "scaling_configuration" {
for_each = length(keys(var.scaling_configuration)) == 0 ? [] : [var.scaling_configuration]
content {
auto_pause = lookup(scaling_configuration.value, "auto_pause", null)
max_capacity = lookup(scaling_configuration.value, "max_capacity", null)
min_capacity = lookup(scaling_configuration.value, "min_capacity", null)
seconds_until_auto_pause = lookup(scaling_configuration.value, "seconds_until_auto_pause", null)
timeout_action = lookup(scaling_configuration.value, "timeout_action", null)
}
}
tags = merge(
var.tags,
var.cluster_tags
)
lifecycle {
ignore_changes = [master_username, master_password, snapshot_identifier]
}
depends_on = [aws_cloudwatch_log_group.audit_log_group]
}
#####
# RDS cluster which is part of Global cluster
#####
resource "aws_rds_cluster" "global" {
count = var.enable_global_cluster ? 1 : 0
global_cluster_identifier = var.global_cluster_identifier
cluster_identifier = var.name_prefix
replication_source_identifier = var.replication_source_identifier
source_region = var.source_region
engine = var.engine
engine_mode = var.engine_mode
engine_version = var.engine_mode == "serverless" ? null : var.engine_version
enable_http_endpoint = var.enable_http_endpoint
kms_key_id = var.kms_key_id
database_name = var.database_name
master_username = var.username
master_password = var.password == "" ? random_password.master_password.result : var.password
final_snapshot_identifier = "${var.final_snapshot_identifier_prefix}-${var.name_prefix}-${random_id.snapshot_identifier.hex}"
skip_final_snapshot = var.skip_final_snapshot
snapshot_identifier = var.snapshot_identifier
copy_tags_to_snapshot = var.copy_tags_to_snapshot
deletion_protection = var.deletion_protection
backup_retention_period = var.backup_retention_period
preferred_backup_window = var.preferred_backup_window
preferred_maintenance_window = var.preferred_cluster_maintenance_window
allow_major_version_upgrade = var.allow_major_version_upgrade
apply_immediately = var.apply_immediately
port = var.port == "" ? var.engine == "aurora-postgresql" ? "5432" : "3306" : var.port
db_subnet_group_name = var.db_subnet_group_name == "" ? join("", aws_db_subnet_group.main.*.name) : var.db_subnet_group_name
vpc_security_group_ids = compact(concat(aws_security_group.main.*.id, var.vpc_security_group_ids))
storage_encrypted = var.storage_encrypted
db_cluster_parameter_group_name = var.create_parameter_group ? aws_rds_cluster_parameter_group.main[0].id : var.db_cluster_parameter_group_name
iam_database_authentication_enabled = var.iam_database_authentication_enabled
backtrack_window = (var.engine == "aurora-mysql" || var.engine == "aurora") && var.engine_mode != "serverless" ? var.backtrack_window : 0
iam_roles = var.iam_roles
enabled_cloudwatch_logs_exports = [for log in var.enabled_cloudwatch_logs_exports : log.name]
dynamic "restore_to_point_in_time" {
for_each = length(keys(var.restore_to_point_in_time)) == 0 ? [] : [var.restore_to_point_in_time]
content {
source_cluster_identifier = lookup(restore_to_point_in_time.value, "source_cluster_identifier", null)
restore_type = lookup(restore_to_point_in_time.value, "restore_type", null)
use_latest_restorable_time = lookup(restore_to_point_in_time.value, "use_latest_restorable_time", null)
restore_to_time = lookup(restore_to_point_in_time.value, "restore_to_time", null)
}
}
dynamic "scaling_configuration" {
for_each = length(keys(var.scaling_configuration)) == 0 ? [] : [var.scaling_configuration]
content {
auto_pause = lookup(scaling_configuration.value, "auto_pause", null)
max_capacity = lookup(scaling_configuration.value, "max_capacity", null)
min_capacity = lookup(scaling_configuration.value, "min_capacity", null)
seconds_until_auto_pause = lookup(scaling_configuration.value, "seconds_until_auto_pause", null)
timeout_action = lookup(scaling_configuration.value, "timeout_action", null)
}
}
tags = merge(
var.tags,
var.cluster_tags
)
lifecycle {
ignore_changes = [master_username, master_password, replication_source_identifier, snapshot_identifier]
}
depends_on = [aws_cloudwatch_log_group.audit_log_group]
}
resource "aws_rds_cluster_instance" "main" {
count = var.replica_scale_enabled ? var.replica_scale_min : var.replica_count
identifier = try(var.instances_parameters[count.index].instance_name, "${var.name_prefix}-${count.index + 1}")
cluster_identifier = var.enable_global_cluster ? aws_rds_cluster.global[0].id : aws_rds_cluster.main[0].id
engine = var.engine
engine_version = var.engine_version
instance_class = try(var.instances_parameters[count.index].instance_type, var.instance_type)
promotion_tier = try(var.instances_parameters[count.index].instance_promotion_tier, count.index + 1)
publicly_accessible = var.publicly_accessible
db_subnet_group_name = var.db_subnet_group_name == "" ? join("", aws_db_subnet_group.main.*.name) : var.db_subnet_group_name
db_parameter_group_name = var.create_parameter_group ? aws_db_parameter_group.main[0].id : var.db_parameter_group_name
preferred_maintenance_window = var.preferred_instance_maintenance_window
apply_immediately = var.apply_immediately
monitoring_role_arn = var.create_monitoring_role ? join("", aws_iam_role.rds_enhanced_monitoring.*.arn) : var.monitoring_role_arn
monitoring_interval = var.monitoring_interval
auto_minor_version_upgrade = var.auto_minor_version_upgrade
performance_insights_enabled = var.performance_insights_enabled
performance_insights_kms_key_id = var.performance_insights_kms_key_id
ca_cert_identifier = var.ca_cert_identifier
tags = merge(
var.tags,
var.cluster_instance_tags
)
lifecycle {
ignore_changes = [
engine_version
]
}
}
#####
# Parameter Groups
#####
resource "aws_rds_cluster_parameter_group" "main" {
count = var.create_parameter_group ? 1 : 0
name = "${var.name_prefix}-aurora-rds-cluster-pg"
family = var.engine_parameter_family
dynamic "parameter" {
for_each = var.cluster_parameters
content {
apply_method = parameter.value.apply_method
name = parameter.value.name
value = parameter.value.value
}
}
tags = merge(
var.tags,
{
"Name" = "${var.name_prefix}-aurora-rds-cluster-parameters",
}
)
}
resource "aws_db_parameter_group" "main" {
count = var.create_parameter_group ? 1 : 0
name = "${var.name_prefix}-aurora-rds-pg"
family = var.engine_parameter_family
dynamic "parameter" {
for_each = var.parameters
content {
name = parameter.value.name
value = parameter.value.value
}
}
tags = merge(
var.tags,
{
"Name" = "${var.name_prefix}-aurora-rds-parameters",
}
)
}
#####
# Enhanced monitoring IAM
#####
resource "aws_iam_role" "rds_enhanced_monitoring" {
count = var.create_monitoring_role && var.monitoring_interval > 0 ? 1 : 0
name_prefix = "${var.name_prefix}-rds-"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "monitoring.rds.amazonaws.com"
},
"Effect": "Allow",
"Sid": "AllowAssumeRoleMonitoringRDS"
}
]
}
EOF
permissions_boundary = var.permissions_boundary
tags = var.tags
}
resource "aws_iam_role_policy_attachment" "rds_enhanced_monitoring" {
count = var.create_monitoring_role && var.monitoring_interval > 0 ? 1 : 0
role = join("", aws_iam_role.rds_enhanced_monitoring.*.name)
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}
resource "aws_cloudwatch_log_group" "audit_log_group" {
for_each = { for export in var.enabled_cloudwatch_logs_exports : export.name => export }
name = "/aws/rds/cluster/${var.name_prefix}/${lookup(each.value, "name")}"
retention_in_days = lookup(each.value, "retention_in_days", null)
kms_key_id = lookup(each.value, "kms_key_id", null)
tags = var.tags
}
#####
# RDS Read Replicas Scaling
#####
resource "aws_appautoscaling_target" "read_replica" {
count = var.replica_scale_enabled ? 1 : 0
max_capacity = var.replica_scale_max
min_capacity = var.replica_scale_min
resource_id = var.enable_global_cluster ? "cluster:${aws_rds_cluster.global[0].cluster_identifier}" : "cluster:${aws_rds_cluster.main[0].cluster_identifier}"
scalable_dimension = "rds:cluster:ReadReplicaCount"
service_namespace = "rds"
}
resource "aws_appautoscaling_policy" "read_replica" {
count = var.replica_scale_enabled ? 1 : 0
name = "target-metric"
policy_type = "TargetTrackingScaling"
resource_id = var.enable_global_cluster ? "cluster:${aws_rds_cluster.global[0].cluster_identifier}" : "cluster:${aws_rds_cluster.main[0].cluster_identifier}"
scalable_dimension = "rds:cluster:ReadReplicaCount"
service_namespace = "rds"
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = var.predefined_metric_type
}
scale_in_cooldown = var.replica_scale_in_cooldown
scale_out_cooldown = var.replica_scale_out_cooldown
target_value = var.predefined_metric_type == "RDSReaderAverageCPUUtilization" ? var.replica_scale_cpu : var.replica_scale_connections
}
depends_on = [aws_appautoscaling_target.read_replica]
}