forked from deliveryhero/tf-aws-rds-aurora
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
189 lines (169 loc) · 7.76 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
locals {
port = var.port == "" ? var.engine == "aurora-postgresql" ? "5432" : "3306" : var.port
master_password = var.password == "" ? random_id.master_password.b64_url : var.password
create_enhanced_monitoring = var.create_resources && var.monitoring_interval > 0 ? true : false
cluster_instance_count = var.create_resources ? var.replica_autoscaling ? var.replica_scale_min : var.replica_count : 0
}
# Random string to use as master password unless one is specified
resource "random_id" "master_password" {
byte_length = 10
}
resource "aws_db_subnet_group" "main" {
count = var.create_resources ? 1 : 0
name = var.name
description = "For Aurora cluster ${var.name}"
subnet_ids = var.subnet_ids
tags = merge(
var.tags,
{
"Name" = "aurora-${var.name}"
},
)
}
resource "aws_rds_cluster" "main" {
count = var.create_resources ? 1 : 0
cluster_identifier = "${var.identifier_prefix}${var.name}"
engine = var.engine
engine_version = var.engine_version
kms_key_id = var.kms_key_id
master_username = var.username
master_password = local.master_password
deletion_protection = var.deletion_protection
final_snapshot_identifier = "${var.final_snapshot_identifier_prefix}${var.name}-${random_id.snapshot_identifier[0].hex}"
skip_final_snapshot = var.skip_final_snapshot
backup_retention_period = var.backup_retention_period
preferred_backup_window = var.preferred_backup_window
preferred_maintenance_window = var.preferred_maintenance_window
port = local.port
db_subnet_group_name = aws_db_subnet_group.main[0].name
vpc_security_group_ids = concat([aws_security_group.main[0].id], var.extra_security_groups)
snapshot_identifier = var.snapshot_identifier
storage_encrypted = var.storage_encrypted
apply_immediately = var.apply_immediately
db_cluster_parameter_group_name = var.db_cluster_parameter_group_name
tags = var.tags
timeouts {
create = var.create_timeout
update = var.update_timeout
delete = var.delete_timeout
}
}
resource "aws_rds_cluster_instance" "instance" {
count = local.cluster_instance_count
identifier = "${var.name}-${count.index + 1}"
cluster_identifier = aws_rds_cluster.main[0].id
engine = var.engine
engine_version = var.engine_version
instance_class = var.instance_type
publicly_accessible = var.publicly_accessible
db_subnet_group_name = aws_db_subnet_group.main[0].name
db_parameter_group_name = var.db_parameter_group_name
preferred_backup_window = var.preferred_backup_window_instance
preferred_maintenance_window = var.preferred_maintenance_window_instance
apply_immediately = var.apply_immediately
monitoring_role_arn = join("", aws_iam_role.rds_enhanced_monitoring.*.arn)
monitoring_interval = var.monitoring_interval
auto_minor_version_upgrade = var.auto_minor_version_upgrade
promotion_tier = count.index + 1
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 = var.tags
}
resource "aws_rds_cluster_instance" "data_reader" {
count = var.create_data_reader && var.create_resources ? 1 : 0
identifier = "${var.name}-data-reader"
cluster_identifier = aws_rds_cluster.main[0].id
engine = var.engine
engine_version = var.engine_version
instance_class = var.data_reader_instance_type
publicly_accessible = var.publicly_accessible
db_subnet_group_name = aws_db_subnet_group.main[0].name
db_parameter_group_name = var.data_reader_parameter_group_name
preferred_backup_window = var.preferred_backup_window_instance
preferred_maintenance_window = var.preferred_maintenance_window_instance
apply_immediately = var.apply_immediately
monitoring_role_arn = join("", aws_iam_role.rds_enhanced_monitoring.*.arn)
monitoring_interval = var.monitoring_interval
auto_minor_version_upgrade = var.auto_minor_version_upgrade
promotion_tier = 15
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.data_reader_tags)
}
resource "random_id" "snapshot_identifier" {
count = var.create_resources ? 1 : 0
byte_length = 4
keepers = {
id = var.name
}
}
data "aws_iam_policy_document" "monitoring_rds_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["monitoring.rds.amazonaws.com"]
}
}
}
resource "aws_iam_role" "rds_enhanced_monitoring" {
count = local.create_enhanced_monitoring ? 1 : 0
name = "rds-enhanced-monitoring-${var.name}"
assume_role_policy = data.aws_iam_policy_document.monitoring_rds_assume_role.json
}
resource "aws_iam_role_policy_attachment" "rds_enhanced_monitoring" {
count = local.create_enhanced_monitoring ? 1 : 0
role = aws_iam_role.rds_enhanced_monitoring[0].name
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}
resource "aws_appautoscaling_target" "read_replica_count" {
count = var.replica_autoscaling ? 1 : 0
max_capacity = var.replica_scale_max
min_capacity = var.replica_scale_min
resource_id = "cluster:${aws_rds_cluster.main[0].cluster_identifier}"
scalable_dimension = "rds:cluster:ReadReplicaCount"
service_namespace = "rds"
}
resource "aws_appautoscaling_policy" "autoscaling_read_replica_count" {
count = var.replica_autoscaling ? 1 : 0
name = "target-metric"
policy_type = "TargetTrackingScaling"
resource_id = "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 = "RDSReaderAverageCPUUtilization"
}
scale_in_cooldown = var.replica_scale_in_cooldown
scale_out_cooldown = var.replica_scale_out_cooldown
target_value = var.replica_scale_cpu
}
depends_on = [aws_appautoscaling_target.read_replica_count]
}
resource "aws_security_group" "main" {
count = var.create_resources ? 1 : 0
name = "${var.security_group_name_prefix}${var.name}"
description = "For Aurora cluster ${var.name}"
vpc_id = var.vpc_id
tags = merge(
var.tags,
{
"Name" = "aurora-${var.name}"
},
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_security_group_rule" "default_ingress" {
count = var.create_resources ? length(var.allowed_security_groups) : 0
type = "ingress"
from_port = aws_rds_cluster.main[0].port
to_port = aws_rds_cluster.main[0].port
protocol = "tcp"
source_security_group_id = element(var.allowed_security_groups, count.index)
security_group_id = aws_security_group.main[0].id
}