terraform.aws-baseline-infra/modules/networking/vpc_subnets/vpc.tf

193 lines
6.3 KiB
Terraform
Raw Normal View History

2021-01-28 15:04:01 +08:00
data "aws_availability_zones" "available-az" {
state = "available"
}
locals {
// subnet_start = cidrsubnets(var.vpc-cidr, 1, 1) # divide vpc into 2
# no-az = length(data.aws_availability_zones.available-az.id)
2023-02-21 12:26:31 +08:00
no-az = 2 # hard-coding to 2AZ
vpc-cidr = var.vpc-cidr
total-no-subnets = local.no-az * (var.number-of-private-subnets-per-az + var.number-of-public-subnets-per-az)
2022-12-19 16:13:45 +08:00
# simple-divide = local.total-no-subnets >=8 ? cidrsubnets(local.vpc-cidr, 4,4,4,4,4,4,4,4) : local.total-no-subnets >=6 ? cidrsubnets(local.vpc-cidr, 3,3,3,3,3,3) : local.total-no-subnets >=4 ? cidrsubnets(local.vpc-cidr, 2,2,2,2) : local.total-no-subnets >=2 ? cidrsubnets(local.vpc-cidr, 1,1) : null
2023-02-21 12:26:31 +08:00
simple-divide = local.total-no-subnets >= 12 ? cidrsubnets(local.vpc-cidr, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4) : local.total-no-subnets >= 8 ? cidrsubnets(local.vpc-cidr, 3, 3, 3, 3, 3, 3, 3, 3) : local.total-no-subnets >= 6 ? cidrsubnets(local.vpc-cidr, 3, 3, 3, 3, 3, 3) : local.total-no-subnets >= 4 ? cidrsubnets(local.vpc-cidr, 2, 2, 2, 2) : local.total-no-subnets >= 2 ? cidrsubnets(local.vpc-cidr, 1, 1) : null
public-subnets = slice(local.simple-divide, 0, var.number-of-public-subnets-per-az * local.no-az)
private-subnets = slice(local.simple-divide, var.number-of-public-subnets-per-az * local.no-az, local.total-no-subnets)
2021-01-28 15:04:01 +08:00
}
2023-02-21 12:26:31 +08:00
resource "aws_subnet" "private-subnets" {
count = length(local.private-subnets)
# count = length(var.private-subnet-cidrs)
# count = var.number-of-private-subnets-per-az * length(data.aws_availability_zones.available-az.names)
2023-02-21 12:26:31 +08:00
vpc_id = aws_vpc.vpc.id
2021-01-28 15:04:01 +08:00
availability_zone = element(data.aws_availability_zones.available-az.names, count.index)
# cidr_block = cidrsubnet(local.subnet_start[0], 2, count.index)
# cidr_block = var.private-subnet-cidrs[count.index]
cidr_block = local.private-subnets[count.index]
2021-01-28 15:04:01 +08:00
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-private-${split("-", element(data.aws_availability_zones.available-az.names, count.index))[2]}-${count.index + 1}"
},
2021-01-28 15:04:01 +08:00
)
}
2023-02-21 12:26:31 +08:00
resource "aws_subnet" "public-subnets" {
count = length(local.public-subnets)
# count = length(var.public-subnet-cidrs)
# count = var.number-of-public-subnets-per-az * length(data.aws_availability_zones.available-az.names)
2023-02-21 12:26:31 +08:00
vpc_id = aws_vpc.vpc.id
2021-01-28 15:04:01 +08:00
availability_zone = element(data.aws_availability_zones.available-az.names, count.index)
# cidr_block = cidrsubnet(local.subnet_start[1], 2, count.index)
# cidr_block = var.public-subnet-cidrs[count.index]
cidr_block = local.public-subnets[count.index]
2021-01-28 15:04:01 +08:00
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-public-${split("-", element(data.aws_availability_zones.available-az.names, count.index))[2]}-${count.index + 1}"
},
2021-01-28 15:04:01 +08:00
)
}
resource "aws_vpc" "vpc" {
cidr_block = var.vpc-cidr
enable_dns_hostnames = true
enable_dns_support = true
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-vpc"
},
2021-01-28 15:04:01 +08:00
)
lifecycle {
create_before_destroy = true
}
}
resource "aws_internet_gateway" "igw" {
2023-02-21 12:26:31 +08:00
count = var.number-of-public-subnets-per-az > 0 ? 1 : 0
2021-01-28 15:04:01 +08:00
vpc_id = aws_vpc.vpc.id
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-igw"
},
2021-01-28 15:04:01 +08:00
)
}
resource "aws_eip" "ngw-eip" {
2023-02-21 12:26:31 +08:00
count = var.create-nat-gateway ? 1 : 0
vpc = true
tags = var.default-tags
2021-01-28 15:04:01 +08:00
depends_on = [aws_internet_gateway.igw]
}
resource "aws_nat_gateway" "ngw" {
2023-02-21 12:26:31 +08:00
count = var.create-nat-gateway ? 1 : 0
2021-01-28 15:04:01 +08:00
allocation_id = aws_eip.ngw-eip[0].id
subnet_id = aws_subnet.public-subnets[0].id
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-ngw"
},
2021-01-28 15:04:01 +08:00
)
depends_on = [aws_internet_gateway.igw]
}
2023-02-21 12:26:31 +08:00
resource "aws_route_table" "public-route-table" {
count = var.number-of-public-subnets-per-az > 0 ? 1 : 0
2021-01-28 15:04:01 +08:00
vpc_id = aws_vpc.vpc.id
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-publicroutetable"
},
2021-01-28 15:04:01 +08:00
)
}
2023-02-21 12:26:31 +08:00
resource "aws_route_table" "private-route-table" {
count = var.number-of-private-subnets-per-az > 0 ? 1 : 0
2021-01-28 15:04:01 +08:00
vpc_id = aws_vpc.vpc.id
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-privateroutetable"
},
2021-01-28 15:04:01 +08:00
)
}
resource "aws_route" "public-routes" {
count = var.number-of-public-subnets-per-az > 0 ? 1 : 0
2021-01-28 15:04:01 +08:00
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw[0].id
route_table_id = aws_route_table.public-route-table[0].id
}
resource "aws_route" "private-routes" {
count = var.number-of-private-subnets-per-az > 0 && var.create-nat-gateway ? 1 : 0
2021-01-28 15:04:01 +08:00
destination_cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.ngw[0].id
route_table_id = aws_route_table.private-route-table[0].id
}
resource "aws_route_table_association" "public_route_association" {
2023-02-21 12:26:31 +08:00
count = length(aws_subnet.public-subnets)
2021-01-28 15:04:01 +08:00
route_table_id = aws_route_table.public-route-table[0].id
2023-02-21 12:26:31 +08:00
subnet_id = aws_subnet.public-subnets[count.index].id
2021-01-28 15:04:01 +08:00
}
resource "aws_route_table_association" "private_route_association" {
2023-02-21 12:26:31 +08:00
count = length(aws_subnet.private-subnets)
2021-01-28 15:04:01 +08:00
route_table_id = aws_route_table.private-route-table[0].id
subnet_id = aws_subnet.private-subnets[count.index].id
2021-01-28 15:04:01 +08:00
}
/*
harden default security group. the default sg created by aws allows all egress.
this resource limits ingress and egress from and to itself
*/
2023-02-21 12:26:31 +08:00
resource "aws_default_security_group" "default-sg" {
vpc_id = aws_vpc.vpc.id
ingress {
2023-02-21 12:26:31 +08:00
protocol = -1
self = true
from_port = 0
to_port = 0
description = "Allow traffic coming from this SG"
}
egress {
2023-02-21 12:26:31 +08:00
from_port = 0
protocol = -1
to_port = 0
self = true
description = "Allow traffic going to this SG"
}
tags = merge(
2023-02-21 12:26:31 +08:00
var.default-tags,
{
Name = "${local.resource-prefix}-defaultsg"
},
)
2022-09-22 00:46:56 +08:00
}
# Enable gateway endpoints which are free
2023-02-21 12:26:31 +08:00
module "vpc-ep" {
count = var.create-free-vpc-endpoints ? 1 : 0
2022-09-22 00:46:56 +08:00
source = "../vpc-endpoints"
default-tags = var.default-tags
2023-02-21 12:26:31 +08:00
gateway-ep-services = ["s3", "dynamodb"]
2022-09-22 00:46:56 +08:00
interface-ep-services = []
resource-prefix = local.resource-prefix
vpc-id = aws_vpc.vpc.id
2023-02-21 12:26:31 +08:00
aws-region = var.aws-region
}