-
Notifications
You must be signed in to change notification settings - Fork 1
/
rds.tf
133 lines (115 loc) · 4.81 KB
/
rds.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
locals {
username = length(var.rds_username) > 0 ? format("%suser", var.rds_username) : format("%suser", var.rds_database_name)
}
resource "aws_db_instance" "this" {
count = local.count_rds_instance
db_name = var.rds_database_name
identifier = var.rds_identifier
engine = var.rds_engine
engine_version = var.rds_engine_version
instance_class = var.rds_instance_class
username = local.username
password = var.rds_password
parameter_group_name = local.count_rds_parameter_group > 0 ? var.rds_parameter_group_name : null
iam_database_authentication_enabled = var.rds_iam_authentication_enabled
enabled_cloudwatch_logs_exports = var.rds_cloudwatch_logs_exports
option_group_name = var.rds_option_group_name
apply_immediately = var.rds_apply_immediately
auto_minor_version_upgrade = var.rds_auto_minor_version_upgrade
db_subnet_group_name = var.rds_subnet_group
vpc_security_group_ids = var.rds_security_group_ids
allocated_storage = var.rds_allocated_storage
max_allocated_storage = var.rds_max_allocated_storage
backup_retention_period = var.rds_backup_retention_period
iops = var.rds_iops
multi_az = var.rds_multi_az
monitoring_interval = var.rds_monitoring_interval
monitoring_role_arn = var.rds_monitoring_role_arn
performance_insights_enabled = var.rds_enable_performance_insights
backup_window = var.rds_backup_window
skip_final_snapshot = var.rds_skip_final_snapshot
final_snapshot_identifier = var.rds_final_snapshot_identifier
storage_encrypted = var.rds_enable_storage_encryption
kms_key_id = var.rds_storage_encryption_kms_key_arn
deletion_protection = var.rds_enable_deletion_protection
tags = merge(
{
"Name" = var.rds_identifier,
"rds_engine" = var.rds_engine
},
var.tags,
var.rds_tags,
)
// We need this dependency because we only pass the var `rds_parameter_group_name` rather than reference the resource.
// Terraform can't tell that it needs to wait for it to be created based on the name alone.
depends_on = [
aws_db_parameter_group.db_parameter_group
]
}
data "aws_db_snapshot" "latest_snapshot" {
count = local.count_rds_instance_with_snapshot
db_instance_identifier = var.rds_identifier
most_recent = true
}
resource "aws_db_instance" "snapshot" {
count = local.count_rds_instance_with_snapshot
db_name = var.rds_database_name
identifier = var.rds_identifier
engine = var.rds_engine
engine_version = var.rds_engine_version
instance_class = var.rds_instance_class
username = local.username
password = var.rds_password
snapshot_identifier = data.aws_db_snapshot.latest_snapshot[0].id
option_group_name = var.rds_option_group_name
apply_immediately = var.rds_apply_immediately
auto_minor_version_upgrade = var.rds_auto_minor_version_upgrade
db_subnet_group_name = var.rds_subnet_group
vpc_security_group_ids = var.rds_security_group_ids
allocated_storage = var.rds_allocated_storage
max_allocated_storage = var.rds_max_allocated_storage
backup_retention_period = var.rds_backup_retention_period
iops = var.rds_iops
multi_az = var.rds_multi_az
monitoring_interval = var.rds_monitoring_interval
monitoring_role_arn = var.rds_monitoring_role_arn
performance_insights_enabled = var.rds_enable_performance_insights
backup_window = var.rds_backup_window
skip_final_snapshot = var.rds_skip_final_snapshot
final_snapshot_identifier = var.rds_final_snapshot_identifier
storage_encrypted = var.rds_enable_storage_encryption
kms_key_id = var.rds_storage_encryption_kms_key_arn
deletion_protection = var.rds_enable_deletion_protection
tags = merge(
{
"Name" = var.rds_identifier,
"rds_engine" = var.rds_engine
},
var.tags,
var.rds_tags,
)
lifecycle {
ignore_changes = [snapshot_identifier]
}
}
resource "aws_db_parameter_group" "db_parameter_group" {
count = local.count_rds_parameter_group
name = var.rds_parameter_group_name
family = var.rds_parameter_group_family
dynamic "parameter" {
for_each = var.rds_parameter_group_parameters
content {
name = parameter.key
value = parameter.value
apply_method = "pending-reboot"
}
}
tags = merge(
{
"Name" = var.rds_identifier,
"rds_engine" = var.rds_engine
},
var.tags,
var.rds_tags,
)
}