-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
main.tf
230 lines (184 loc) · 10.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
locals {
monitoring_role_arn = var.create_monitoring_role ? aws_iam_role.enhanced_monitoring[0].arn : var.monitoring_role_arn
final_snapshot_identifier = var.skip_final_snapshot ? null : "${var.final_snapshot_identifier_prefix}-${var.identifier}-${try(random_id.snapshot_identifier[0].hex, "")}"
identifier = var.use_identifier_prefix ? null : var.identifier
identifier_prefix = var.use_identifier_prefix ? "${var.identifier}-" : null
monitoring_role_name = var.monitoring_role_use_name_prefix ? null : var.monitoring_role_name
monitoring_role_name_prefix = var.monitoring_role_use_name_prefix ? "${var.monitoring_role_name}-" : null
# Replicas will use source metadata
is_replica = var.replicate_source_db != null
}
# Ref. https://docs.aws.amazon.com/general/latest/gr/aws-arns-and-namespaces.html#genref-aws-service-namespaces
data "aws_partition" "current" {}
resource "random_id" "snapshot_identifier" {
count = var.create && !var.skip_final_snapshot ? 1 : 0
keepers = {
id = var.identifier
}
byte_length = 4
}
resource "aws_db_instance" "this" {
count = var.create ? 1 : 0
identifier = local.identifier
identifier_prefix = local.identifier_prefix
engine = local.is_replica ? null : var.engine
engine_version = var.engine_version
engine_lifecycle_support = var.engine_lifecycle_support
instance_class = var.instance_class
allocated_storage = var.allocated_storage
storage_type = var.storage_type
storage_encrypted = var.storage_encrypted
kms_key_id = var.kms_key_id
license_model = var.license_model
db_name = var.db_name
username = !local.is_replica ? var.username : null
password = !local.is_replica && var.manage_master_user_password ? null : var.password
port = var.port
domain = var.domain
domain_auth_secret_arn = var.domain_auth_secret_arn
domain_dns_ips = var.domain_dns_ips
domain_fqdn = var.domain_fqdn
domain_iam_role_name = var.domain_iam_role_name
domain_ou = var.domain_ou
iam_database_authentication_enabled = var.iam_database_authentication_enabled
custom_iam_instance_profile = var.custom_iam_instance_profile
manage_master_user_password = !local.is_replica && var.manage_master_user_password ? var.manage_master_user_password : null
master_user_secret_kms_key_id = !local.is_replica && var.manage_master_user_password ? var.master_user_secret_kms_key_id : null
vpc_security_group_ids = var.vpc_security_group_ids
db_subnet_group_name = var.db_subnet_group_name
parameter_group_name = var.parameter_group_name
option_group_name = var.option_group_name
network_type = var.network_type
availability_zone = var.availability_zone
multi_az = var.multi_az
iops = var.iops
storage_throughput = var.storage_throughput
publicly_accessible = var.publicly_accessible
ca_cert_identifier = var.ca_cert_identifier
dedicated_log_volume = var.dedicated_log_volume
upgrade_storage_config = var.upgrade_storage_config
allow_major_version_upgrade = var.allow_major_version_upgrade
auto_minor_version_upgrade = var.auto_minor_version_upgrade
apply_immediately = var.apply_immediately
maintenance_window = var.maintenance_window
# https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/blue-green-deployments.html
dynamic "blue_green_update" {
for_each = length(var.blue_green_update) > 0 ? [var.blue_green_update] : []
content {
enabled = try(blue_green_update.value.enabled, null)
}
}
snapshot_identifier = var.snapshot_identifier
copy_tags_to_snapshot = var.copy_tags_to_snapshot
skip_final_snapshot = var.skip_final_snapshot
final_snapshot_identifier = local.final_snapshot_identifier
performance_insights_enabled = var.performance_insights_enabled
performance_insights_retention_period = var.performance_insights_enabled ? var.performance_insights_retention_period : null
performance_insights_kms_key_id = var.performance_insights_enabled ? var.performance_insights_kms_key_id : null
replicate_source_db = var.replicate_source_db
replica_mode = var.replica_mode
backup_retention_period = length(var.blue_green_update) > 0 ? coalesce(var.backup_retention_period, 1) : var.backup_retention_period
backup_window = var.backup_window
max_allocated_storage = var.max_allocated_storage
monitoring_interval = var.monitoring_interval
monitoring_role_arn = var.monitoring_interval > 0 ? local.monitoring_role_arn : null
character_set_name = var.character_set_name
nchar_character_set_name = var.nchar_character_set_name
timezone = var.timezone
enabled_cloudwatch_logs_exports = var.enabled_cloudwatch_logs_exports
deletion_protection = var.deletion_protection
delete_automated_backups = var.delete_automated_backups
dynamic "restore_to_point_in_time" {
for_each = var.restore_to_point_in_time != null ? [var.restore_to_point_in_time] : []
content {
restore_time = lookup(restore_to_point_in_time.value, "restore_time", null)
source_db_instance_automated_backups_arn = lookup(restore_to_point_in_time.value, "source_db_instance_automated_backups_arn", null)
source_db_instance_identifier = lookup(restore_to_point_in_time.value, "source_db_instance_identifier", null)
source_dbi_resource_id = lookup(restore_to_point_in_time.value, "source_dbi_resource_id", null)
use_latest_restorable_time = lookup(restore_to_point_in_time.value, "use_latest_restorable_time", null)
}
}
dynamic "s3_import" {
for_each = var.s3_import != null ? [var.s3_import] : []
content {
source_engine = "mysql"
source_engine_version = s3_import.value.source_engine_version
bucket_name = s3_import.value.bucket_name
bucket_prefix = lookup(s3_import.value, "bucket_prefix", null)
ingestion_role = s3_import.value.ingestion_role
}
}
tags = merge(var.tags, var.db_instance_tags)
depends_on = [aws_cloudwatch_log_group.this]
timeouts {
create = lookup(var.timeouts, "create", null)
delete = lookup(var.timeouts, "delete", null)
update = lookup(var.timeouts, "update", null)
}
# Note: do not add `latest_restorable_time` to `ignore_changes`
# https://github.com/terraform-aws-modules/terraform-aws-rds/issues/478
}
################################################################################
# CloudWatch Log Group
################################################################################
# Log groups will not be created if using an identifier prefix
resource "aws_cloudwatch_log_group" "this" {
for_each = toset([for log in var.enabled_cloudwatch_logs_exports : log if var.create && var.create_cloudwatch_log_group && !var.use_identifier_prefix])
name = "/aws/rds/instance/${var.identifier}/${each.value}"
retention_in_days = var.cloudwatch_log_group_retention_in_days
kms_key_id = var.cloudwatch_log_group_kms_key_id
skip_destroy = var.cloudwatch_log_group_skip_destroy
log_group_class = var.cloudwatch_log_group_class
tags = merge(var.tags, var.cloudwatch_log_group_tags)
}
################################################################################
# Enhanced monitoring
################################################################################
data "aws_iam_policy_document" "enhanced_monitoring" {
statement {
actions = [
"sts:AssumeRole",
]
principals {
type = "Service"
identifiers = ["monitoring.rds.amazonaws.com"]
}
}
}
resource "aws_iam_role" "enhanced_monitoring" {
count = var.create_monitoring_role ? 1 : 0
name = local.monitoring_role_name
name_prefix = local.monitoring_role_name_prefix
assume_role_policy = data.aws_iam_policy_document.enhanced_monitoring.json
description = var.monitoring_role_description
permissions_boundary = var.monitoring_role_permissions_boundary
tags = merge(
{
"Name" = format("%s", var.monitoring_role_name)
},
var.tags,
)
}
resource "aws_iam_role_policy_attachment" "enhanced_monitoring" {
count = var.create_monitoring_role ? 1 : 0
role = aws_iam_role.enhanced_monitoring[0].name
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}
################################################################################
# Managed Secret Rotation
################################################################################
# There is not currently a way to disable secret rotation on an initial apply.
# In order to use master password secrets management without a rotation, the following workaround can be used:
# `manage_master_user_password_rotation` must be set to true first and applied followed by setting it to false and another apply.
# Note: when setting `manage_master_user_password_rotation` to true, a schedule must also be set using `master_user_password_rotation_schedule_expression` or `master_user_password_rotation_automatically_after_days`.
# See: https://github.com/hashicorp/terraform-provider-aws/issues/37779
resource "aws_secretsmanager_secret_rotation" "this" {
count = var.create && var.manage_master_user_password && var.manage_master_user_password_rotation ? 1 : 0
secret_id = aws_db_instance.this[0].master_user_secret[0].secret_arn
rotate_immediately = var.master_user_password_rotate_immediately
rotation_rules {
automatically_after_days = var.master_user_password_rotation_automatically_after_days
duration = var.master_user_password_rotation_duration
schedule_expression = var.master_user_password_rotation_schedule_expression
}
}