@@ -4,12 +4,11 @@ terraform {
44 required_providers {
55 aws = {
66 source = " hashicorp/aws"
7- version = " ~> 5.0" # Allows any version 5.x.x
7+ version = " ~> 5.0"
88 }
99 }
1010}
1111
12-
1312resource "aws_vpc" "eks_vpc" {
1413 cidr_block = var. vpc_cidr
1514 instance_tenancy = " default"
@@ -20,44 +19,111 @@ resource "aws_vpc" "eks_vpc" {
2019 }
2120}
2221
23- resource "aws_internet_gateway" "eks_internet_gateway" {
24- vpc_id = aws_vpc. eks_vpc . id
25- }
26-
27- # Using data source to get all Avalablility Zones in region
22+ # Data source to get all Availability Zones in the region
2823data "aws_availability_zones" "available_zones" {}
2924
30- resource "aws_subnet" "public_subnet_az1" {
25+ # Private Subnet in AZ1
26+ resource "aws_subnet" "private_subnet_az1" {
3127 vpc_id = aws_vpc. eks_vpc . id
32- cidr_block = var. public_subnet_az1_cidr
28+ cidr_block = var. private_subnet_az1_cidr
3329 availability_zone = data. aws_availability_zones . available_zones . names [0 ]
34- map_public_ip_on_launch = true
30+ map_public_ip_on_launch = false
31+
32+ tags = {
33+ Name = " ${ var . customer } -private-subnet-az1"
34+ }
3535}
3636
37- resource "aws_subnet" "public_subnet_az2" {
37+ # Private Subnet in AZ2
38+ resource "aws_subnet" "private_subnet_az2" {
3839 vpc_id = aws_vpc. eks_vpc . id
39- cidr_block = var. public_subnet_az2_cidr
40+ cidr_block = var. private_subnet_az2_cidr
4041 availability_zone = data. aws_availability_zones . available_zones . names [1 ]
41- map_public_ip_on_launch = true
42+ map_public_ip_on_launch = false
43+
44+ tags = {
45+ Name = " ${ var . customer } -private-subnet-az2"
46+ }
47+ }
48+
49+ # Public Subnet for NAT Gateway
50+ resource "aws_subnet" "public_subnet" {
51+ vpc_id = aws_vpc. eks_vpc . id
52+ cidr_block = var. public_subnet_nat_cidr
53+ availability_zone = data. aws_availability_zones . available_zones . names [0 ]
54+ map_public_ip_on_launch = true # Public for NAT
55+
56+ tags = {
57+ Name = " ${ var . customer } -public-subnet-for-nat"
58+ }
59+ }
60+
61+ # Internet Gateway (needed for NAT Gateway)
62+ resource "aws_internet_gateway" "eks_internet_gateway" {
63+ vpc_id = aws_vpc. eks_vpc . id
64+
65+ tags = {
66+ Name = " ${ var . customer } -eks-igw"
67+ }
4268}
4369
70+ # Elastic IP for NAT Gateway
71+ resource "aws_eip" "nat_eip" {
72+ domain = " vpc"
73+ }
74+
75+ # NAT Gateway in Public Subnet
76+ resource "aws_nat_gateway" "nat_gw" {
77+ allocation_id = aws_eip. nat_eip . id
78+ subnet_id = aws_subnet. public_subnet . id
79+
80+ tags = {
81+ Name = " ${ var . customer } -nat-gateway"
82+ }
83+ }
84+
85+ # Private Route Table (Uses NAT Gateway)
86+ resource "aws_route_table" "private_route_table" {
87+ vpc_id = aws_vpc. eks_vpc . id
88+
89+ route {
90+ cidr_block = " 0.0.0.0/0"
91+ nat_gateway_id = aws_nat_gateway. nat_gw . id
92+ }
93+
94+ tags = {
95+ Name = " ${ var . customer } -private-route-table"
96+ }
97+ }
98+
99+ # Associate Private Subnet in AZ1 with Private Route Table
100+ resource "aws_route_table_association" "private_subnet_az1_association" {
101+ subnet_id = aws_subnet. private_subnet_az1 . id
102+ route_table_id = aws_route_table. private_route_table . id
103+ }
104+
105+ # Associate Private Subnet in AZ2 with Private Route Table
106+ resource "aws_route_table_association" "private_subnet_az2_association" {
107+ subnet_id = aws_subnet. private_subnet_az2 . id
108+ route_table_id = aws_route_table. private_route_table . id
109+ }
110+
111+ # Public Route Table for NAT Gateway
44112resource "aws_route_table" "public_route_table" {
45113 vpc_id = aws_vpc. eks_vpc . id
46114
47115 route {
48116 cidr_block = " 0.0.0.0/0"
49117 gateway_id = aws_internet_gateway. eks_internet_gateway . id
50118 }
51- }
52119
53- # Associating Public Subnet in AZ1 to route table
54- resource "aws_route_table_association" "public_subnet_az1_route_table_association" {
55- subnet_id = aws_subnet. public_subnet_az1 . id
56- route_table_id = aws_route_table. public_route_table . id
120+ tags = {
121+ Name = " public-route-table"
122+ }
57123}
58124
59- # Associating Public Subnet in AZ2 to route table
60- resource "aws_route_table_association" "public_subnet_az2_route_table_association " {
61- subnet_id = aws_subnet. public_subnet_az2 . id
125+ # Public Subnet with Public Route Table
126+ resource "aws_route_table_association" "public_subnet_association " {
127+ subnet_id = aws_subnet. public_subnet . id
62128 route_table_id = aws_route_table. public_route_table . id
63129}
0 commit comments