forked from Young-ook/terraform-aws-spinnaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.tf
124 lines (107 loc) · 3.96 KB
/
database.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
## aurora cluster for orca-mysql
# security/password
resource "random_password" "password" {
count = (var.mysql_node_size > 0) ? 1 : 0
length = 16
special = true
override_special = "^"
}
# security/firewall
resource "aws_security_group" "db" {
count = (var.mysql_node_size > 0) ? 1 : 0
name = format("%s-db", local.name)
description = format("security group for %s-db", local.name)
vpc_id = aws_vpc.vpc.id
tags = merge(map("Name", format("%s-db", local.name)), var.tags)
}
resource "aws_security_group_rule" "db-ingress-rules" {
count = (var.mysql_node_size > 0) ? 1 : 0
type = "ingress"
from_port = var.mysql_port
to_port = var.mysql_port
protocol = "tcp"
source_security_group_id = aws_security_group.nodes.id
security_group_id = aws_security_group.db[0].id
}
# subnet group
resource "aws_db_subnet_group" "db" {
count = (var.mysql_node_size > 0) ? 1 : 0
name = format("%s-db", local.name)
subnet_ids = aws_subnet.private.*.id
tags = merge(map("Name", format("%s-db", local.name)), var.tags)
}
# parameter groups
resource "aws_rds_cluster_parameter_group" "db" {
count = (var.mysql_node_size > 0) ? 1 : 0
name = format("%s-db-cluster-params", local.name)
family = format("aurora-mysql%s.%s",
element(split(".", var.mysql_version), 0),
element(split(".", var.mysql_version), 1)
)
parameter {
name = "character_set_server"
value = "utf8"
}
parameter {
name = "character_set_client"
value = "utf8"
}
lifecycle {
create_before_destroy = true
}
}
resource "aws_db_parameter_group" "db" {
count = (var.mysql_node_size > 0) ? 1 : 0
name = format("%s-db-params", local.name)
family = format("aurora-mysql%s.%s",
element(split(".", var.mysql_version), 0),
element(split(".", var.mysql_version), 1)
)
lifecycle {
create_before_destroy = true
}
}
# rds (aurora)
resource "aws_rds_cluster" "db" {
count = (var.mysql_node_size > 0) ? 1 : 0
cluster_identifier_prefix = format("%s-", local.cluster-name)
engine = "aurora-mysql"
engine_mode = "provisioned"
engine_version = var.mysql_version
port = var.mysql_port
skip_final_snapshot = "true"
database_name = var.mysql_db
master_username = var.mysql_master_user
master_password = random_password.password[0].result
snapshot_identifier = var.mysql_snapshot
backup_retention_period = "5"
db_subnet_group_name = aws_db_subnet_group.db[0].name
db_cluster_parameter_group_name = aws_rds_cluster_parameter_group.db[0].name
vpc_security_group_ids = coalescelist(aws_security_group.db.*.id, list(""))
tags = merge(map("Name", format("%s-db", local.name)), var.tags)
lifecycle {
ignore_changes = [snapshot_identifier, master_password]
create_before_destroy = true
}
}
# rds instances
resource "aws_rds_cluster_instance" "db" {
count = var.mysql_node_size
identifier = format("%s-%s", local.cluster-name, count.index)
cluster_identifier = aws_rds_cluster.db[0].id
instance_class = var.mysql_node_type
engine = "aurora-mysql"
engine_version = var.mysql_version
db_parameter_group_name = aws_db_parameter_group.db[0].name
db_subnet_group_name = aws_db_subnet_group.db[0].name
apply_immediately = var.mysql_apply_immediately
}
# dns records
resource "aws_route53_record" "db" {
count = (var.mysql_node_size > 0) ? 1 : 0
zone_id = aws_route53_zone.vpc.zone_id
name = format("%s-db.%s", local.cluster-name, var.dns_zone)
type = "CNAME"
ttl = 300
records = coalescelist(aws_rds_cluster.db.*.endpoint, list(""))
}