-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterraform task1.tf
More file actions
328 lines (260 loc) · 7.73 KB
/
terraform task1.tf
File metadata and controls
328 lines (260 loc) · 7.73 KB
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
provider "aws" {
region = "ap-south-1"
profile = "hitkool"
}
//step 1 : making a private key and storing it to computer
resource "tls_private_key" "SSH_key" {
algorithm = "RSA"
}
resource "local_file" "SSH_privatekey" {
content = tls_private_key.SSH_key.private_key_pem
filename = "nokey.pem"
file_permission = 0400
}
resource "aws_key_pair" "key1"{
key_name= "nokey"
public_key = tls_private_key.SSH_key.public_key_openssh
}
/*
// creating a vpc group for security grp
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "TFvpc"
}
*/
// step 2 : Create security group : hitsec
// tip : when i write vpc id then code is working fine.
resource "aws_security_group" "hit_sec" {
name = "hit_security"
description = "Made using TF"
vpc_id = "vpc-608d9b08"
//vpc_id = aws_vpc.main.id
tags = {
Name = "hitsec"
}
//For inbound rules in security group : hitsec
ingress {
description = "creating inbound rule for port no 443"
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "creating inbound rule for port no 80"
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
description = "creating inbound rule for port no 22"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
//creating outbount rules for PN 22 443 80
egress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
//launching aws instance
resource "aws_instance" "web" {
//ami = "ami-005956c5f0f757d37" //amzn linux 2
ami = "ami-052c08d70def0ac62" // redhat
instance_type = "t2.micro"
key_name = aws_key_pair.key1.key_name
security_groups = [ aws_security_group.hit_sec.name ]
//security_groups = [ "launch-wizard-1" ]
connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.SSH_key.private_key_pem
//private_key = file("C:/Users/hites/Downloads/27apr.pem")
//password = file("C:/Users/hites/Downloads/27apr.pem")
host = aws_instance.web.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo yum install httpd git php -y",
"sudo systemctl start httpd",
"sudo systemctl enable httpd",
]
}
provisioner "remote-exec" {
when = destroy
inline = [ "sudo poweroff" ]
}
tags = {
Name = "ami2os1"
}
}
output "ins_web_public_ip" {
value = aws_instance.web.public_ip
}
output "ins_web_id" {
value = aws_instance.web.id
}
//step 4.1 : create a ebs : hitebs
resource "aws_ebs_volume" "hitebs" {
depends_on = [
aws_instance.web,
]
availability_zone = aws_instance.web.availability_zone
//picking value of subnet of myin1 and creating a vol there
size = 1
//skip_destroy = True
tags = {
Name = "hitebs"
}
}
//step 4.2 : attach a ebs volume to /dev/sdh
resource "aws_volume_attachment" "attach_hit1_hitebs" {
device_name = "/dev/xvdh"
volume_id = aws_ebs_volume.hitebs.id
instance_id = aws_instance.web.id
}
//Saving instance public ip in a txt file
resource "null_resource" "nulllocall" {
provisioner "local-exec" {
command = "echo ${aws_instance.web.public_ip} > publicip.txt"
} //string interpolation
}
//step 4.3 : formatting mounting and downloading git repo in ebs storage
resource "null_resource" "vol-attach" {
depends_on = [
aws_volume_attachment.attach_hit1_hitebs,
]
connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.SSH_key.private_key_pem
host = aws_instance.web.public_ip
}
provisioner "remote-exec" {
inline = [
"sudo mkfs.ext4 /dev/xvdh",
"sudo mount /dev/xvdh /var/www/html",
"sudo rm -rf /var/www/html/*",
"sudo git clone https://github.com/hitk6/hitcloud.git /var/www/html"
]
}
}
// error when destroying ebs vol is not disconnected from instance and shows error
/*
provisioner "remote-exec" {
when = "destroy"
inline = [ "sudo poweroff" ]
}
*/
//step 5 : creating a s3 bucket
resource "aws_s3_bucket" "bucket-img" {
depends_on = [
null_resource.vol-attach,
]
bucket = "aws-task-1"
acl = "public-read"
tags = {
Name = "mybucket1"
Environment="Dev"
}
}
//step 6 : uploading my image to s3 bucket
resource "aws_s3_bucket_object" "image" {
depends_on = [
aws_s3_bucket.bucket-img
]
key = "hitesh.jpg"
bucket = aws_s3_bucket.bucket-img.bucket
acl = "public-read"
source = "C:\\Users\\hites\\Desktop\\terra\\hitesh.jpg"
etag = "${filemd5("C:\\Users\\hites\\Desktop\\terra\\hitesh.jpg")}"
}
locals {
s3_origin_id = "S3-${aws_s3_bucket.bucket-img.bucket}"
}
resource "aws_cloudfront_distribution" "s3_distribution" {
origin {
domain_name = "${aws_s3_bucket.bucket-img.bucket_regional_domain_name}"
origin_id = "${aws_s3_bucket.bucket-img.id}"
}
enabled = true
is_ipv6_enabled = true
comment = "S3 bucket"
default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "${aws_s3_bucket.bucket-img.id}"
forwarded_values {
query_string = false
cookies {
forward = "none"
}
}
viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400
}
# Cache behavior with precedence 0
ordered_cache_behavior {
path_pattern = "/content/immutable/*"
allowed_methods = ["GET", "HEAD", "OPTIONS"]
cached_methods = ["GET", "HEAD", "OPTIONS"]
target_origin_id = "${aws_s3_bucket.bucket-img.id}"
forwarded_values {
query_string = false
headers = ["Origin"]
cookies {
forward = "none"
}
}
min_ttl = 0
default_ttl = 86400
max_ttl = 31536000
compress = true
viewer_protocol_policy = "redirect-to-https"
}
restrictions {
geo_restriction {
restriction_type = "whitelist"
locations = ["IN"]
}
}
tags = {
Environment = "production"
}
viewer_certificate {
cloudfront_default_certificate = true
}
connection {
type = "ssh"
user = "ec2-user"
private_key = tls_private_key.SSH_key.private_key_pem
host = aws_instance.web.public_ip
}
provisioner "remote-exec"{
inline= [
"sudo su << EOF",
"echo \"<img src='http://${self.domain_name}/${aws_s3_bucket_object.image.key}' width='300' height='400'>\" >> /var/www/html/index.html",
"EOF",
]
}
}