From 655b8fce7236dd3162b75c64b2ce48f5e05ce73b Mon Sep 17 00:00:00 2001 From: Luca Venturelli Date: Mon, 7 Aug 2017 18:32:27 +0200 Subject: [PATCH] fixing subnet configuration (#16) * fixing subnet configuration * adding readme and fixing route_table count --- README.md | 3 ++- subnets/main.tf | 2 +- subnets/variables.tf | 3 +++ vpc/main.tf | 5 +++++ 4 files changed, 11 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bc47867..a44afc3 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,8 @@ Creates a number of subnets and divides them in different parts based on the inp * [`project`]: String(required): the name of the project these subnets belong to * [`environment`]: String(required): the name of the environment these subnets belong to (prod,stag,dev) * [`num_subnets`]: String(optional): default to 3. the number of subnets we want to create + * [`route_tables`]: List(optional): the list of route tables to associate to the created subnet. This will associate the route table to the created subnet sequentially. If the subnet number is greater than the number of route tables, the route table will be selected using a standard mod algorithm + * [`num_route_tables`]: String(optional): default to 0. the number of route tables passed in route_tables. NOTE: this is due to a bug in terraform that cannot iterate over count param ### Output * [`ids`]: List: the ids of the subnets created @@ -77,7 +79,6 @@ It will also create the required route tables for the private subnets. The priva * [`netnum_private_app`]: String(optional): default to 20. First number of subnet to start of for private_app subnets * [`netnum_private_db`]: String(optional): default to 30. First number of subnet to start of for private_db subnets * [`tags`]: Map(optional): optional tags - * [`route_tables`]: List(optional): the list of route tables to associate to the created subnet. This will associate the route table to the created subnet sequentially. If the subnet number is greater than the number of route tables, the route table will be selected using a standard mod algorithm ### Output: diff --git a/subnets/main.tf b/subnets/main.tf index 2539def..1294fd3 100644 --- a/subnets/main.tf +++ b/subnets/main.tf @@ -16,7 +16,7 @@ resource "aws_subnet" "subnets" { } resource "aws_route_table_association" "subnet_association" { - count = "${length(var.route_tables) >0 ? "${var.num_subnets}" : 0 }" + count = "${var.num_route_tables >0 ? "${var.num_subnets}" : 0 }" subnet_id = "${element(aws_subnet.subnets.*.id, count.index)}" route_table_id = "${element(var.route_tables, count.index)}" } diff --git a/subnets/variables.tf b/subnets/variables.tf index 8c61777..1244fbc 100644 --- a/subnets/variables.tf +++ b/subnets/variables.tf @@ -43,3 +43,6 @@ variable "route_tables" { type = "list" default = [] } +variable "num_route_tables" { + default = "0" +} diff --git a/vpc/main.tf b/vpc/main.tf index cd109cf..04bb952 100644 --- a/vpc/main.tf +++ b/vpc/main.tf @@ -23,6 +23,7 @@ module "public_nat-bastion_subnets" { project = "${var.project}" tags = "${var.tags}" route_tables = "${aws_route_table.public.*.id}" + num_route_tables = "1" } module "public_lb_subnets" { @@ -37,6 +38,7 @@ module "public_lb_subnets" { project = "${var.project}" tags = "${var.tags}" route_tables = "${aws_route_table.public.*.id}" + num_route_tables = "1" } module "private_app_subnets" { @@ -51,6 +53,7 @@ module "private_app_subnets" { project = "${var.project}" tags = "${var.tags}" route_tables = "${aws_route_table.private.*.id}" + num_route_tables = "${var.number_private_rt}" } module "private_db_subnets" { @@ -65,6 +68,7 @@ module "private_db_subnets" { project = "${var.project}" tags = "${var.tags}" route_tables = "${aws_route_table.private.*.id}" + num_route_tables = "${var.number_private_rt}" } module "private_management_subnets" { @@ -79,6 +83,7 @@ module "private_management_subnets" { project = "${var.project}" tags = "${var.tags}" route_tables = "${aws_route_table.private.*.id}" + num_route_tables = "${var.number_private_rt}" } # Create internet gateway