-
Notifications
You must be signed in to change notification settings - Fork 12
/
aurora.tf
181 lines (142 loc) · 4.85 KB
/
aurora.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
# --------------------------------------------------------------------------
# Generate random username
# --------------------------------------------------------------------------
resource "random_string" "db_username" {
length = 12
special = false
upper = false
number = false
keepers = {
cluster_identifier = var.name
}
}
# --------------------------------------------------------------------------
# Generate safe random password
# --------------------------------------------------------------------------
resource "random_string" "db_password" {
length = 28
special = false
keepers = {
cluster_identifier = var.name
}
}
# --------------------------------------------------------------------------
# SecurityGroup
# --------------------------------------------------------------------------
resource "aws_security_group" "aurora" {
name_prefix = "${var.name}-"
description = "${var.name} - Aurora"
vpc_id = var.vpc_id
ingress {
from_port = 3306
to_port = 3306
protocol = "tcp"
cidr_blocks = data.aws_subnet.subnet.*.cidr_block
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"] #tfsec:ignore:AWS009
}
tags = local.tags
lifecycle {
create_before_destroy = true
}
}
# --------------------------------------------------------------------------
# KMS Key for encryption-at-rest
# --------------------------------------------------------------------------
data "aws_kms_key" "kms" {
count = var.kms_key != null ? 1 : 0
key_id = var.kms_key
}
data "aws_kms_alias" "kms" {
name = "alias/aws/rds"
}
resource "random_integer" "final_identifier" {
min = 1000
max = 9999
keepers = {
cluster_identifier = var.name
}
}
# --------------------------------------------------------------------------
# AWS RDS Aurora Serverless
# --------------------------------------------------------------------------
resource "aws_db_subnet_group" "default" {
count = var.db_subnet_group_name == null ? 1 : 0
name = "${var.name}-public"
subnet_ids = var.public_subnet_ids
tags = local.tags
}
resource "aws_rds_cluster" "aurora" {
cluster_identifier = var.name
#availability_zones = data.aws_availability_zones.available.names
# Engine
engine = "aurora"
engine_mode = "serverless"
engine_version = "5.6.10a"
# Database
database_name = var.db_name
master_username = random_string.db_username.result
master_password = random_string.db_password.result
db_subnet_group_name = var.db_subnet_group_name == null ? aws_db_subnet_group.default[0].name : var.db_subnet_group_name
vpc_security_group_ids = [aws_security_group.aurora.id]
final_snapshot_identifier = "final-${var.name}-${random_integer.final_identifier.result}"
scaling_configuration {
auto_pause = var.serverless_auto_pause
max_capacity = var.serverless_max
min_capacity = var.serverless_min
seconds_until_auto_pause = var.serverless_seconds_pause
timeout_action = var.serverless_timeout_action
}
enable_http_endpoint = var.serverless_http_endpoint
# Encryption, Logging, Protection
storage_encrypted = true
kms_key_id = var.kms_key == null ? data.aws_kms_alias.kms.target_key_arn : data.aws_kms_key.kms[0].arn
deletion_protection = var.deletion_protection
# Backup
backup_retention_period = var.backup_retention_days
preferred_backup_window = var.backup_window
# Maintenance
apply_immediately = var.apply_immediately
preferred_maintenance_window = var.preferred_maintenance_window
copy_tags_to_snapshot = true
tags = local.tags
}
# --------------------------------------------------------------------------
# SSM Parameter Store
# --------------------------------------------------------------------------
resource "aws_ssm_parameter" "username" {
name = "/rds/${var.name}/username"
value = aws_rds_cluster.aurora.master_username
description = "Aurora DB username for ${var.name}"
type = "SecureString"
overwrite = true
tags = local.tags
}
resource "aws_ssm_parameter" "password" {
name = "/rds/${var.name}/password"
value = aws_rds_cluster.aurora.master_password
description = "Aurora DB password for ${var.name}"
type = "SecureString"
overwrite = true
tags = local.tags
}
resource "aws_ssm_parameter" "endpoint" {
name = "/rds/${var.name}/instance_endpoint"
value = aws_rds_cluster.aurora.endpoint
description = "Aurora Endpoint for ${var.name}"
type = "String"
overwrite = true
tags = local.tags
}
resource "aws_ssm_parameter" "database" {
name = "/rds/${var.name}/database"
value = aws_rds_cluster.aurora.database_name
description = "Aurora DB name for ${var.name}"
type = "String"
overwrite = true
tags = local.tags
}