-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathvpc_networking.tf
297 lines (251 loc) · 7.63 KB
/
vpc_networking.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
//VPC CONFIGURATIONS//
//provider declaration//
provider "aws" {
region = "ap-south-1"
}
//vpc creation//
resource "aws_vpc" "myvpc" {
cidr_block = var.cidr
enable_dns_hostnames = var.enable_dns_hostnames #imp to connect internet publicaly
enable_dns_support = var.enable_dns_support
tags = {
Name = "myvpc"
}
}
//creation of IG//
resource "aws_internet_gateway" "mygateway" {
vpc_id = aws_vpc.myvpc.id ###it will attach IG to the above created vpc
tags = {
Name = var.mygateway
}
}
//fetching available AZ//
data "aws_availability_zones" "available" {
state = "available"
}
//creation of public subnet-1//
resource "aws_subnet" "public-subnet-1" {
vpc_id = aws_vpc.myvpc.id ##subnet will launch in above created vpc thats why we gave ref myvpc.id
cidr_block = var.public-subnet-1-cidr
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = var.map_public_ip_on_launch #to convert subnet into public subnet
tags = {
Name = var.public-subnet-1
}
}
//creation of public subnet-2//
resource "aws_subnet" "public-subnet-2" {
vpc_id = aws_vpc.myvpc.id ##subnet will launch in above created vpc thats why we gave ref myvpc.id
cidr_block = var.public-subnet-2-cidr
availability_zone = data.aws_availability_zones.available.names[1]
map_public_ip_on_launch = var.map_public_ip_on_launch
tags = {
Name = var.public-subnet-2
}
}
//creation of private subnet-1//
resource "aws_subnet" "private-subnet-1" {
vpc_id = aws_vpc.myvpc.id ##subnet will launch in above created vpc thats why we gave ref myvpc.id
cidr_block = var.private-subnet-1-cidr
availability_zone = data.aws_availability_zones.available.names[0]
map_public_ip_on_launch = false #to make subnet private
tags = {
Name = var.private-subnet-1
}
}
//creation of private subnet-2//
resource "aws_subnet" "private-subnet-2" {
vpc_id = aws_vpc.myvpc.id ##subnet will launch in above created vpc thats why we gave ref myvpc.id
cidr_block = var.private-subnet-2-cidr
availability_zone = data.aws_availability_zones.available.names[1]
map_public_ip_on_launch = false #to make subnet private
tags = {
Name = var.private-subnet-2
}
}
//creation of public route table//
resource "aws_route_table" "public-route-table" {
vpc_id = aws_vpc.myvpc.id
route = []
tags = {
Name = var.public-route-table
}
}
//creation of public route//
resource "aws_route" "public-route" {
route_table_id = aws_route_table.public-route-table.id ##it will attach route to above route table
destination_cidr_block = "0.0.0.0/0" ##for internet connection
gateway_id = aws_internet_gateway.mygateway.id
depends_on = [aws_route_table.public-route-table] ##it is depend on above route table it only creates when above route table will 1 create
}
//creation of private route table//
resource "aws_route_table" "private-route-table" {
vpc_id = aws_vpc.myvpc.id
route = []
tags = {
Name = var.private-route-table
}
}
//no need to create route for private subnet//
//subnet public association1//
resource "aws_route_table_association" "public-association1" {
subnet_id = aws_subnet.public-subnet-1.id
route_table_id = aws_route_table.public-route-table.id
}
//subnet public association2//
resource "aws_route_table_association" "public-association2" {
subnet_id = aws_subnet.public-subnet-2.id
route_table_id = aws_route_table.public-route-table.id
}
//subnet private association1//
resource "aws_route_table_association" "private-association1" {
subnet_id = aws_subnet.private-subnet-1.id
route_table_id = aws_route_table.private-route-table.id
}
//subnet private association2//
resource "aws_route_table_association" "private-association2" {
subnet_id = aws_subnet.private-subnet-2.id
route_table_id = aws_route_table.private-route-table.id
}
//creation of security group//
resource "aws_security_group" "mysg" {
name = "mysg"
description = "allow all inbound traffic"
vpc_id = aws_vpc.myvpc.id
ingress {
description = "All traffic"
from_port = 0 ##all port
to_port = 0
protocol = "-1" ##for all protocal
cidr_blocks = ["0.0.0.0/0"] ##internet se access
ipv6_cidr_blocks = null ##dont have value
security_groups = null
prefix_list_ids = null
self = null
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
description = "Outbound rule"
security_groups = null
prefix_list_ids = null
self = null
}
tags = {
Name = "mysg"
}
}
//variables declaration//
variable "cidr" {
description = "Enter the CIDR range required for VPC"
type = string
default = "192.168.0.0/16"
}
variable "enable_dns_hostnames" {
description = "Enable DNS Hostname"
type = bool
default = null
}
variable "enable_dns_support" {
description = "Enable DNS Support"
type = bool
default = null
}
variable "mygateway" {
description = "internet gateway name"
type = string
default = "mygateway"
}
variable "public_subnet" {
description = "enter the number of public subnets you need"
type = number
default = null
}
variable "private_subnet" {
description = "CIDR block for database subnet"
type = list(any)
default = null
}
variable "public-subnet-1-cidr" {
description = "Cidr Blocks"
type = string
default = "192.168.1.0/24"
}
variable "public-subnet-2-cidr" {
description = "Cidr Blocks"
type = string
default = "192.168.2.0/24"
}
variable "map_public_ip_on_launch" {
description = "It will map the public ip while launching resources"
type = bool
default = null
}
variable "public-subnet-1" {
description = "public subnet name"
type = string
default = "public-subnet-1"
}
variable "public-subnet-2" {
description = "public subnet name"
type = string
default = "public-subnet-2"
}
variable "private-subnet-1-cidr" {
description = "Cidr Blocks"
type = string
default = "192.168.5.0/24"
}
variable "private-subnet-2-cidr" {
description = "Cidr Blocks"
type = string
default = "192.168.6.0/24"
}
variable "private-subnet-1" {
description = "private subnet name"
type = string
default = "private-subnet-1"
}
variable "private-subnet-2" {
description = "private subnet name"
type = string
default = "private-subnet-2"
}
variable "manage_default_route_table" {
description = "Are we managing default route table"
type = bool
default = null
}
variable "public-route-table" {
description = "Tag name for public route table"
type = string
default = "public-route-table"
}
variable "private-route-table" {
description = "Tag name for private route table"
type = string
default = "private-route-table"
}
variable "database_route_table_association_required" {
description = "Whether db route table association required"
type = bool
default = null
}
variable "enable_ipv6" {
description = "Requests an Amazon-provided IPv6 CIDR block with a /56 prefix length for the VPC. You cannot specify the range of IP addresses, or the size of the CIDR block."
type = bool
default = null
}
variable "default_security_group_name" {
description = "Enter the name for security group"
type = string
default = null
}
variable "enable_dhcp_options" {
description = "Enable DHCP options True or False"
type = bool
default = null
}