generated from dxw/terraform-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* This calculates the IP range for the 'private' and 'public' networks within the VPC. It does this by initially splitting the given vpc CIDR block into 2, and then calcalting the 'nebits' so that individual '/24' ranges can be calulated for subnets. * A subnet for each given availability zone will be created, in both the 'private' and 'public' ranges. * A public route table will be created, along with an Internet Gateway, and a 0/0 route created for the gateway. The subnets within the 'public' range will then be associated with this route table, allowing any resource launched within them to access the internet. * A private route table will also be created. If 'public' networking is enabled, a NAT gateway will be created in one of the public subnets, and a route created to allow resources within the 'private' subnets access to the internet. * Example subnets that will be created: ``` infrastructure_vpc_cidr_block = "10.0.0.0/16" infrastructure_vpc_network_enable_public = true infrastructure_vpc_network_enable_private = true infrastructure_vpc_network_availability_zones = ["a", "b", "c"] public subnets (Allocated "10.0.0.0/17"): [ "10.0.0.0/24", "10.0.1.0/24", "10.0.2.0/24" ] private subnets (Allocated "10.0.128.0/17"): [ "10.0.128.0/24", "10.0.129.0/24", "10.0.130.0/24" ] ```
- Loading branch information
Showing
6 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
resource "aws_route_table" "infrastructure_private" { | ||
count = local.infrastructure_vpc_network_enable_private ? 1 : 0 | ||
|
||
vpc_id = aws_vpc.infrastructure[0].id | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-private" | ||
} | ||
} | ||
|
||
resource "aws_subnet" "infrastructure_private" { | ||
for_each = local.infrastructure_vpc_network_enable_private ? local.infrastructure_vpc_network_availability_zones : [] | ||
|
||
vpc_id = aws_vpc.infrastructure[0].id | ||
availability_zone = "${local.aws_region}-${each.value}" | ||
|
||
cidr_block = cidrsubnet( | ||
local.infrastructure_vpc_network_private_cidr, | ||
local.infrastructure_vpc_network_private_cidr_newbits, | ||
index(local.infrastructure_vpc_network_availability_zones, each.value) | ||
) | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-private-${each.value}" | ||
} | ||
} | ||
|
||
resource "aws_route_table_association" "infrastructure_private" { | ||
for_each = local.infrastructure_vpc_network_enable_private ? local.infrastructure_vpc_network_availability_zones : [] | ||
|
||
subnet_id = aws_subnet.infrastructure_private[each.value].id | ||
route_table_id = aws_route_table.infrastructure_private[0] | ||
} | ||
|
||
resource "aws_eip" "infrastructure_nat" { | ||
count = local.infrastructure_vpc_network_enable_private && local.infrastructure_vpc_network_enable_public ? 1 : 0 | ||
|
||
domain = "vpc" | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-nat" | ||
} | ||
} | ||
|
||
resource "aws_nat_gateway" "infrastructure" { | ||
count = local.infrastructure_vpc_network_enable_private && local.infrastructure_vpc_network_enable_public ? 1 : 0 | ||
|
||
allocation_id = aws_eip.infrastructure_nat[0].id | ||
subnet_id = aws_subnet.infrastructure_public[0].id | ||
depends_on = [aws_internet_gateway.infrastructure_public] | ||
} | ||
|
||
resource "aws_route" "private_nat_gateway" { | ||
count = local.infrastructure_vpc_network_enable_private && local.infrastructure_vpc_network_enable_public ? 1 : 0 | ||
|
||
route_table_id = aws_route_table.infrastructure_private[0] | ||
destination_cidr_block = "0.0.0.0/0" | ||
nat_gateway_id = aws_nat_gateway.infrastructure[0].id | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
resource "aws_route_table" "infrastructure_public" { | ||
count = local.infrastructure_vpc_network_enable_public ? 1 : 0 | ||
|
||
vpc_id = aws_vpc.infrastructure[0].id | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-public" | ||
} | ||
} | ||
|
||
resource "aws_internet_gateway" "infrastructure_public" { | ||
count = local.infrastructure_vpc_network_enable_public ? 1 : 0 | ||
|
||
vpc_id = aws_vpc.infrastructure[0].id | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-public" | ||
} | ||
} | ||
|
||
resource "aws_route" "infrustructure_public_internet_gateway" { | ||
count = local.infrastructure_vpc_network_enable_public ? 1 : 0 | ||
|
||
route_table_id = aws_route_table.infrastructure_public[0].id | ||
destination_cidr_block = "0.0.0.0/0" | ||
gateway_id = aws_internet_gateway.infrastructure_public[0].id | ||
} | ||
|
||
resource "aws_subnet" "infrastructure_public" { | ||
for_each = local.infrastructure_vpc_network_enable_public ? local.infrastructure_vpc_network_availability_zones : [] | ||
|
||
vpc_id = aws_vpc.infrastructure[0].id | ||
availability_zone = "${local.aws_region}-${each.value}" | ||
|
||
cidr_block = cidrsubnet( | ||
local.infrastructure_vpc_network_public_cidr, | ||
local.infrastructure_vpc_network_public_cidr_newbits, | ||
index(local.infrastructure_vpc_network_availability_zones, each.value) | ||
) | ||
|
||
tags = { | ||
Name = "${local.resource_prefix}-infrastructure-public-${each.value}" | ||
} | ||
} | ||
|
||
resource "aws_route_table_association" "infrastructure_public" { | ||
for_each = local.infrastructure_vpc_network_enable_public ? local.infrastructure_vpc_network_availability_zones : [] | ||
|
||
subnet_id = aws_subnet.infrastructure_public[each.value].id | ||
route_table_id = aws_route_table.infrastructure_public[0] | ||
} |