-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvpc.tf
152 lines (132 loc) · 5.25 KB
/
vpc.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
resource "aws_vpc" "terraform_orchestration_vpc" {
cidr_block = "10.0.0.0/16"
enable_dns_support = true
enable_dns_hostnames = true
tags = {
Name = "terraform-orchestration-vpc"
}
}
# Create Internet Gateway for Public Subnet
resource "aws_internet_gateway" "terraform_orchestration_igw" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
tags = {
Name = "terraform-orchestration-igw"
}
}
# Create Public Subnet 1 in AZ us-east-1a
resource "aws_subnet" "terraform_orchestration_public_subnet_1" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true # This allows public IP addresses for instances in this subnet
tags = {
Name = "terraform-orchestration-public-subnet-1"
}
}
# Create Route Table for Public Subnet
resource "aws_route_table" "terraform_orchestration_public_route_table" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
route {
cidr_block = "0.0.0.0/0" # Route all outbound traffic to the internet
gateway_id = aws_internet_gateway.terraform_orchestration_igw.id
}
tags = {
Name = "terraform-orchestration-public-route-table"
}
}
# Associate Public Subnet 1 with Public Route Table
resource "aws_route_table_association" "public_association" {
subnet_id = aws_subnet.terraform_orchestration_public_subnet_1.id
route_table_id = aws_route_table.terraform_orchestration_public_route_table.id
}
# Public Subnet in Availability Zone 2
resource "aws_subnet" "terraform_orchestration_public_subnet_2" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1b" # Second Availability Zone
map_public_ip_on_launch = true
tags = {
Name = "terraform-orchestration-public-subnet-2"
}
}
# Associate the public route table with the second public subnet
resource "aws_route_table_association" "public_association_2" {
subnet_id = aws_subnet.terraform_orchestration_public_subnet_2.id
route_table_id = aws_route_table.terraform_orchestration_public_route_table.id
}
# Create Elastic IP for NAT Gateway 1 (Public Subnet 1)
resource "aws_eip" "terraform_orchestration_nat_eip" {
domain = "vpc"
}
# Create NAT Gateway in the Public Subnet 1
resource "aws_nat_gateway" "terraform_orchestration_nat_gateway_1" {
allocation_id = aws_eip.terraform_orchestration_nat_eip.id
subnet_id = aws_subnet.terraform_orchestration_public_subnet_1.id # NAT in Public Subnet
tags = {
Name = "terraform-orchestration-nat-gateway"
}
}
# Create Elastic IP for NAT Gateway 2 (Public Subnet 2)
resource "aws_eip" "terraform_orchestration_nat_eip_2" {
domain = "vpc"
}
# Create NAT Gateway in the Public Subnet 2
resource "aws_nat_gateway" "terraform_orchestration_nat_gateway_2" {
allocation_id = aws_eip.terraform_orchestration_nat_eip_2.id
subnet_id = aws_subnet.terraform_orchestration_public_subnet_2.id # NAT in Public Subnet 2
tags = {
Name = "terraform-orchestration-nat-gateway-2"
}
}
# Create Private Subnet 1
resource "aws_subnet" "terraform_orchestration_private_subnet_1" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
cidr_block = "10.0.3.0/24" # Private subnet range
availability_zone = "us-east-1a"
map_public_ip_on_launch = false # No public IPs for instances in this subnet
tags = {
Name = "terraform-orchestration-private-subnet-1"
}
}
# Create Private Route Table for Private Subnet 1
resource "aws_route_table" "terraform_orchestration_private_route_table_1" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
route {
cidr_block = "0.0.0.0/0" # Route all outbound traffic to the NAT Gateway
nat_gateway_id = aws_nat_gateway.terraform_orchestration_nat_gateway_1.id
}
tags = {
Name = "terraform-orchestration-private-route-table"
}
}
# Associate Private Subnet 1 with Private Route Table
resource "aws_route_table_association" "private_association" {
subnet_id = aws_subnet.terraform_orchestration_private_subnet_1.id
route_table_id = aws_route_table.terraform_orchestration_private_route_table_1.id
}
# Create Private Subnet 2
resource "aws_subnet" "terraform_orchestration_private_subnet_2" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
cidr_block = "10.0.4.0/24" # Private subnet range
availability_zone = "us-east-1b"
map_public_ip_on_launch = false # No public IPs for instances in this subnet
tags = {
Name = "terraform-orchestration-private-subnet-2"
}
}
# Create Private Route Table for Private Subnet 2
resource "aws_route_table" "terraform_orchestration_private_route_table_2" {
vpc_id = aws_vpc.terraform_orchestration_vpc.id
route {
cidr_block = "0.0.0.0/0" # Route all outbound traffic to the NAT Gateway
nat_gateway_id = aws_nat_gateway.terraform_orchestration_nat_gateway_2.id
}
tags = {
Name = "terraform-orchestration-private-route-table-2"
}
}
# Associate Private Subnet 2 with Private Route Table
resource "aws_route_table_association" "private_association_2" {
subnet_id = aws_subnet.terraform_orchestration_private_subnet_2.id
route_table_id = aws_route_table.terraform_orchestration_private_route_table_2.id
}