-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.tf
386 lines (312 loc) · 8.82 KB
/
aws.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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#Configure AWS Provider
#Operational environment is set to run in <us-east-1> region
#importing infrastructure into terraform's control
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"]
}
resource "aws_instance" "web2" {
ami = data.aws_ami.ubuntu.id
instance_type = "t2.micro"
tags = {
Name = "ec2"
}
}
resource "aws_db_instance" "default" {
allocated_storage = 10
db_name = "mydb"
engine = "mysql"
engine_version = "5.7"
instance_class = "db.t3.micro"
username = "foo"
password = "foobarbaz"
parameter_group_name = "default.mysql5.7"
skip_final_snapshot = true
}
resource "aws_s3_bucket" "b2" {
bucket = "thisismybucket"
tags = {
Name = "My bucket"
Environment = "Dev"
}
}
resource "aws_s3_bucket_acl" "example" {
bucket = aws_s3_bucket.b2.id
acl = "private"
}
resource "aws_security_group" "allow_tls_second" {
name = "allow_tls_second"
description = "Allow TLS inbound traffic"
vpc_id = aws_default_vpc.default.id
ingress {
description = "TLS from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
}
}
#Configure custom VPC
resource "aws_vpc" "TF_vpc" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "TF_vpc"
}
}
#Configure internet gateway
resource "aws_internet_gateway" "TF_gw" {
vpc_id = aws_vpc.TF_vpc.id
tags = {
Name = "TF_gw"
}
}
#Configure custom route table
resource "aws_route_table" "TF-rte-tble" {
vpc_id = aws_vpc.TF_vpc.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.TF_gw.id
}
route {
ipv6_cidr_block = "::/0"
gateway_id = aws_internet_gateway.TF_gw.id
}
tags = {
Name = "TF-route"
}
}
#Configure subnet for us-east-1a
resource "aws_subnet" "TF_subnet_1a" {
vpc_id = aws_vpc.TF_vpc.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
tags = {
ame = "TF_subnet"
}
}
#Configure subnet for us-east-1b
resource "aws_subnet" "TF_subnet_1b" {
vpc_id = aws_vpc.TF_vpc.id
cidr_block = "10.0.10.0/24"
availability_zone = "us-east-1b"
tags = {
ame = "TF_subnet"
}
}
#Configure route table with subnet 1a
resource "aws_route_table_association" "TF_sub_ass_1a" {
subnet_id = aws_subnet.TF_subnet_1a.id
route_table_id = aws_route_table.TF-rte-tble.id
}
#Configure route table with subnet 1b
resource "aws_route_table_association" "TF_sub_ass_1b" {
subnet_id = aws_subnet.TF_subnet_1b.id
route_table_id = aws_route_table.TF-rte-tble.id
}
#Configure network interface for AmazonLinux
resource "aws_network_interface" "TF_NIC_AMZN_LX" {
subnet_id = aws_subnet.TF_subnet_1a.id
private_ips = ["10.0.1.50"]
security_groups = [aws_security_group.TF_allow_web.id]
tags = {
Name = "TF_NIC_AMZN_LX"
}
}
#Configure network interface for Ubuntu
resource "aws_network_interface" "TF_NIC_UBUNTU" {
subnet_id = aws_subnet.TF_subnet_1b.id
private_ips = ["10.0.10.51"]
security_groups = [aws_security_group.TF_allow_web.id]
tags = {
Name = "TF_NIC_UBUNTU"
}
}
#Create AWS Elastic IP for AmazonLinux
resource "aws_eip" "TF_EIP_1" {
vpc = true
network_interface = aws_network_interface.TF_NIC_AMZN_LX.id
associate_with_private_ip = "10.0.1.50"
depends_on = [aws_internet_gateway.TF_gw]
}
#Create AWS Elastic IP for Ubuntu
resource "aws_eip" "TF_EIP_2" {
vpc = true
network_interface = aws_network_interface.TF_NIC_UBUNTU.id
associate_with_private_ip = "10.0.10.51"
depends_on = [aws_internet_gateway.TF_gw]
}
#Create security group for instances
resource "aws_security_group" "TF_allow_web" {
name = "allow_elb_traffic"
description = "Allow LB inbound traffic"
vpc_id = aws_vpc.TF_vpc.id
#Inbound HTTP
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
security_groups = [aws_security_group.TF_ELB_SG.id]
}
#Inbound HTTPS
ingress {
description = "HTTPs from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
#Inbound SSH
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
#Outbound ANY
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "TF_Allow_Web"
}
}
#Create security group for Elastic Load Balancer
resource "aws_security_group" "TF_ELB_SG" {
name = "allow_web_traffic"
description = "Allow Web inbound traffic"
vpc_id = aws_vpc.TF_vpc.id
#Inbound HTTP
ingress {
description = "HTTP from VPC"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
#Inbound HTTPS
ingress {
description = "HTTPs from VPC"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
#Inbound SSH
ingress {
description = "SSH from VPC"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
#Outbound ANY
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "TF_ELB_SG"
}
}
#Linux environment configuration
#Install and configure Apache web server on AmazonLinux
#Deployed on /us-east-1a/
resource "aws_instance" "myFirst_TF-server" {
ami = "ami-0d5eff06f840b45e9"
instance_type = "t2.micro"
availability_zone = "us-east-1a"
key_name = "enterhere"
user_data = <<-EOF
#!/bin/bash
yum update -y
yum upgrade -y
yum install -y httpd.x86_64
systemctl start httpd.service
systemctl enable httpd.service
echo "Hello this is terraform made Amazon_Linux server from $(hostname -f)" > /var/www/html/index.html
EOF
network_interface {
device_index = 0
network_interface_id = aws_network_interface.TF_NIC_AMZN_LX.id
}
tags = {
Name = "TF_AmazonLinux_web"
}
}
#Install and configure Apache web server on Ubuntu
#Deployed on /us-east-1b/
resource "aws_instance" "mySecond_TF-server" {
ami = "ami-09e67e426f25ce0d7"
instance_type = "t2.micro"
availability_zone = "us-east-1b"
key_name = "enterhere"
user_data = <<-EOF
#!/bin/bash
sudo apt-get update -y
sudo apt-get install -y apache2
sudo systemctl start apache2
echo "Hello this is terraform made Ubuntu server from $(hostname -f)" > /var/www/html/index.html
EOF
network_interface {
device_index = 0
network_interface_id = aws_network_interface.TF_NIC_UBUNTU.id
}
tags = {
Name = "TF_Ubuntu_web"
}
}
resource "aws_elb" "my-elb" {
name = "my-elb"
subnets = [aws_subnet.TF_subnet_1a.id, aws_subnet.TF_subnet_1b.id]
security_groups = [aws_security_group.TF_ELB_SG.id]
listener {
instance_port = 80
instance_protocol = "http"
lb_port = 80
lb_protocol = "http"
}
health_check {
healthy_threshold = 10
unhealthy_threshold = 2
timeout = 5
target = "HTTP:80/"
interval = 10
}
instances = [aws_instance.myFirst_TF-server.id, aws_instance.mySecond_TF-server.id]
cross_zone_load_balancing = true
idle_timeout = 400
connection_draining = true
connection_draining_timeout = 400
tags = {
Name = "my-elb"
}
}
resource "aws_default_vpc" "default" {
tags = {
Name = "Default VPC"
}
}
resource "aws_vpc_ipv4_cidr_block_association" "secondary_cidr" {
vpc_id = aws_default_vpc.default.id
cidr_block = "172.2.0.0/16"
}
resource "aws_subnet" "in_secondary_cidr" {
vpc_id = aws_vpc_ipv4_cidr_block_association.secondary_cidr.vpc_id
cidr_block = "172.2.0.0/24"
}