diff --git a/nat.tf b/nat.tf index fa7e12c3..ae4fabe1 100644 --- a/nat.tf +++ b/nat.tf @@ -1,3 +1,12 @@ +module "nat_label" { + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3" + namespace = "${var.namespace}" + stage = "${var.stage}" + name = "${var.name}" + delimiter = "${var.delimiter}" + tags = "${var.tags}" +} + locals { nat_gateways_count = "${var.nat_gateway_enabled == "true" ? length(var.availability_zones) : 0}" } @@ -5,6 +14,7 @@ locals { resource "aws_eip" "default" { count = "${local.nat_gateways_count}" vpc = true + tags = "${module.private_label.tags}" lifecycle { create_before_destroy = true @@ -15,6 +25,7 @@ resource "aws_nat_gateway" "default" { count = "${local.nat_gateways_count}" allocation_id = "${element(aws_eip.default.*.id, count.index)}" subnet_id = "${element(aws_subnet.public.*.id, count.index)}" + tags = "${module.nat_label.tags}" lifecycle { create_before_destroy = true diff --git a/private.tf b/private.tf index 9cfe9bc7..12c95e93 100644 --- a/private.tf +++ b/private.tf @@ -9,17 +9,22 @@ module "private_label" { } module "private_subnet_label" { - source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3" - namespace = "${var.namespace}" - stage = "${var.stage}" - name = "private" + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3" + namespace = "${var.namespace}" + stage = "${var.stage}" + name = "subnet" + attributes = ["private"] +} + +locals { + private_subnet_count = "${var.max_subnet_count == 0 ? length(data.aws_availability_zones.available.names) : var.max_subnet_count}" } resource "aws_subnet" "private" { count = "${length(var.availability_zones)}" vpc_id = "${data.aws_vpc.default.id}" availability_zone = "${element(var.availability_zones, count.index)}" - cidr_block = "${cidrsubnet(signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block, ceil(log(length(data.aws_availability_zones.available.names) * 2, 2)), count.index)}" + cidr_block = "${cidrsubnet(signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block, ceil(log(local.private_subnet_count * 2, 2)), count.index)}" tags = { "Name" = "${module.private_subnet_label.id}${var.delimiter}${replace(element(var.availability_zones, count.index),"-",var.delimiter)}" diff --git a/public.tf b/public.tf index 9949f7d2..20ca5bca 100644 --- a/public.tf +++ b/public.tf @@ -1,8 +1,9 @@ module "public_subnet_label" { - source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3" - namespace = "${var.namespace}" - stage = "${var.stage}" - name = "public" + source = "git::https://github.com/cloudposse/terraform-null-label.git?ref=tags/0.3.3" + namespace = "${var.namespace}" + stage = "${var.stage}" + name = "${var.name}" + attributes = ["public"] } module "public_label" { @@ -15,11 +16,15 @@ module "public_label" { tags = "${var.tags}" } +locals { + public_subnet_count = "${var.max_subnet_count == 0 ? length(data.aws_availability_zones.available.names) : var.max_subnet_count}" +} + resource "aws_subnet" "public" { count = "${length(var.availability_zones)}" vpc_id = "${data.aws_vpc.default.id}" availability_zone = "${element(var.availability_zones, count.index)}" - cidr_block = "${cidrsubnet(signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block, ceil(log(length(data.aws_availability_zones.available.names) * 2, 2)), length(data.aws_availability_zones.available.names) + count.index)}" + cidr_block = "${cidrsubnet(signum(length(var.cidr_block)) == 1 ? var.cidr_block : data.aws_vpc.default.cidr_block, ceil(log(local.public_subnet_count * 2, 2)), local.public_subnet_count + count.index)}" tags = { "Name" = "${module.public_subnet_label.id}${var.delimiter}${replace(element(var.availability_zones, count.index),"-",var.delimiter)}" diff --git a/variables.tf b/variables.tf index d5af49a1..23bdcbc8 100644 --- a/variables.tf +++ b/variables.tf @@ -36,6 +36,11 @@ variable "region" { description = "AWS Region (e.g. `us-east-1`)" } +variable "max_subnet_count" { + default = 0 + description = "Sets the maximum amount of subnets to deploy. 0 will deploy a subnet for every availablility zone within the region" +} + variable "vpc_id" { type = "string" description = "VPC ID where subnets will be created (e.g. `vpc-aceb2723`)"