Skip to content

Commit

Permalink
Adding configurable maximum subnet variable and more descriptive tagg…
Browse files Browse the repository at this point in the history
…ing (#29)

* Updating to use supplied availablity zones for cidr calculation

* Updating to use supplied availablity zones for cidr calculation

* Revert "Updating to use supplied availablity zones for cidr calculation"

This reverts commit ad782e8.

* Adding abilityt to control az count

* Adding ability to control az count

* Adding ability to control az count

* Adding ability to control az count

* Adding ability to control az count

* Adding ability to control az count

* Adding ability to control az count

* Adding tags to nat resources

* Updating naming of subnets

* Formatting fixes

* Formatting fixes

* Cleanup from pull request review

* Cleanup from pull request review

* Adding attributes to label and description to max_subnets variable

* Adding attributes to label and description to max_subnets variable

* Adding attributes to label and description to max_subnets variable
  • Loading branch information
dcowan-vestmark authored and aknysh committed May 14, 2018
1 parent bb6d88f commit ca5be43
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
11 changes: 11 additions & 0 deletions nat.tf
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
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}"
}

resource "aws_eip" "default" {
count = "${local.nat_gateways_count}"
vpc = true
tags = "${module.private_label.tags}"

lifecycle {
create_before_destroy = true
Expand All @@ -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
Expand Down
15 changes: 10 additions & 5 deletions private.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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)}"
Expand Down
15 changes: 10 additions & 5 deletions public.tf
Original file line number Diff line number Diff line change
@@ -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" {
Expand All @@ -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)}"
Expand Down
5 changes: 5 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -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`)"
Expand Down

0 comments on commit ca5be43

Please sign in to comment.