Browse Source

make private network configurable

Chris Mague 6 years ago
parent
commit
5e0263dfe3
4 changed files with 50 additions and 41 deletions
  1. 4 0
      main.tf
  2. 35 35
      private_subnets.tf
  3. 7 6
      test/main.tf
  4. 4 0
      variables.tf

+ 4 - 0
main.tf

@@ -3,3 +3,7 @@ provider "aws" {
   profile = var.profile
 }
 
+# Allow us to disable the private networks
+locals {
+  count_private = (var.enable-private == true ? length(var.vpc-azs) : 0)
+}

+ 35 - 35
private_subnets.tf

@@ -1,35 +1,35 @@
-#resource "aws_subnet" "private" {
-#  count             = "${length(var.vpc-azs)}"
-#  vpc_id            = "${aws_vpc.vpc.id}"
-#  cidr_block        = "${cidrsubnet(var.vpc-cidr, length(var.vpc-azs) * 2, count.index + length(var.vpc-azs))}"
-#  availability_zone = "${var.vpc-azs[count.index]}"
-#  tags              = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
-#}
-#
-#resource "aws_eip" "private-nat-eip" {
-#  count      = "${length(var.vpc-azs)}"
-#  vpc        = true
-#  tags       = merge({ Name = "${var.vpc-name}-nat-eip-${count.index}" }, var.common-tags)
-#  depends_on = ["aws_internet_gateway.vpc"]
-#}
-#
-#resource "aws_nat_gateway" "private" {
-#  count         = "${length(var.vpc-azs)}"
-#  allocation_id = "${element(aws_eip.private-nat-eip.*.id, count.index)}"
-#  subnet_id     = "${element(aws_subnet.public.*.id, count.index)}"
-#  depends_on    = ["aws_internet_gateway.vpc"]
-#  tags          = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
-#}
-#
-#resource "aws_route" "nat_gateway" {
-#  count                  = "${length(var.vpc-azs)}"
-#  route_table_id         = "${element(aws_route_table.private.*.id, count.index)}"
-#  destination_cidr_block = "0.0.0.0/0"
-#  nat_gateway_id         = "${element(aws_nat_gateway.private.*.id, count.index)}"
-#}
-#
-#resource "aws_route_table" "private" {
-#  count  = "${length(var.vpc-azs)}"
-#  vpc_id = "${aws_vpc.vpc.id}"
-#  tags   = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
-#}
+resource "aws_subnet" "private" {
+  count             = local.count_private
+  vpc_id            = "${aws_vpc.vpc.id}"
+  cidr_block        = "${cidrsubnet(var.vpc-cidr, length(var.vpc-azs) * 2, count.index + length(var.vpc-azs))}"
+  availability_zone = "${var.vpc-azs[count.index]}"
+  tags              = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
+}
+
+resource "aws_eip" "private-nat-eip" {
+  count      = local.count_private
+  vpc        = true
+  tags       = merge({ Name = "${var.vpc-name}-nat-eip-${count.index}" }, var.common-tags)
+  depends_on = ["aws_internet_gateway.vpc"]
+}
+
+resource "aws_nat_gateway" "private" {
+  count         = local.count_private
+  allocation_id = "${element(aws_eip.private-nat-eip.*.id, count.index)}"
+  subnet_id     = "${element(aws_subnet.public.*.id, count.index)}"
+  depends_on    = ["aws_internet_gateway.vpc"]
+  tags          = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
+}
+
+resource "aws_route" "nat_gateway" {
+  count                  = local.count_private
+  route_table_id         = "${element(aws_route_table.private.*.id, count.index)}"
+  destination_cidr_block = "0.0.0.0/0"
+  nat_gateway_id         = "${element(aws_nat_gateway.private.*.id, count.index)}"
+}
+
+resource "aws_route_table" "private" {
+  count  = local.count_private
+  vpc_id = "${aws_vpc.vpc.id}"
+  tags   = merge({ Name = "${var.vpc-name}-private-${element(var.vpc-azs, count.index)}" }, var.common-tags)
+}

+ 7 - 6
test/main.tf

@@ -4,12 +4,13 @@ provider "aws" {
 }
 
 module "awx" {
-  source   = "../"
-  profile  = "redislabs"
-  region   = "us-east-1"
-  vpc-name = "rltest1"
-  vpc-cidr = "10.0.0.0/8"
-  vpc-azs  = ["us-east-1a", "us-east-1b"]
+  source         = "../"
+  profile        = "redislabs"
+  region         = "us-east-1"
+  vpc-name       = "rltest1"
+  vpc-cidr       = "10.0.0.0/8"
+  enable-private = false
+  vpc-azs        = ["us-east-1a", "us-east-1b"]
   common-tags = {
     "Owner"   = "maguec"
     "Project" = "example"

+ 4 - 0
variables.tf

@@ -34,3 +34,7 @@ variable "common-tags" {
   description = "Tags that go everywhere"
 }
 
+variable "enable-private" {
+  description = "Enable Private Networks"
+  default     = true
+}