diff --git a/README.md b/README.md index fb25c25..a17191d 100644 --- a/README.md +++ b/README.md @@ -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" @@ -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" { @@ -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" @@ -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" { @@ -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" @@ -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" { @@ -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 | diff --git a/main.tf b/main.tf index 34df752..e9da44b 100644 --- a/main.tf +++ b/main.tf @@ -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" { @@ -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" @@ -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" { @@ -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" @@ -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" { @@ -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}" @@ -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 @@ -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}" @@ -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}" @@ -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}" } diff --git a/tests/terraform/test-mysql-56.tf b/tests/terraform/test-mysql-56.tf index eca9ac2..8b50691 100644 --- a/tests/terraform/test-mysql-56.tf +++ b/tests/terraform/test-mysql-56.tf @@ -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" @@ -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" { diff --git a/tests/terraform/test-mysql-57-autoscaling.tf b/tests/terraform/test-mysql-57-autoscaling.tf index 28d1c9e..35ce903 100644 --- a/tests/terraform/test-mysql-57-autoscaling.tf +++ b/tests/terraform/test-mysql-57-autoscaling.tf @@ -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}"] @@ -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" { diff --git a/tests/terraform/test-mysql-57.tf b/tests/terraform/test-mysql-57.tf index 9c8eb9b..f1f90f0 100644 --- a/tests/terraform/test-mysql-57.tf +++ b/tests/terraform/test-mysql-57.tf @@ -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" @@ -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" { diff --git a/tests/terraform/test-postgres.tf b/tests/terraform/test-postgres.tf index 13ed1ec..3081ed6 100644 --- a/tests/terraform/test-postgres.tf +++ b/tests/terraform/test-postgres.tf @@ -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" @@ -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" { diff --git a/variables.tf b/variables.tf index 2666530..3bc2bef 100644 --- a/variables.tf +++ b/variables.tf @@ -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" } @@ -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" +}