-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.tf
255 lines (239 loc) · 10.3 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/**
* # AWS Aurora Serverless Terraform module
*
* ![Build Status](https://travis-ci.com/104corp/terraform-aws-aurora-serverless.svg?branch=master) ![LicenseBadge](https://img.shields.io/github/license/104corp/terraform-aws-aurora-serverless.svg)
*
* AWS Aurora DB Cluster for Serverless Terraform Module.
*
* Gives you:
*
* - A DB subnet group
* - An Aurora DB cluster
* - Optionally RDS 'Enhanced Monitoring' + associated required IAM role/policy (by simply setting the `monitoring_interval` param to > `0`
* - Optionally sensible alarms to SNS (high CPU, high connections, slow replication)
*
* ## Terraform version
*
* Terraform version 0.10.3 or newer is required for this module to work.
*
* ## Contributing
*
* Ensure any variables you add have a type and a description.
* This README is generated with [terraform-docs](https://github.com/segmentio/terraform-docs):
*
* `terraform-docs md . > README.md`
*
* ## Usage examples
*
* *It is recommended you always create a parameter group, even if it exactly matches the defaults.*
* Changing the parameter group in use requires a restart of the DB cluster, modifying parameters within a group
* may not (depending on the parameter being altered)
*
* ### Aurora 1.x (MySQL 5.6)
*
* ```hcl
* resource "aws_sns_topic" "db_alarms_56" {
* name = "aurora-db-alarms-56"
* }
*
* module "aurora_db_56" {
* source = "../.."
* name = "test-aurora-db-56"
* envname = "test56"
* envtype = "test"
* subnets = ["${module.vpc.private_subnets}"]
* azs = ["${module.vpc.availability_zones}"]
* security_groups = ["${aws_security_group.allow_all.id}"]
* username = "root"
* password = "changeme"
* backup_retention_period = "5"
* final_snapshot_identifier = "final-db-snapshot-prod"
* storage_encrypted = "true"
* apply_immediately = "true"
* monitoring_interval = "10"
* cw_alarms = true
* cw_sns_topic = "${aws_sns_topic.db_alarms_56.id}"
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_56_parameter_group.id}"
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_56_parameter_group.id}"
* }
*
* resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
* name = "test-aurora-db-56-parameter-group"
* family = "aurora5.6"
* description = "test-aurora-db-56-parameter-group"
* }
*
* resource "aws_rds_cluster_parameter_group" "aurora_cluster_56_parameter_group" {
* name = "test-aurora-56-cluster-parameter-group"
* family = "aurora5.6"
* description = "test-aurora-56-cluster-parameter-group"
* }
* ```
*
* ### Aurora 2.x (MySQL 5.7)
*
* ```hcl
* resource "aws_sns_topic" "db_alarms" {
* name = "aurora-db-alarms"
* }
*
* module "aurora_db_57" {
* source = "../.."
* engine_version = "5.7.12"
* name = "test-aurora-db-57"
* envname = "test-57"
* envtype = "test"
* subnets = ["${module.vpc.private_subnets}"]
* azs = ["${module.vpc.availability_zones}"]
* security_groups = ["${aws_security_group.allow_all.id}"]
* username = "root"
* password = "changeme"
* backup_retention_period = "5"
* final_snapshot_identifier = "final-db-snapshot-prod"
* storage_encrypted = "true"
* apply_immediately = "true"
* monitoring_interval = "10"
* cw_alarms = true
* cw_sns_topic = "${aws_sns_topic.db_alarms.id}"
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_57_parameter_group.id}"
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_57_cluster_parameter_group.id}"
* }
*
* resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
* name = "test-aurora-db-57-parameter-group"
* family = "aurora-mysql5.7"
* description = "test-aurora-db-57-parameter-group"
* }
*
* resource "aws_rds_cluster_parameter_group" "aurora_57_cluster_parameter_group" {
* name = "test-aurora-57-cluster-parameter-group"
* family = "aurora-mysql5.7"
* description = "test-aurora-57-cluster-parameter-group"
* }
* ```
*
* ### Aurora PostgreSQL
*
* ```hcl
* resource "aws_sns_topic" "db_alarms_postgres96" {
* name = "aurora-db-alarms-postgres96"
* }
*
* module "aurora_db_postgres96" {
* source = "../.."
* engine = "aurora-postgresql"
* engine_version = "9.6.3"
* name = "test-aurora-db-postgres96"
* envname = "test-pg96"
* envtype = "test"
* subnets = ["${module.vpc.private_subnets}"]
* azs = ["${module.vpc.availability_zones}"]
* security_groups = ["${aws_security_group.allow_all.id}"]
* username = "root"
* password = "changeme"
* backup_retention_period = "5"
* final_snapshot_identifier = "final-db-snapshot-prod"
* storage_encrypted = "true"
* apply_immediately = "true"
* monitoring_interval = "10"
* cw_alarms = true
* cw_sns_topic = "${aws_sns_topic.db_alarms_postgres96.id}"
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_postgres96_parameter_group.id}"
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_postgres96_parameter_group.id}"
* }
*
* resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {
* name = "test-aurora-db-postgres96-parameter-group"
* family = "aurora-postgresql9.6"
* description = "test-aurora-db-postgres96-parameter-group"
* }
*
* resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres96_parameter_group" {
* name = "test-aurora-postgres96-cluster-parameter-group"
* family = "aurora-postgresql9.6"
* description = "test-aurora-postgres96-cluster-parameter-group"
* }
* ```
*
*
* ## Development
*
* Terraform modules on the Terraform Module Registry are open projects, and community contributions are essential for keeping them great. Please follow our guidelines when contributing changes.
*
* For more information, see our [module contribution guide](https://registry.terraform.io/modules/104corp/aurora-serverless/aws/).
*
* ## Contributors
*
* To see who's already involved, see the list of [contributors](https://github.com/104corp/terraform-aws-aurora-serverless/graphs/contributors).
*/
// Create DB Cluster
resource "aws_rds_cluster" "default" {
count = "${var.enabled ? 1 : 0}"
cluster_identifier = "${var.identifier_prefix != "" ? format("%s-cluster", var.identifier_prefix) : format("%s-aurora-cluster", var.envname)}"
availability_zones = ["${var.azs}"]
engine = "aurora"
engine_version = "${var.engine_version}"
engine_mode = "serverless"
database_name = "${var.database_name}"
master_username = "${var.username}"
master_password = "${var.password}"
final_snapshot_identifier = "${var.final_snapshot_identifier}-${random_id.server.hex}"
skip_final_snapshot = "${var.skip_final_snapshot}"
backup_retention_period = "${var.backup_retention_period}"
preferred_backup_window = "${var.preferred_backup_window}"
preferred_maintenance_window = "${var.preferred_maintenance_window}"
port = "${var.port}"
db_subnet_group_name = "${aws_db_subnet_group.main.name}"
vpc_security_group_ids = ["${var.security_groups}"]
snapshot_identifier = "${var.snapshot_identifier}"
storage_encrypted = "${var.storage_encrypted}"
apply_immediately = "${var.apply_immediately}"
db_cluster_parameter_group_name = "${var.db_cluster_parameter_group_name}"
iam_database_authentication_enabled = "${var.iam_database_authentication_enabled}"
scaling_configuration {
auto_pause = "${var.auto_pause}"
max_capacity = "${var.max_capacity}"
min_capacity = "${var.min_capacity}"
}
lifecycle {
create_before_destroy = true
}
tags = "${local.tags}"
}
// DB Subnet Group creation
resource "aws_db_subnet_group" "main" {
count = "${var.enabled ? 1 : 0}"
name = "${var.name}"
description = "Group of DB subnets"
subnet_ids = ["${var.subnets}"]
tags = "${local.tags}"
}
// Geneate an ID when an environment is initialised
resource "random_id" "server" {
count = "${var.enabled ? 1 : 0}"
keepers = {
id = "${aws_db_subnet_group.main.name}"
}
byte_length = 8
}
// IAM Role + Policy attach for Enhanced Monitoring
data "aws_iam_policy_document" "monitoring-rds-assume-role-policy" {
count = "${var.enabled ? 1 : 0}"
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["monitoring.rds.amazonaws.com"]
}
}
}
resource "aws_iam_role" "rds-enhanced-monitoring" {
count = "${var.enabled && var.monitoring_interval > 0 ? 1 : 0}"
name_prefix = "rds-enhanced-mon-${var.envname}-"
assume_role_policy = "${data.aws_iam_policy_document.monitoring-rds-assume-role-policy.json}"
}
resource "aws_iam_role_policy_attachment" "rds-enhanced-monitoring-policy-attach" {
count = "${var.enabled && var.monitoring_interval > 0 ? 1 : 0}"
role = "${aws_iam_role.rds-enhanced-monitoring.name}"
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonRDSEnhancedMonitoringRole"
}