forked from cloudposse/terraform-aws-rds-cluster
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
372 lines (326 loc) · 16.3 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
locals {
enabled = module.this.enabled
partition = join("", data.aws_partition.current.*.partition)
cluster_instance_count = local.enabled ? var.cluster_size : 0
is_regional_cluster = var.cluster_type == "regional"
is_serverless = var.engine_mode == "serverless"
ignore_admin_credentials = var.replication_source_identifier != "" || var.snapshot_identifier != null
}
data "aws_partition" "current" {
count = local.enabled ? 1 : 0
}
# TODO: Use cloudposse/security-group module
resource "aws_security_group" "default" {
count = local.enabled ? 1 : 0
name = module.this.id
description = "Allow inbound traffic from Security Groups and CIDRs"
vpc_id = var.vpc_id
tags = module.this.tags
}
resource "aws_security_group_rule" "ingress_security_groups" {
count = local.enabled ? length(var.security_groups) : 0
description = "Allow inbound traffic from existing security groups"
type = "ingress"
from_port = var.db_port
to_port = var.db_port
protocol = "tcp"
source_security_group_id = var.security_groups[count.index]
security_group_id = join("", aws_security_group.default.*.id)
}
resource "aws_security_group_rule" "ingress_cidr_blocks" {
count = local.enabled && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
description = "Allow inbound traffic from existing CIDR blocks"
type = "ingress"
from_port = var.db_port
to_port = var.db_port
protocol = "tcp"
cidr_blocks = var.allowed_cidr_blocks
security_group_id = join("", aws_security_group.default.*.id)
}
resource "aws_security_group_rule" "egress" {
count = local.enabled && var.egress_enabled ? 1 : 0
description = "Allow outbound traffic"
type = "egress"
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
security_group_id = join("", aws_security_group.default.*.id)
}
# The name "primary" is poorly chosen. We actually mean standalone or regional.
# The primary cluster of a global database is actually created with the "secondary" cluster resource below.
resource "aws_rds_cluster" "primary" {
count = local.enabled && local.is_regional_cluster ? 1 : 0
cluster_identifier = var.cluster_identifier == "" ? module.this.id : var.cluster_identifier
database_name = var.db_name
master_username = local.ignore_admin_credentials ? null : var.admin_user
master_password = local.ignore_admin_credentials ? null : var.admin_password
backup_retention_period = var.retention_period
preferred_backup_window = var.backup_window
copy_tags_to_snapshot = var.copy_tags_to_snapshot
final_snapshot_identifier = var.cluster_identifier == "" ? lower(module.this.id) : lower(var.cluster_identifier)
skip_final_snapshot = var.skip_final_snapshot
apply_immediately = var.apply_immediately
storage_encrypted = local.is_serverless ? null : var.storage_encrypted
kms_key_id = var.kms_key_arn
source_region = var.source_region
snapshot_identifier = var.snapshot_identifier
vpc_security_group_ids = compact(flatten([join("", aws_security_group.default.*.id), var.vpc_security_group_ids]))
preferred_maintenance_window = var.maintenance_window
db_subnet_group_name = join("", aws_db_subnet_group.default.*.name)
db_cluster_parameter_group_name = join("", aws_rds_cluster_parameter_group.default.*.name)
iam_database_authentication_enabled = var.iam_database_authentication_enabled
tags = module.this.tags
engine = var.engine
engine_version = var.engine_version
allow_major_version_upgrade = var.allow_major_version_upgrade
engine_mode = var.engine_mode
iam_roles = var.iam_roles
backtrack_window = var.backtrack_window
enable_http_endpoint = local.is_serverless && var.enable_http_endpoint
depends_on = [
aws_db_subnet_group.default,
aws_rds_cluster_parameter_group.default,
aws_security_group.default,
]
dynamic "s3_import" {
for_each = var.s3_import[*]
content {
bucket_name = lookup(s3_import.value, "bucket_name", null)
bucket_prefix = lookup(s3_import.value, "bucket_prefix", null)
ingestion_role = lookup(s3_import.value, "ingestion_role", null)
source_engine = lookup(s3_import.value, "source_engine", null)
source_engine_version = lookup(s3_import.value, "source_engine_version", null)
}
}
dynamic "scaling_configuration" {
for_each = 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)
}
}
dynamic "timeouts" {
for_each = var.timeouts_configuration
content {
create = lookup(timeouts.value, "create", "120m")
update = lookup(timeouts.value, "update", "120m")
delete = lookup(timeouts.value, "delete", "120m")
}
}
dynamic "restore_to_point_in_time" {
for_each = var.restore_to_point_in_time
content {
source_cluster_identifier = lookup(restore_to_point_in_time.value, "source_cluster_identifier", "120m")
restore_type = lookup(restore_to_point_in_time.value, "restore_type", "copy-on-write")
use_latest_restorable_time = lookup(restore_to_point_in_time.value, "use_latest_restorable_time", true)
}
}
enabled_cloudwatch_logs_exports = var.enabled_cloudwatch_logs_exports
deletion_protection = var.deletion_protection
replication_source_identifier = var.replication_source_identifier
}
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster#replication_source_identifier
resource "aws_rds_cluster" "secondary" {
count = local.enabled && ! local.is_regional_cluster ? 1 : 0
cluster_identifier = var.cluster_identifier == "" ? module.this.id : var.cluster_identifier
database_name = var.db_name
master_username = local.ignore_admin_credentials ? null : var.admin_user
master_password = local.ignore_admin_credentials ? null : var.admin_password
backup_retention_period = var.retention_period
preferred_backup_window = var.backup_window
copy_tags_to_snapshot = var.copy_tags_to_snapshot
final_snapshot_identifier = var.cluster_identifier == "" ? lower(module.this.id) : lower(var.cluster_identifier)
skip_final_snapshot = var.skip_final_snapshot
apply_immediately = var.apply_immediately
storage_encrypted = var.storage_encrypted
kms_key_id = var.kms_key_arn
source_region = var.source_region
snapshot_identifier = var.snapshot_identifier
vpc_security_group_ids = compact(flatten([join("", aws_security_group.default.*.id), var.vpc_security_group_ids]))
preferred_maintenance_window = var.maintenance_window
db_subnet_group_name = join("", aws_db_subnet_group.default.*.name)
db_cluster_parameter_group_name = join("", aws_rds_cluster_parameter_group.default.*.name)
iam_database_authentication_enabled = var.iam_database_authentication_enabled
tags = module.this.tags
engine = var.engine
engine_version = var.engine_version
allow_major_version_upgrade = var.allow_major_version_upgrade
engine_mode = var.engine_mode
iam_roles = var.iam_roles
backtrack_window = var.backtrack_window
enable_http_endpoint = local.is_serverless && var.enable_http_endpoint
depends_on = [
aws_db_subnet_group.default,
aws_db_parameter_group.default,
aws_rds_cluster_parameter_group.default,
aws_security_group.default,
]
dynamic "scaling_configuration" {
for_each = 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)
}
}
dynamic "timeouts" {
for_each = var.timeouts_configuration
content {
create = lookup(timeouts.value, "create", "120m")
update = lookup(timeouts.value, "update", "120m")
delete = lookup(timeouts.value, "delete", "120m")
}
}
enabled_cloudwatch_logs_exports = var.enabled_cloudwatch_logs_exports
deletion_protection = var.deletion_protection
global_cluster_identifier = var.global_cluster_identifier
# https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/rds_cluster#replication_source_identifier
# ARN of a source DB cluster or DB instance if this DB cluster is to be created as a Read Replica.
# If DB Cluster is part of a Global Cluster, use the lifecycle configuration block ignore_changes argument
# to prevent Terraform from showing differences for this argument instead of configuring this value.
lifecycle {
ignore_changes = [
replication_source_identifier, # will be set/managed by Global Cluster
snapshot_identifier, # if created from a snapshot, will be non-null at creation, but null afterwards
]
}
}
resource "aws_rds_cluster_instance" "default" {
count = local.cluster_instance_count
identifier = var.cluster_identifier == "" ? "${module.this.id}-${count.index + 1}" : "${var.cluster_identifier}-${count.index + 1}"
cluster_identifier = coalesce(join("", aws_rds_cluster.primary.*.id), join("", aws_rds_cluster.secondary.*.id))
instance_class = var.instance_type
db_subnet_group_name = join("", aws_db_subnet_group.default.*.name)
db_parameter_group_name = join("", aws_db_parameter_group.default.*.name)
publicly_accessible = var.publicly_accessible
tags = module.this.tags
engine = var.engine
engine_version = var.engine_version
auto_minor_version_upgrade = var.auto_minor_version_upgrade
monitoring_interval = var.rds_monitoring_interval
monitoring_role_arn = var.enhanced_monitoring_role_enabled ? join("", aws_iam_role.enhanced_monitoring.*.arn) : var.rds_monitoring_role_arn
performance_insights_enabled = var.performance_insights_enabled
performance_insights_kms_key_id = var.performance_insights_kms_key_id
performance_insights_retention_period = var.performance_insights_retention_period
availability_zone = var.instance_availability_zone
apply_immediately = var.apply_immediately
preferred_maintenance_window = var.maintenance_window
copy_tags_to_snapshot = var.copy_tags_to_snapshot
ca_cert_identifier = var.ca_cert_identifier
dynamic "timeouts" {
for_each = var.timeouts_configuration
content {
create = lookup(timeouts.value, "create", "120m")
update = lookup(timeouts.value, "update", "120m")
delete = lookup(timeouts.value, "delete", "120m")
}
}
depends_on = [
aws_db_subnet_group.default,
aws_db_parameter_group.default,
aws_iam_role.enhanced_monitoring,
aws_rds_cluster.secondary,
aws_rds_cluster_parameter_group.default,
]
lifecycle {
ignore_changes = [engine_version]
}
}
resource "aws_db_subnet_group" "default" {
count = local.enabled ? 1 : 0
name = try(length(var.subnet_group_name), 0) == 0 ? module.this.id : var.subnet_group_name
description = "Allowed subnets for DB cluster instances"
subnet_ids = var.subnets
tags = module.this.tags
}
resource "aws_rds_cluster_parameter_group" "default" {
count = local.enabled ? 1 : 0
name_prefix = "${module.this.id}${module.this.delimiter}"
description = "DB cluster parameter group"
family = var.cluster_family
dynamic "parameter" {
for_each = var.cluster_parameters
content {
apply_method = lookup(parameter.value, "apply_method", null)
name = parameter.value.name
value = parameter.value.value
}
}
tags = module.this.tags
lifecycle {
create_before_destroy = true
}
}
resource "aws_db_parameter_group" "default" {
count = local.enabled ? 1 : 0
name_prefix = "${module.this.id}${module.this.delimiter}"
description = "DB instance parameter group"
family = var.cluster_family
dynamic "parameter" {
for_each = var.instance_parameters
content {
apply_method = lookup(parameter.value, "apply_method", null)
name = parameter.value.name
value = parameter.value.value
}
}
tags = module.this.tags
lifecycle {
create_before_destroy = true
}
}
locals {
cluster_dns_name_default = "master.${module.this.name}"
cluster_dns_name = var.cluster_dns_name != "" ? var.cluster_dns_name : local.cluster_dns_name_default
reader_dns_name_default = "replicas.${module.this.name}"
reader_dns_name = var.reader_dns_name != "" ? var.reader_dns_name : local.reader_dns_name_default
}
module "dns_master" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.12.2"
enabled = local.enabled && length(var.zone_id) > 0
dns_name = local.cluster_dns_name
zone_id = var.zone_id
records = coalescelist(aws_rds_cluster.primary.*.endpoint, aws_rds_cluster.secondary.*.endpoint, [""])
context = module.this.context
}
module "dns_replicas" {
source = "cloudposse/route53-cluster-hostname/aws"
version = "0.12.2"
enabled = local.enabled && length(var.zone_id) > 0 && ! local.is_serverless && local.cluster_instance_count > 0
dns_name = local.reader_dns_name
zone_id = var.zone_id
records = coalescelist(aws_rds_cluster.primary.*.reader_endpoint, aws_rds_cluster.secondary.*.reader_endpoint, [""])
context = module.this.context
}
resource "aws_appautoscaling_target" "replicas" {
count = local.enabled && var.autoscaling_enabled ? 1 : 0
service_namespace = "rds"
scalable_dimension = "rds:cluster:ReadReplicaCount"
resource_id = "cluster:${coalesce(join("", aws_rds_cluster.primary.*.id), join("", aws_rds_cluster.secondary.*.id))}"
min_capacity = var.autoscaling_min_capacity
max_capacity = var.autoscaling_max_capacity
}
resource "aws_appautoscaling_policy" "replicas" {
count = local.enabled && var.autoscaling_enabled ? 1 : 0
name = module.this.id
service_namespace = join("", aws_appautoscaling_target.replicas.*.service_namespace)
scalable_dimension = join("", aws_appautoscaling_target.replicas.*.scalable_dimension)
resource_id = join("", aws_appautoscaling_target.replicas.*.resource_id)
policy_type = var.autoscaling_policy_type
target_tracking_scaling_policy_configuration {
predefined_metric_specification {
predefined_metric_type = var.autoscaling_target_metrics
}
disable_scale_in = false
target_value = var.autoscaling_target_value
scale_in_cooldown = var.autoscaling_scale_in_cooldown
scale_out_cooldown = var.autoscaling_scale_out_cooldown
}
}