From 6610eef6a6d182b5ae6f279f335832b03a9238b8 Mon Sep 17 00:00:00 2001 From: Rupert Broad Date: Thu, 13 Sep 2018 18:13:44 +0200 Subject: [PATCH 1/2] add vars for custom subnet and route table names --- main.tf | 20 ++++++++++---------- variables.tf | 25 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/main.tf b/main.tf index 6b41a57a5..57feaf7bb 100644 --- a/main.tf +++ b/main.tf @@ -77,7 +77,7 @@ resource "aws_route_table" "public" { vpc_id = "${local.vpc_id}" - tags = "${merge(map("Name", format("%s-public", var.name)), var.public_route_table_tags, var.tags)}" + tags = "${merge(map("Name", format("%s-${var.public_subnet_suffix}", var.name)), var.public_route_table_tags, var.tags)}" } resource "aws_route" "public_internet_gateway" { @@ -101,7 +101,7 @@ resource "aws_route_table" "private" { vpc_id = "${local.vpc_id}" - tags = "${merge(map("Name", (var.single_nat_gateway ? "${var.name}-private" : format("%s-private-%s", var.name, element(var.azs, count.index)))), var.private_route_table_tags, var.tags)}" + tags = "${merge(map("Name", (var.single_nat_gateway ? "${var.name}-${var.private_subnet_suffix}" : format("%s-${var.private_subnet_suffix}-%s", var.name, element(var.azs, count.index)))), var.private_route_table_tags, var.tags)}" lifecycle { # When attaching VPN gateways it is common to define aws_vpn_gateway_route_propagation @@ -118,7 +118,7 @@ resource "aws_route_table" "database" { vpc_id = "${local.vpc_id}" - tags = "${merge(var.tags, var.database_route_table_tags, map("Name", "${var.name}-database"))}" + tags = "${merge(var.tags, var.database_route_table_tags, map("Name", "${var.name}-${var.database_subnet_suffix}"))}" } ################# @@ -129,7 +129,7 @@ resource "aws_route_table" "redshift" { vpc_id = "${local.vpc_id}" - tags = "${merge(var.tags, var.redshift_route_table_tags, map("Name", "${var.name}-redshift"))}" + tags = "${merge(var.tags, var.redshift_route_table_tags, map("Name", "${var.name}-${var.redshift_subnet_suffix}"))}" } ################# @@ -140,7 +140,7 @@ resource "aws_route_table" "elasticache" { vpc_id = "${local.vpc_id}" - tags = "${merge(var.tags, var.elasticache_route_table_tags, map("Name", "${var.name}-elasticache"))}" + tags = "${merge(var.tags, var.elasticache_route_table_tags, map("Name", "${var.name}-${var.elasticache_subnet_suffix}"))}" } ################# @@ -165,7 +165,7 @@ resource "aws_subnet" "public" { availability_zone = "${element(var.azs, count.index)}" map_public_ip_on_launch = "${var.map_public_ip_on_launch}" - tags = "${merge(map("Name", format("%s-public-%s", var.name, element(var.azs, count.index))), var.public_subnet_tags, var.tags)}" + tags = "${merge(map("Name", format("%s-${var.public_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.public_subnet_tags, var.tags)}" } ################# @@ -178,7 +178,7 @@ resource "aws_subnet" "private" { cidr_block = "${var.private_subnets[count.index]}" availability_zone = "${element(var.azs, count.index)}" - tags = "${merge(map("Name", format("%s-private-%s", var.name, element(var.azs, count.index))), var.private_subnet_tags, var.tags)}" + tags = "${merge(map("Name", format("%s-${var.private_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.private_subnet_tags, var.tags)}" } ################## @@ -191,7 +191,7 @@ resource "aws_subnet" "database" { cidr_block = "${var.database_subnets[count.index]}" availability_zone = "${element(var.azs, count.index)}" - tags = "${merge(map("Name", format("%s-db-%s", var.name, element(var.azs, count.index))), var.database_subnet_tags, var.tags)}" + tags = "${merge(map("Name", format("%s-${var.database_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.database_subnet_tags, var.tags)}" } resource "aws_db_subnet_group" "database" { @@ -214,7 +214,7 @@ resource "aws_subnet" "redshift" { cidr_block = "${var.redshift_subnets[count.index]}" availability_zone = "${element(var.azs, count.index)}" - tags = "${merge(map("Name", format("%s-redshift-%s", var.name, element(var.azs, count.index))), var.redshift_subnet_tags, var.tags)}" + tags = "${merge(map("Name", format("%s-${var.redshift_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.redshift_subnet_tags, var.tags)}" } resource "aws_redshift_subnet_group" "redshift" { @@ -237,7 +237,7 @@ resource "aws_subnet" "elasticache" { cidr_block = "${var.elasticache_subnets[count.index]}" availability_zone = "${element(var.azs, count.index)}" - tags = "${merge(map("Name", format("%s-elasticache-%s", var.name, element(var.azs, count.index))), var.elasticache_subnet_tags, var.tags)}" + tags = "${merge(map("Name", format("%s-${var.elasticache_subnet_suffix}-%s", var.name, element(var.azs, count.index))), var.elasticache_subnet_tags, var.tags)}" } resource "aws_elasticache_subnet_group" "elasticache" { diff --git a/variables.tf b/variables.tf index c21d2872b..c9e870659 100644 --- a/variables.tf +++ b/variables.tf @@ -28,6 +28,31 @@ variable "instance_tenancy" { default = "default" } +variable "public_subnet_suffix" { + description = "Suffix to append to public subnets name" + default = "public" +} + +variable "private_subnet_suffix" { + description = "Suffix to append to private subnets name" + default = "private" +} + +variable "database_subnet_suffix" { + description = "Suffix to append to database subnets name" + default = "database" +} + +variable "redshift_subnet_suffix" { + description = "Suffix to append to redshift subnets name" + default = "redshift" +} + +variable "elasticache_subnet_suffix" { + description = "Suffix to append to elasticache subnets name" + default = "elasticache" +} + variable "public_subnets" { description = "A list of public subnets inside the VPC" default = [] From 9b8f90fcdbe050ddcef01b04e88dbc752bfb19de Mon Sep 17 00:00:00 2001 From: Rupert Broad Date: Thu, 13 Sep 2018 18:45:01 +0200 Subject: [PATCH 2/2] revert db suffix to "db" --- variables.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/variables.tf b/variables.tf index c9e870659..7b1e6ae16 100644 --- a/variables.tf +++ b/variables.tf @@ -40,7 +40,7 @@ variable "private_subnet_suffix" { variable "database_subnet_suffix" { description = "Suffix to append to database subnets name" - default = "database" + default = "db" } variable "redshift_subnet_suffix" {