forked from cloudposse/terraform-aws-rds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.tf
178 lines (160 loc) · 6.93 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
module "label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.15.0"
enabled = var.enabled
namespace = var.namespace
name = var.name
stage = var.stage
delimiter = var.delimiter
attributes = var.attributes
tags = var.tags
}
module "final_snapshot_label" {
source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.15.0"
enabled = var.enabled
namespace = var.namespace
name = var.name
stage = var.stage
delimiter = var.delimiter
attributes = compact(concat(var.attributes, ["final", "snapshot"]))
tags = var.tags
}
locals {
computed_major_engine_version = var.engine == "postgres" ? join(".", slice(split(".", var.engine_version), 0, 1)) : join(".", slice(split(".", var.engine_version), 0, 2))
major_engine_version = var.major_engine_version == "" ? local.computed_major_engine_version : var.major_engine_version
}
resource "aws_db_instance" "default" {
count = var.enabled ? 1 : 0
identifier = module.label.id
name = var.database_name
username = var.database_user
password = var.database_password
port = var.database_port
engine = var.engine
engine_version = var.engine_version
instance_class = var.instance_class
allocated_storage = var.allocated_storage
max_allocated_storage = var.max_allocated_storage
storage_encrypted = var.storage_encrypted
kms_key_id = var.kms_key_arn
vpc_security_group_ids = compact(
concat(
[join("", aws_security_group.default.*.id)],
var.associate_security_group_ids
)
)
db_subnet_group_name = join("", aws_db_subnet_group.default.*.name)
parameter_group_name = length(var.parameter_group_name) > 0 ? var.parameter_group_name : join("", aws_db_parameter_group.default.*.name)
option_group_name = length(var.option_group_name) > 0 ? var.option_group_name : join("", aws_db_option_group.default.*.name)
license_model = var.license_model
multi_az = var.multi_az
storage_type = var.storage_type
iops = var.iops
publicly_accessible = var.publicly_accessible
snapshot_identifier = var.snapshot_identifier
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
skip_final_snapshot = var.skip_final_snapshot
copy_tags_to_snapshot = var.copy_tags_to_snapshot
backup_retention_period = var.backup_retention_period
backup_window = var.backup_window
tags = module.label.tags
deletion_protection = var.deletion_protection
final_snapshot_identifier = length(var.final_snapshot_identifier) > 0 ? var.final_snapshot_identifier : module.final_snapshot_label.id
enabled_cloudwatch_logs_exports = var.enabled_cloudwatch_logs_exports
performance_insights_enabled = var.performance_insights_enabled
performance_insights_kms_key_id = var.performance_insights_enabled ? var.performance_insights_kms_key_id : null
performance_insights_retention_period = var.performance_insights_enabled ? var.performance_insights_retention_period : null
}
resource "aws_db_parameter_group" "default" {
count = length(var.parameter_group_name) == 0 && var.enabled ? 1 : 0
name = module.label.id
family = var.db_parameter_group
tags = module.label.tags
dynamic "parameter" {
for_each = var.db_parameter
content {
apply_method = lookup(parameter.value, "apply_method", null)
name = parameter.value.name
value = parameter.value.value
}
}
}
resource "aws_db_option_group" "default" {
count = length(var.option_group_name) == 0 && var.enabled ? 1 : 0
name = module.label.id
engine_name = var.engine
major_engine_version = local.major_engine_version
tags = module.label.tags
dynamic "option" {
for_each = var.db_options
content {
db_security_group_memberships = lookup(option.value, "db_security_group_memberships", null)
option_name = option.value.option_name
port = lookup(option.value, "port", null)
version = lookup(option.value, "version", null)
vpc_security_group_memberships = lookup(option.value, "vpc_security_group_memberships", null)
dynamic "option_settings" {
for_each = lookup(option.value, "option_settings", [])
content {
name = option_settings.value.name
value = option_settings.value.value
}
}
}
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_db_subnet_group" "default" {
count = var.enabled ? 1 : 0
name = module.label.id
subnet_ids = var.subnet_ids
tags = module.label.tags
}
resource "aws_security_group" "default" {
count = var.enabled ? 1 : 0
name = module.label.id
description = "Allow inbound traffic from the security groups"
vpc_id = var.vpc_id
tags = module.label.tags
}
resource "aws_security_group_rule" "ingress_security_groups" {
count = var.enabled ? length(var.security_group_ids) : 0
description = "Allow inbound traffic from existing Security Groups"
type = "ingress"
from_port = var.database_port
to_port = var.database_port
protocol = "tcp"
source_security_group_id = var.security_group_ids[count.index]
security_group_id = join("", aws_security_group.default.*.id)
}
resource "aws_security_group_rule" "ingress_cidr_blocks" {
count = var.enabled && length(var.allowed_cidr_blocks) > 0 ? 1 : 0
description = "Allow inbound traffic from CIDR blocks"
type = "ingress"
from_port = var.database_port
to_port = var.database_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 = var.enabled ? 1 : 0
description = "Allow all egress 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)
}
module "dns_host_name" {
source = "git::https://github.com/cloudposse/terraform-aws-route53-cluster-hostname.git?ref=tags/0.3.0"
enabled = length(var.dns_zone_id) > 0 && var.enabled ? true : false
name = var.host_name
zone_id = var.dns_zone_id
records = coalescelist(aws_db_instance.default.*.address, [""])
}