-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
108 lines (84 loc) · 2.32 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
resource "aws_vpc" "my_vpc" {
cidr_block = var.vpc_cidr
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = var.vpc_name
}
}
# Public Subnet
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = var.public_subnet_cidr
availability_zone = var.private_subnet_az
tags = {
Name = var.public_subnet_name
}
}
# Private Subnet
resource "aws_subnet" "private_subnet" {
vpc_id = aws_vpc.my_vpc.id
cidr_block = var.private_subnet_cidr
availability_zone = var.private_subnet_az
tags = {
Name = var.private_subnet_name
}
}
# Internet Gateway
resource "aws_internet_gateway" "igw" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = var.igw_name
}
}
# Public Route Table
resource "aws_route_table" "public_rt" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = var.public_rt_name
}
}
# Associate Public Subnet with public route table
resource "aws_route_table_association" "public_rt_association" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_rt.id
}
# Private Route Table
resource "aws_route_table" "private_rt" {
vpc_id = aws_vpc.my_vpc.id
tags = {
Name = var.private_rt_name
}
}
# Associate Private Subnet with private route table
resource "aws_route_table_association" "private_rt_association" {
subnet_id = aws_subnet.private_subnet.id
route_table_id = aws_route_table.private_rt.id
}
# Public Route: Create a Route to the Internet Gateway for Public Subnet
resource "aws_route" "route_to_igw" {
route_table_id = aws_route_table.public_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.igw.id
}
# elastic IP for NAT gateway
resource "aws_eip" "nat_eip" {
domain = "vpc"
}
# Create a NAT gateway
resource "aws_nat_gateway" "nat_gw" {
allocation_id = aws_eip.nat_eip.id
subnet_id = aws_subnet.public_subnet.id
tags = {
Name = var.nat_gw_name
}
# To ensure proper ordering, it is recommended to add an explicit dependency
# on the Internet Gateway for the VPC.
depends_on = [aws_internet_gateway.igw]
}
# Private route: Create a route to the NAT GW for the Private Subnet
resource "aws_route" "route_to_nat_gw" {
route_table_id = aws_route_table.private_rt.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_nat_gateway.nat_gw.id
}