Skip to content

Commit 1b3a805

Browse files
committed
Better use of tags as map
1 parent 2989a8d commit 1b3a805

File tree

7 files changed

+72
-67
lines changed

7 files changed

+72
-67
lines changed

README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ resource "aws_sns_topic" "db_alarms_56" {
3737
module "aurora_db_56" {
3838
source = "claranet/aurora/aws"
3939
name = "test-aurora-db-56"
40-
envname = "test56"
41-
envtype = "test"
4240
subnets = ["${module.vpc.private_subnets}"]
4341
azs = ["${module.vpc.availability_zones}"]
4442
replica_count = "1"
@@ -55,6 +53,10 @@ module "aurora_db_56" {
5553
cw_sns_topic = "${aws_sns_topic.db_alarms_56.id}"
5654
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_56_parameter_group.id}"
5755
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_56_parameter_group.id}"
56+
tags = {
57+
envname = "test-57-autoscaling"
58+
envtype = "test"
59+
}
5860
}
5961
6062
resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
@@ -82,8 +84,6 @@ module "aurora_db_57" {
8284
engine = "aurora-mysql"
8385
engine-version = "5.7.12"
8486
name = "test-aurora-db-57"
85-
envname = "test-57"
86-
envtype = "test"
8787
subnets = ["${module.vpc.private_subnets}"]
8888
azs = ["${module.vpc.availability_zones}"]
8989
replica_count = "1"
@@ -100,6 +100,10 @@ module "aurora_db_57" {
100100
cw_sns_topic = "${aws_sns_topic.db_alarms.id}"
101101
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_57_parameter_group.id}"
102102
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_57_cluster_parameter_group.id}"
103+
tags = {
104+
envname = "test-57-autoscaling"
105+
envtype = "test"
106+
}
103107
}
104108
105109
resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
@@ -126,8 +130,6 @@ module "aurora_db_postgres96" {
126130
engine = "aurora-postgresql"
127131
engine-version = "9.6.3"
128132
name = "test-aurora-db-postgres96"
129-
envname = "test-pg96"
130-
envtype = "test"
131133
subnets = ["${module.vpc.private_subnets}"]
132134
azs = ["${module.vpc.availability_zones}"]
133135
replica_count = "1"
@@ -144,6 +146,10 @@ module "aurora_db_postgres96" {
144146
cw_sns_topic = "${aws_sns_topic.db_alarms_postgres96.id}"
145147
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_postgres96_parameter_group.id}"
146148
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_postgres96_parameter_group.id}"
149+
tags = {
150+
envname = "test-57-autoscaling"
151+
envtype = "test"
152+
}
147153
}
148154
149155
resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {
@@ -177,8 +183,7 @@ resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres96_parameter_
177183
| db_parameter_group_name | The name of a DB parameter group to use | string | `default.aurora5.6` | no |
178184
| engine | Aurora database engine type, currently aurora, aurora-mysql or aurora-postgresql | string | `aurora` | no |
179185
| engine-version | Aurora database engine version. | string | `5.6.10a` | no |
180-
| envname | Environment name (eg,test, stage or prod) | string | - | yes |
181-
| envtype | Environment type (eg,prod or nonprod) | string | - | yes |
186+
| tags | Tags to attach to resources | map | - | no |
182187
| final_snapshot_identifier | The name to use when creating a final snapshot on cluster destroy, appends a random 8 digits to name to ensure it's unique too. | string | `final` | no |
183188
| identifier_prefix | Prefix for cluster and instance identifier | string | `` | no |
184189
| instance_type | Instance type to use | string | `db.t2.small` | no |

main.tf

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,28 @@
3636
* }
3737
*
3838
* module "aurora_db_56" {
39-
* source = "../.."
40-
* name = "test-aurora-db-56"
41-
* envname = "test56"
42-
* envtype = "test"
43-
* subnets = ["${module.vpc.private_subnets}"]
44-
* azs = ["${module.vpc.availability_zones}"]
45-
* replica_count = "1"
46-
* security_groups = ["${aws_security_group.allow_all.id}"]
47-
* instance_type = "db.t2.medium"
48-
* username = "root"
49-
* password = "changeme"
50-
* backup_retention_period = "5"
51-
* final_snapshot_identifier = "final-db-snapshot-prod"
52-
* storage_encrypted = "true"
53-
* apply_immediately = "true"
54-
* monitoring_interval = "10"
55-
* cw_alarms = true
56-
* cw_sns_topic = "${aws_sns_topic.db_alarms_56.id}"
57-
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_56_parameter_group.id}"
58-
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_56_parameter_group.id}"
39+
* source = "../.."
40+
* name = "test-aurora-db-56"
41+
* subnets = ["${module.vpc.private_subnets}"]
42+
* azs = ["${module.vpc.availability_zones}"]
43+
* replica_count = "1"
44+
* security_groups = ["${aws_security_group.allow_all.id}"]
45+
* instance_type = "db.t2.medium"
46+
* username = "root"
47+
* password = "changeme"
48+
* backup_retention_period = "5"
49+
* final_snapshot_identifier = "final-db-snapshot-prod"
50+
* storage_encrypted = "true"
51+
* apply_immediately = "true"
52+
* monitoring_interval = "10"
53+
* cw_alarms = true
54+
* cw_sns_topic = "${aws_sns_topic.db_alarms_56.id}"
55+
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_56_parameter_group.id}"
56+
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_56_parameter_group.id}"
57+
* tags = {
58+
* envname = "test-57-autoscaling"
59+
* envtype = "test"
60+
* }
5961
* }
6062
*
6163
* resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
@@ -82,8 +84,6 @@
8284
* engine = "aurora-mysql"
8385
* engine-version = "5.7.12"
8486
* name = "test-aurora-db-57"
85-
* envname = "test-57"
86-
* envtype = "test"
8787
* subnets = ["${module.vpc.private_subnets}"]
8888
* azs = ["${module.vpc.availability_zones}"]
8989
* replica_count = "1"
@@ -100,6 +100,10 @@
100100
* cw_sns_topic = "${aws_sns_topic.db_alarms.id}"
101101
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_57_parameter_group.id}"
102102
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_57_cluster_parameter_group.id}"
103+
* tags = {
104+
* envname = "test-57-autoscaling"
105+
* envtype = "test"
106+
* }
103107
* }
104108
*
105109
* resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
@@ -126,8 +130,6 @@
126130
* engine = "aurora-postgresql"
127131
* engine-version = "9.6.3"
128132
* name = "test-aurora-db-postgres96"
129-
* envname = "test-pg96"
130-
* envtype = "test"
131133
* subnets = ["${module.vpc.private_subnets}"]
132134
* azs = ["${module.vpc.availability_zones}"]
133135
* replica_count = "1"
@@ -144,6 +146,10 @@
144146
* cw_sns_topic = "${aws_sns_topic.db_alarms_postgres96.id}"
145147
* db_parameter_group_name = "${aws_db_parameter_group.aurora_db_postgres96_parameter_group.id}"
146148
* db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_postgres96_parameter_group.id}"
149+
* tags = {
150+
* envname = "test-57-autoscaling"
151+
* envtype = "test"
152+
* }
147153
* }
148154
*
149155
* resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {
@@ -166,15 +172,12 @@ resource "aws_db_subnet_group" "main" {
166172
description = "Group of DB subnets"
167173
subnet_ids = ["${var.subnets}"]
168174

169-
tags {
170-
envname = "${var.envname}"
171-
envtype = "${var.envtype}"
172-
}
175+
tags = "${merge(var.tags, map("Name", format("%s", var.identifier_prefix)))}"
173176
}
174177

175178
// Create single DB instance
176179
resource "aws_rds_cluster_instance" "cluster_instance_0" {
177-
identifier = "${var.identifier_prefix != "" ? format("%s-node-0", var.identifier_prefix) : format("%s-aurora-node-0", var.envname)}"
180+
identifier = "${format("%s-node-0", var.identifier_prefix)}"
178181
cluster_identifier = "${aws_rds_cluster.default.id}"
179182
engine = "${var.engine}"
180183
engine_version = "${var.engine-version}"
@@ -189,10 +192,7 @@ resource "aws_rds_cluster_instance" "cluster_instance_0" {
189192
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
190193
promotion_tier = "0"
191194

192-
tags {
193-
envname = "${var.envname}"
194-
envtype = "${var.envtype}"
195-
}
195+
tags = "${merge(var.tags, map("Name", format("%s", var.identifier_prefix)))}"
196196
}
197197

198198
// Create 'n' number of additional DB instance(s) in same cluster
@@ -201,7 +201,7 @@ resource "aws_rds_cluster_instance" "cluster_instance_n" {
201201
count = "${var.replica_scale_enabled ? var.replica_scale_min : var.replica_count}"
202202
engine = "${var.engine}"
203203
engine_version = "${var.engine-version}"
204-
identifier = "${var.identifier_prefix != "" ? format("%s-node-%d", var.identifier_prefix, count.index + 1) : format("%s-aurora-node-%d", var.envname, count.index + 1)}"
204+
identifier = "${format("%s-node-%d", var.identifier_prefix, count.index + 1)}"
205205
cluster_identifier = "${aws_rds_cluster.default.id}"
206206
instance_class = "${var.instance_type}"
207207
publicly_accessible = "${var.publicly_accessible}"
@@ -214,15 +214,12 @@ resource "aws_rds_cluster_instance" "cluster_instance_n" {
214214
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
215215
promotion_tier = "${count.index + 1}"
216216

217-
tags {
218-
envname = "${var.envname}"
219-
envtype = "${var.envtype}"
220-
}
217+
tags = "${merge(var.tags, map("Name", format("%s", var.identifier_prefix)))}"
221218
}
222219

223220
// Create DB Cluster
224221
resource "aws_rds_cluster" "default" {
225-
cluster_identifier = "${var.identifier_prefix != "" ? format("%s-cluster", var.identifier_prefix) : format("%s-aurora-cluster", var.envname)}"
222+
cluster_identifier = "${format("%s-cluster", var.identifier_prefix)}"
226223
availability_zones = ["${var.azs}"]
227224
engine = "${var.engine}"
228225

@@ -266,7 +263,7 @@ data "aws_iam_policy_document" "monitoring-rds-assume-role-policy" {
266263

267264
resource "aws_iam_role" "rds-enhanced-monitoring" {
268265
count = "${var.monitoring_interval > 0 ? 1 : 0}"
269-
name = "rds-enhanced-monitoring-${var.envname}"
266+
name = "rds-enhanced-monitoring-${var.identifier_prefix}"
270267
assume_role_policy = "${data.aws_iam_policy_document.monitoring-rds-assume-role-policy.json}"
271268
}
272269

tests/terraform/test-mysql-56.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ resource "aws_sns_topic" "db_alarms_56" {
55
module "aurora_db_56" {
66
source = "../.."
77
name = "test-aurora-db-56"
8-
envname = "test56"
9-
envtype = "test"
108
subnets = ["${module.vpc.private_subnets}"]
119
azs = ["${module.vpc.availability_zones}"]
1210
replica_count = "1"
@@ -23,6 +21,10 @@ module "aurora_db_56" {
2321
cw_sns_topic = "${aws_sns_topic.db_alarms_56.id}"
2422
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_56_parameter_group.id}"
2523
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_56_parameter_group.id}"
24+
tags = {
25+
envname = "test-57-autoscaling"
26+
envtype = "test"
27+
}
2628
}
2729

2830
resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {

tests/terraform/test-mysql-57-autoscaling.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module "aurora_db_57_autoscaling" {
77
engine = "aurora-mysql"
88
engine-version = "5.7.12"
99
name = "test-aurora-db-57-autoscaling"
10-
envname = "test-57-autoscaling"
11-
envtype = "test"
1210
subnets = ["${module.vpc.private_subnets}"]
1311
azs = ["${module.vpc.availability_zones}"]
1412
security_groups = ["${aws_security_group.allow_all.id}"]
@@ -30,6 +28,10 @@ module "aurora_db_57_autoscaling" {
3028
replica_scale_cpu = "70"
3129
replica_scale_in_cooldown = "300"
3230
replica_scale_out_cooldown = "300"
31+
tags = {
32+
envname = "test-57-autoscaling"
33+
envtype = "test"
34+
}
3335
}
3436

3537
resource "aws_db_parameter_group" "aurora_db_57_autoscaling_parameter_group" {

tests/terraform/test-mysql-57.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module "aurora_db_57" {
77
engine = "aurora-mysql"
88
engine-version = "5.7.12"
99
name = "test-aurora-db-57"
10-
envname = "test-57"
11-
envtype = "test"
1210
subnets = ["${module.vpc.private_subnets}"]
1311
azs = ["${module.vpc.availability_zones}"]
1412
replica_count = "1"
@@ -25,6 +23,10 @@ module "aurora_db_57" {
2523
cw_sns_topic = "${aws_sns_topic.db_alarms.id}"
2624
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_57_parameter_group.id}"
2725
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_57_cluster_parameter_group.id}"
26+
tags = {
27+
envname = "test-57-autoscaling"
28+
envtype = "test"
29+
}
2830
}
2931

3032
resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {

tests/terraform/test-postgres.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ module "aurora_db_postgres96" {
77
engine = "aurora-postgresql"
88
engine-version = "9.6.6"
99
name = "test-aurora-db-postgres96"
10-
envname = "test-pg96"
11-
envtype = "test"
1210
subnets = ["${module.vpc.private_subnets}"]
1311
azs = ["${module.vpc.availability_zones}"]
1412
replica_count = "1"
@@ -25,6 +23,10 @@ module "aurora_db_postgres96" {
2523
cw_sns_topic = "${aws_sns_topic.db_alarms_postgres96.id}"
2624
db_parameter_group_name = "${aws_db_parameter_group.aurora_db_postgres96_parameter_group.id}"
2725
db_cluster_parameter_group_name = "${aws_rds_cluster_parameter_group.aurora_cluster_postgres96_parameter_group.id}"
26+
tags = {
27+
envname = "test-57-autoscaling"
28+
envtype = "test"
29+
}
2830
}
2931

3032
resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {

variables.tf

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,8 @@ variable "subnets" {
88
description = "List of subnet IDs to use"
99
}
1010

11-
variable "envname" {
12-
type = "string"
13-
description = "Environment name (eg,test, stage or prod)"
14-
}
15-
16-
variable "envtype" {
17-
type = "string"
18-
description = "Environment type (eg,prod or nonprod)"
19-
}
20-
2111
variable "identifier_prefix" {
2212
type = "string"
23-
default = ""
2413
description = "Prefix for cluster and instance identifier"
2514
}
2615

@@ -217,3 +206,9 @@ variable "replica_scale_out_cooldown" {
217206
default = "300"
218207
description = "Cooldown in seconds before allowing further scaling operations after a scale out"
219208
}
209+
210+
variable "tags" {
211+
description = "A mapping of tags to assign to the resource"
212+
default = {}
213+
type = "map"
214+
}

0 commit comments

Comments
 (0)