diff options
| author | Saumit <justsaumit@protonmail.com> | 2025-09-28 02:25:37 +0530 |
|---|---|---|
| committer | Saumit <justsaumit@protonmail.com> | 2025-09-28 02:25:37 +0530 |
| commit | 912405a8f3f6b831b9abcf6e16a9372160e7ce32 (patch) | |
| tree | 88514db155e36f430be8cc87d12b8d136e156cb2 /astroshop-terraform/modules/vpc/main.tf | |
| parent | d682471acaa88e96c78717b813ffeedb726da831 (diff) | |
vpc: Adding vpc module
Diffstat (limited to 'astroshop-terraform/modules/vpc/main.tf')
| -rw-r--r-- | astroshop-terraform/modules/vpc/main.tf | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/astroshop-terraform/modules/vpc/main.tf b/astroshop-terraform/modules/vpc/main.tf new file mode 100644 index 0000000..3694588 --- /dev/null +++ b/astroshop-terraform/modules/vpc/main.tf @@ -0,0 +1,103 @@ +resource "aws_vpc" "main" { + cidr_block = var.vpc_cidr + enable_dns_support = true + enable_dns_hostnames = true + + tags = { + Name = "${var.cluster_name}-vpc" + } +} + +resource "aws_subnet" "private" { + count = length(var.private_subnet_cidrs) + vpc_id = aws_vpc.main.id + cidr_block = var.private_subnet_cidrs[count.index] + availability_zone = var.availability_zones[count.index] + + tags = { + Name = "${var.cluster_name}-private-${count.index + 1}" + "kubernetes.io/cluster/${var.cluster_name}" = "shared" + "kubernetes.io/role/internal-elb" = "1" + } +} + +resource "aws_subnet" "public" { + count = length(var.public_subnet_cidrs) + vpc_id = aws_vpc.main.id + cidr_block = var.public_subnet_cidrs[count.index] + availability_zone = var.availability_zones[count.index] + + map_public_ip_on_launch = true + + tags = { + Name = "${var.cluster_name}-public-${count.index + 1}" + "kubernetes.io/cluster/${var.cluster_name}" = "shared" + "kubernetes.io/role/elb" = "1" + } +} + +resource "aws_internet_gateway" "main" { + vpc_id = aws_vpc.main.id + + tags = { + Name = "${var.cluster_name}-igw" + } +} + +resource "aws_eip" "nat" { + count = length(var.public_subnet_cidrs) + domain = "vpc" + + tags = { + Name = "${var.cluster_name}-nat-${count.index + 1}" + } +} + +resource "aws_nat_gateway" "main" { + count = length(var.public_subnet_cidrs) + allocation_id = aws_eip.nat[count.index].id + subnet_id = aws_subnet.public[count.index].id + + tags = { + Name = "${var.cluster_name}-nat-${count.index + 1}" + } +} + +resource "aws_route_table" "public" { + vpc_id = aws_vpc.main.id + + route { + cidr_block = "0.0.0.0/0" + gateway_id = aws_internet_gateway.main.id + } + + tags = { + Name = "${var.cluster_name}-public" + } +} + +resource "aws_route_table" "private" { + count = length(var.private_subnet_cidrs) + vpc_id = aws_vpc.main.id + + route { + cidr_block = "0.0.0.0/0" + nat_gateway_id = aws_nat_gateway.main[count.index].id + } + + tags = { + Name = "${var.cluster_name}-private-${count.index + 1}" + } +} + +resource "aws_route_table_association" "private" { + count = length(var.private_subnet_cidrs) + subnet_id = aws_subnet.private[count.index].id + route_table_id = aws_route_table.private[count.index].id +} + +resource "aws_route_table_association" "public" { + count = length(var.public_subnet_cidrs) + subnet_id = aws_subnet.public[count.index].id + route_table_id = aws_route_table.public.id +}
\ No newline at end of file |
