Skip to content

Commit

Permalink
Better use of tags as map
Browse files Browse the repository at this point in the history
  • Loading branch information
myoung34 committed May 14, 2018
1 parent 2989a8d commit 6885627
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 67 deletions.
21 changes: 13 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ resource "aws_sns_topic" "db_alarms_56" {
module "aurora_db_56" {
source = "claranet/aurora/aws"
name = "test-aurora-db-56"
envname = "test56"
envtype = "test"
subnets = ["${module.vpc.private_subnets}"]
azs = ["${module.vpc.availability_zones}"]
replica_count = "1"
Expand All @@ -55,6 +53,10 @@ module "aurora_db_56" {
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}"
tags = {
envname = "test-56"
envtype = "test"
}
}
resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
Expand Down Expand Up @@ -82,8 +84,6 @@ module "aurora_db_57" {
engine = "aurora-mysql"
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}"]
replica_count = "1"
Expand All @@ -100,6 +100,10 @@ module "aurora_db_57" {
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}"
tags = {
envname = "test-57"
envtype = "test"
}
}
resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
Expand All @@ -126,8 +130,6 @@ module "aurora_db_postgres96" {
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}"]
replica_count = "1"
Expand All @@ -144,6 +146,10 @@ module "aurora_db_postgres96" {
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}"
tags = {
envname = "test-pg96"
envtype = "test"
}
}
resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {
Expand Down Expand Up @@ -177,8 +183,7 @@ resource "aws_rds_cluster_parameter_group" "aurora_cluster_postgres96_parameter_
| db_parameter_group_name | The name of a DB parameter group to use | string | `default.aurora5.6` | no |
| engine | Aurora database engine type, currently aurora, aurora-mysql or aurora-postgresql | string | `aurora` | no |
| engine-version | Aurora database engine version. | string | `5.6.10a` | no |
| envname | Environment name (eg,test, stage or prod) | string | - | yes |
| envtype | Environment type (eg,prod or nonprod) | string | - | yes |
| tags | Tags to attach to resources | map | - | no |
| 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 |
| identifier_prefix | Prefix for cluster and instance identifier | string | `` | no |
| instance_type | Instance type to use | string | `db.t2.small` | no |
Expand Down
77 changes: 37 additions & 40 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,28 @@
* }
*
* module "aurora_db_56" {
* source = "../.."
* name = "test-aurora-db-56"
* envname = "test56"
* envtype = "test"
* subnets = ["${module.vpc.private_subnets}"]
* azs = ["${module.vpc.availability_zones}"]
* replica_count = "1"
* security_groups = ["${aws_security_group.allow_all.id}"]
* instance_type = "db.t2.medium"
* 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}"
* source = "../.."
* name = "test-aurora-db-56"
* subnets = ["${module.vpc.private_subnets}"]
* azs = ["${module.vpc.availability_zones}"]
* replica_count = "1"
* security_groups = ["${aws_security_group.allow_all.id}"]
* instance_type = "db.t2.medium"
* 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}"
* tags = {
* envname = "test-56"
* envtype = "test"
* }
* }
*
* resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
Expand All @@ -82,8 +84,6 @@
* engine = "aurora-mysql"
* 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}"]
* replica_count = "1"
Expand All @@ -100,6 +100,10 @@
* 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}"
* tags = {
* envname = "test-57"
* envtype = "test"
* }
* }
*
* resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
Expand All @@ -126,8 +130,6 @@
* 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}"]
* replica_count = "1"
Expand All @@ -144,6 +146,10 @@
* 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}"
* tags = {
* envname = "test-pg96"
* envtype = "test"
* }
* }
*
* resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {
Expand All @@ -166,15 +172,12 @@ resource "aws_db_subnet_group" "main" {
description = "Group of DB subnets"
subnet_ids = ["${var.subnets}"]

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
}
tags = "${merge(var.tags, map("Name", format("%s", var.identifier_prefix)))}"
}

// Create single DB instance
resource "aws_rds_cluster_instance" "cluster_instance_0" {
identifier = "${var.identifier_prefix != "" ? format("%s-node-0", var.identifier_prefix) : format("%s-aurora-node-0", var.envname)}"
identifier = "${format("%s-node-0", var.identifier_prefix)}"
cluster_identifier = "${aws_rds_cluster.default.id}"
engine = "${var.engine}"
engine_version = "${var.engine-version}"
Expand All @@ -189,10 +192,7 @@ resource "aws_rds_cluster_instance" "cluster_instance_0" {
auto_minor_version_upgrade = "${var.auto_minor_version_upgrade}"
promotion_tier = "0"

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
}
tags = "${merge(var.tags, map("Name", format("%s", var.identifier_prefix)))}"
}

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

tags {
envname = "${var.envname}"
envtype = "${var.envtype}"
}
tags = "${merge(var.tags, map("Name", format("%s", var.identifier_prefix)))}"
}

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

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

resource "aws_iam_role" "rds-enhanced-monitoring" {
count = "${var.monitoring_interval > 0 ? 1 : 0}"
name = "rds-enhanced-monitoring-${var.envname}"
name = "rds-enhanced-monitoring-${var.identifier_prefix}"
assume_role_policy = "${data.aws_iam_policy_document.monitoring-rds-assume-role-policy.json}"
}

Expand Down
6 changes: 4 additions & 2 deletions tests/terraform/test-mysql-56.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ resource "aws_sns_topic" "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}"]
replica_count = "1"
Expand All @@ -23,6 +21,10 @@ module "aurora_db_56" {
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}"
tags = {
envname = "test-56"
envtype = "test"
}
}

resource "aws_db_parameter_group" "aurora_db_56_parameter_group" {
Expand Down
6 changes: 4 additions & 2 deletions tests/terraform/test-mysql-57-autoscaling.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module "aurora_db_57_autoscaling" {
engine = "aurora-mysql"
engine-version = "5.7.12"
name = "test-aurora-db-57-autoscaling"
envname = "test-57-autoscaling"
envtype = "test"
subnets = ["${module.vpc.private_subnets}"]
azs = ["${module.vpc.availability_zones}"]
security_groups = ["${aws_security_group.allow_all.id}"]
Expand All @@ -30,6 +28,10 @@ module "aurora_db_57_autoscaling" {
replica_scale_cpu = "70"
replica_scale_in_cooldown = "300"
replica_scale_out_cooldown = "300"
tags = {
envname = "test-57-autoscaling"
envtype = "test"
}
}

resource "aws_db_parameter_group" "aurora_db_57_autoscaling_parameter_group" {
Expand Down
6 changes: 4 additions & 2 deletions tests/terraform/test-mysql-57.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module "aurora_db_57" {
engine = "aurora-mysql"
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}"]
replica_count = "1"
Expand All @@ -25,6 +23,10 @@ module "aurora_db_57" {
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}"
tags = {
envname = "test-57"
envtype = "test"
}
}

resource "aws_db_parameter_group" "aurora_db_57_parameter_group" {
Expand Down
6 changes: 4 additions & 2 deletions tests/terraform/test-postgres.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ module "aurora_db_postgres96" {
engine = "aurora-postgresql"
engine-version = "9.6.6"
name = "test-aurora-db-postgres96"
envname = "test-pg96"
envtype = "test"
subnets = ["${module.vpc.private_subnets}"]
azs = ["${module.vpc.availability_zones}"]
replica_count = "1"
Expand All @@ -25,6 +23,10 @@ module "aurora_db_postgres96" {
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}"
tags = {
envname = "test-pg96"
envtype = "test"
}
}

resource "aws_db_parameter_group" "aurora_db_postgres96_parameter_group" {
Expand Down
17 changes: 6 additions & 11 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,8 @@ variable "subnets" {
description = "List of subnet IDs to use"
}

variable "envname" {
type = "string"
description = "Environment name (eg,test, stage or prod)"
}

variable "envtype" {
type = "string"
description = "Environment type (eg,prod or nonprod)"
}

variable "identifier_prefix" {
type = "string"
default = ""
description = "Prefix for cluster and instance identifier"
}

Expand Down Expand Up @@ -217,3 +206,9 @@ variable "replica_scale_out_cooldown" {
default = "300"
description = "Cooldown in seconds before allowing further scaling operations after a scale out"
}

variable "tags" {
description = "A mapping of tags to assign to the resource"
default = {}
type = "map"
}

0 comments on commit 6885627

Please sign in to comment.