-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy path8. TERRAFORM
825 lines (684 loc) · 18.2 KB
/
8. TERRAFORM
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
DAY-01: 27-07-2023: INTRO, LIFECYCLE, BASIC FILES
INFRA: RESOURCE USED TO RUN OUR APPLICATION.
EX: SERVERS, ASG, ELB, VPC ----
1. ON-PREM: IT WAS MANAGED BY OUR IT TEAM.
2. CLOUD: CLOUD PROVIDERS (AWS, AZURE,GCP)
INFRA IN CLOUD IS CREATED MANUALLY TILL NOW:
LIMITATIONS:
1. MISTAKES WILL HAPPEN.
2. TAKES TIME.
3. INFRA CREATION LATE.
AUTOMATION:
DEVOPS: TERRAFORM, PULUMI, CHEF, ANSIBLE ----
CLOUD:
AWS - CFT
AZURE - ARM
GCP - GDE
HISTORY:
MITCHEL HASIMOTO -- > 2011 : TUMBLR -- > NO RESPONSE
YEAR: 2014
LANGUAGE: GO LANG
COMPANY: HASHICORP
TYPE: FREE AND OPENSOURCE
SPECIFICATION: INFRA CREATION
WHO IT WORKS: CODE -- > HCL (HASHICORP CONFIGURATION LANGUAGE)
ADVANTAGES:
1. AUTOMATION
2. TIME SAVING
3. REUSEABLE
4. DRY RUN
5. CLOUD AGONISTIC
installation:
sudo yum install -y yum-utils shadow-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
sudo yum -y install terraform
PERMISSION:
attach iam user creds to ec2
CONFIGURATION FILE: It has information about our resource.
EX: .tf, .tfvars
cat main.tf
provider "aws" {
region = "eu-west-1"
}
resource "aws_instance" "one" {
count = 3
ami = "ami-06935448000742e6b"
instance_type = "t2.micro"
}
LIFECYCLE:
terrafrom init: it will download provider plugins.
terraform plan: it will create execution plan for resource.
terraform apply: it will create the resource.
terraform destroy: it will destroy the resource.
STATE FILE: It will store the resource infromation which created by terraform.
terrafrom state list: it will list resources inside the state file
TARGET: It will destroy a particular resource in terraform
terraform destroy -target="aws_instance.one[1]"
HISTORY:
1 mkdir terraform
2 cd terraform/
3 sudo yum install -y yum-utils shadow-utils
4 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
5 sudo yum -y install terraform
6 terraform -v
7 cd
8 aws configure
9 cd terraform/
10 ll
11 vim main.tf
12 terraform init
13 terraform plan
14 terraform apply
15 vim main.tf
16 cd
17 vim .aws/config
18 cd terraform/
19 terraform apply
20 ll
21 cat terraform.tfstate
22 terraform state list
23 terraform destroy
24 cat terraform.tfstate
25 vim main.tf
26 terraform init
27 terraform plan
28 terraform apply
29 cat terraform.tfstate
30 terraform state list
31 cat main.tf
32 terraform state list
33 terraform destroy -target="aws_instance.one[1]
34 terraform destroy -target="aws_instance.one[1]"
35 terraform state list
36 terraform destroy -target="aws_instance.one[2]"
37 terraform state list
38 cat main.tf
39 history
====================================================================================
OUTPUTS: It will print the resource outputs
provider "aws" {
region = "eu-west-3"
}
resource "aws_instance" "one" {
ami = "ami-0b915513496b814ce"
instance_type = "t2.micro"
tags = {
Name = "raham-terra-server"
}
}
output "raham" {
value = [aws_instance.one.public_ip, aws_instance.one.private_ip, aws_instance.one.public_dns]
}
TERRAFORM REFRESH: it will refresh the state file and shows the objects existence.
TERRAFORM TAINT: it will recreate specific resource.
provider "aws" {
region = "eu-west-3"
}
resource "aws_instance" "one" {
ami = "ami-0b915513496b814ce"
instance_type = "t2.micro"
tags = {
Name = "raham-terra-server"
}
}
resource "aws_s3_bucket" "two" {
bucket = "rahamterra443357-swiggy"
}
terraform taint aws_instance.one
terraform apply --auto-approve
UNTAINT: To undo the tainting.
terraform untaint aws_s3_bucket.two
ALIAS & PROVIDERS:
it is used to create multiple resource on multiple regions at a time.
provider "aws" {
region = "eu-west-3"
}
resource "aws_instance" "one" {
ami = "ami-0b915513496b814ce"
instance_type = "t2.micro"
tags = {
Name = "raham-terra-server"
}
}
provider "aws" {
region = "eu-west-2"
alias = "london"
}
resource "aws_instance" "two" {
provider = aws.london
ami = "ami-0b594cc165f9cddaa"
instance_type = "t2.micro"
tags = {
Name = "raham-terra-server"
}
}
HISTORY:
1 mkdir terraform
2 cd terraform/
3 sudo yum install -y yum-utils shadow-utils
4 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
5 sudo yum -y install terraform
6 aws configure
7 vim mai.tf
8 vim main.tf
9 terraform init
10 terraform
11 terraform plan
12 terraform apply --auto-approve
13 vim main.tf
14 wq
15 terraform apply --auto-approve
16 vim main.tf
17 terraform apply --auto-approve
18 terraform destroy --auto-approve
19 vim main.tf
20 terraform apply --auto-approve
21 vim main.tf
22 cat main.tf
23 terraform apply --auto-approve
24 vim main.tf
25 terraform apply --auto-approve
26 vim main.tf
27 terraform state list
28 terraform refresh
29 terraform state list
30 vim main.tf
31 terraform apply --auto-approve
32 terraform state list
33 terraform taint aws_instance.one
34 terraform apply --auto-approve
35 terraform state list
36 terraform taint aws_s3_bucket.two
37 terraform apply --auto-approve
38 cat main.tf
39 terraform state list
40 terraform taint aws_s3_bucket.two
41 terraform untaint aws_s3_bucket.two
42 vim main.tf
43 terraform destroy --auto-approve
44 vim main.tf
45 terraform appl\y --auto-approve
46 terraform apply --auto-approve
47 terraform apply --auto-approve -lock=false
48 ps
49 kill -9 1427 1399 1433 1449 1463 1469
50 ps
51 terraform apply --auto-approve -lock=false
52 terraform destroy --auto-approve
53 vim main.tf
54 terraform apply --auto-approve -lock=false
55 vim main.tf
56 terraform apply --auto-approve
57 vim main.tf
58 history
========================================
DAY-04: 1-08-2023: LOCAL, VPC, SUBNET, WORKSPACE, VALIDATE, GRAPH
TERRAFORM VALIDATE: It will validte the terraform files. (syntax check)
LOCALS: we use this block to define the values for resources.
we can define value once and use this multiple times.
Adv: we can modify the value once and rest will change automatically.
provider "aws" {
region = "eu-west-3"
}
locals {
Name = "devserver"
}
resource "aws_instance" "one" {
ami = "ami-0b915513496b814ce"
instance_type = "t2.micro"
tags = {
Name = "${local.Name}"
}
}
provider "aws" {
region = "eu-west-3"
}
locals {
Name = "test"
}
resource "aws_vpc" "one" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "${local.Name}-vpc"
}
}
resource "aws_subnet" "two" {
vpc_id = aws_vpc.one.id
cidr_block = "10.0.0.0/24"
tags = {
Name = "${local.Name}-subnet"
}
}
resource "aws_instance" "three" {
subnet_id = aws_subnet.two.id
ami = "ami-0b915513496b814ce"
instance_type = "t2.micro"
tags = {
Name = "${local.Name}-instance"
}
}
TERRAFORM GRAPH: It will show the blueprint(flow chart) of terraform resource creation.
linK: graphwiz online
copy past the content on this site.
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route_table.html
WORKSPACE: it is used to isloate the environments.
each action we apply on one workspace will not effect another workspace.
in real time we use the workspace to create dev, test --- envs
Note:
1. To delete workspace it should be empty.
2. We cant delete current workspace.
2. we cant Default workspace (it will not create any resource)
terraform workspace list : lists the workspaces
terraform workspace new dev : creates workspace
terraform workspace show : shows the current workspace
terraform workspace select dev : switch to dev workspace
terraform workspace delete dev : to delete dev workspace
provider "aws" {
region = "eu-west-3"
}
locals {
Name = "${terraform.workspace}"
}
resource "aws_instance" "three" {
ami = "ami-0b915513496b814ce"
availability_zone = "eu-west-3b"
key_name = "terraform"
instance_type = "t2.medium"
tags = {
Name = "${local.Name}-instance"
}
}
history:
67 terraform state list
68 vim main.tf
69 terraform init
70 terraform validate
71 terraform plan
72 terraform apply --auto-approve
73 vim main.tf
74 terraform destroy --auto-approve
75 vim main.tf
76 terraform plan
77 vim main.tf
78 terraform plan
79 vim main.tf
80 terraform plan
81 terraform apply --auto-approve
82 vim main.tf
83 terraform apply --auto-approve
84 terraform state list
85 terraform graph
86 terraform workspace list
87 terraform workspace new dev
88 terraform workspace list
89 terraform workspace show
90 terraform workspace select default
91 terraform destroy --auto-approve
92 terraform workspace select dev
93 vim main.tf
94 terraform apply --auto-approve
95 terraform state list
96 terraform workspace new test
97 terraform apply --auto-approve
98 terraform workspace new prod
99 vim main.tf
100 terraform apply --auto-approve
101 terraform state list
102 terraform workspace delete prod
103 terraform destroy --auto-approve
104 terraform worspace list
105 terraform workspace list
106 terraform workspace delete prod
107 terraform workspace select test
108 terraform workspace delete prod
109 terraform workspace list
110 terraform state list
111*
112 terraform workspace list
113 terraform workspace select dev
114 terraform workspace delete test
115 terraform workspace list
116 terraform destroy --auto-approve
117 terraform workspace select default
118 terraform workspace delete dev
119 terraform workspace list
120 terraform workspace delete default
121 vim main.tf
122 terraform apply --auto-approve
123 vim main.tf
124 terraform apply --auto-approve
125 vim main.tf
126 aws ec2 stop-instances --instance-ids i-09d27f174f2d07bc1
127 aws ec2 stop
128 aws ec2 | frep -i stop
129 aws ec2 | grep -i stop
130 aws ec2 stop -h
131 aws ec2 stop-instances --instance-ids i-0a9684385a12b8593
132 aws ec2 stop-instances --instance-ids i-09d27f174f2d07bc1
133 cat /root/.aws/config
134 vim /root/.aws/config
135 aws ec2 stop-instances --instance-ids i-09d27f174f2d07bc1
136 vim /root/.aws/config
137 aws ec2 stop-instances --instance-ids i-0a9684385a12b8593
138 aws ec2 start-instances --instance-ids i-0a9684385a12b8593
==============================================================================
DAY-06:
TERRAFORM DYNMIC BLOCK:
It will help to reuse the code.it will reduce the lenght of code
WITHOUT DYNAMIC BLOCK:
resource "aws_security_group" "five" {
name = "elb-sg"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
provider "aws" {
}
locals {
ingress_rules = [{
port = 443
},
{
port = 80
},
{
port = 8080
}]
}
resource "aws_instance" "ec2_example" {
ami = "ami-0c02fb55956c7d316"
instance_type = "t2.micro"
vpc_security_group_ids = [aws_security_group.main.id]
tags = {
Name = "Terraform EC2"
}
}
resource "aws_security_group" "main" {
egress = [
{
cidr_blocks = ["0.0.0.0/0"]
description = "*"
from_port = 0
ipv6_cidr_blocks = []
prefix_list_ids = []
protocol = "-1"
security_groups = []
self = false
to_port = 0
}]
dynamic "ingress" {
for_each = local.ingress_rules
content {
description = "*"
from_port = ingress.value.port
to_port = ingress.value.port
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
tags = {
Name = "terra sg"
}
}
BACKEND S3:
provider "aws" {
}
terraform {
backend "s3" {
bucket = "mybucket"
key = "prod"
region = "eu-west-3"
}
}
resource "aws_instance" "one" {
for_each = toset(["one", "two", "three"])
ami = "ami-06f7633a6b8e0fb0a"
instance_type = "t2.micro"
tags = {
Name = "instance-${each.key}"
}
}
LOOPS:
provider "aws" {
}
resource "aws_instance" "one" {
for_each = toset(["one", "two", "three"])
ami = "ami-06f7633a6b8e0fb0a"
instance_type = "t2.micro"
tags = {
Name = "instance-${each.key}"
}
}
HISTORY:
1 aws configure
2 mkdir terraform
3 cd terraform/
4 sudo yum install -y yum-utils shadow-utils
5 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
6 sudo yum -y install terraform
7 vim main.tf
8 terraform init
9 terraform plan
10 vim /root/.aws/credentials
11 terraform plan
12 terraform apply --auto-approve
13 cat main.tf
14 vim main.tf
15 terraform apply --auto-approve
16 terraform destroy --auto-approve
17 vim main.tf
18 terraform apply --auto-approve
19 vim main.tf
20 terraform apply --auto-approve
21 vim main.tf
22 terraform apply --auto-approve
23 terraform destroy --auto-approve
24 vim main.tf
25 terraform apply --auto-approve
26 vim main.tf
27 terraform apply --auto-approve
28 vim main.tf
29 terraform apply --auto-approve
30 terraform state list
31 ll
32 cat terraform.tfstate
33 cat terraform.tfstate.backup
34 vim -o terraform.tfstate terraform.tfstate.backup
35 vim -O terraform.tfstate terraform.tfstate.backup
36 ll
37 vim main.tf
38 terraform init
39 terraform validate
40 terraform apply --auto-approve
41 terraform state list
42 vim main.tf
43 terraform apply --auto-approve
44 terraform destrou --auto-approve
45 terraform destroy --auto-approve
46 vim main.tf
47 terraform apply --auto-approve
48 terraform init -reconfigure
49 terraform apply --auto-approve
50 terraform init
51 terraform plan
52 terraform init -reconfigure
53 terraform init -migrate-state
54 terraform apply --auto-approve
55 vim main.tf
56 terraform apply --auto-approve
57 terraform state list
58 terraform destroy --auto-approve
59 vim main.tf
60 history
========================================================
day-06: 7-08-2023: IMPORT, LOCAL RESOURCES, VERSION CONSTARTINTS, META ARGUMENTS LIFECYCLE
TERRAFORM IMPORT: Used to import the resource config which is created manually.
provider "aws" {
region = "eu-west-3"
}
resource "aws_instance" "abc" {
}
terraform import aws_instance.abc (instance-id)
LOCAL RESOURCES:
If we want to create local resources we need to use local provider plugins.
so use terraform init
provider "aws" {
region = "eu-west-3"
}
resource "local_file" "one" {
filename = "abc.txt"
content = "hai all this is my file created by terrafom"
}
VERSION CONSTRAINTS:
aws provoider, gcp provider, azure provider, local provider
if we want to chnage the version of provider so we use this concept.
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">5.9.0, <5.11.0"
}
}
}
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "2.4.0"
}
}
}
MODULES: It will divide the code into multiple files and folders seperately.
if you want ec2 code you need to go ec2 folder
if you want s3 code you need to go s3 folder
cat main.tf
module "my_instance_module" {
source = "./modules/instances"
ami = "ami-0b915513496b814ce"
instance_type = "t2.micro"
instance_name = " rahaminstance"
}
module "s3_module" {
source = "./modules/buckets"
bucket_name = "devopsherahamshaik009988"
}
cat provider.tf
provider "aws" {
}
cat modules/instances/main.tf
resource "aws_instance" "my_instance" {
ami = var.ami
instance_type = var.instance_type
tags = {
Name = var.instance_name
}
}
cat modules/instances/variable.tf
variable "ami" {
type = string
}
variable "instance_type" {
type = string
}
variable "instance_name" {
description = "Value of the Name tag for the EC2 instance"
type = string
}
cat modules/buckets/main.tf
resource "aws_s3_bucket" "b" {
bucket = var.bucket_name
}
cat modules/buckets/variable.tf
variable "bucket_name" {
type = string
}
HISTORY:
144 vim main.tf
145 terraform import aws_instance.abc=i-07016d4e7a943e3cc
146 cat main.tf
147 terraform import aws_instance.abc i-07016d4e7a943e3cc
148 vim /root/.aws/credentials
149 terraform import aws_instance.abc i-07016d4e7a943e3cc
150 cat terraform.tfstate
151 vim main.tf
152 terraform init
153 terraform apply --auto-approve
154 vim main.tf
155 terraform apply --auto-approve
156 ll
157 cat abc.txt
158 vim main.tf
159 terraform init
160 vim main.tf
161 ll
162 cat main.tf
163 terraform apply --auto-approve
164 vim main.tf
165 terraform init
166 terraform init -upgrade
167 vim main.tf
168 terraform init -upgrade
169 vim main.tf
170 terraform init -upgrade
171 vim main.tf
172 terraform init -upgrade
173 vim main.tf
174 terraform init -upgrade
175 vim main.tf
176 terraform init -upgrade
177 vim main.tf
178 terraform init -upgrade
179 vim main.tf
180 terraform init -upgrade
181 vim main.tf
182 vim provider.tf
183 cat main.tf
184 cat provider.tf
185 mkdir -p modules/instances
186 mkdir -p modules/buckets
187 yum install tree -y
188 tree
189 vim modules/instances/main.tf
190 vim modules/instances/variable
191 vim modules/instances/variable.tf
192 cat m
193 cat main.tf
194 tree
195 cat main.tf
196 vim modules/buckets/main.tf
197 vim modules/buckets/variable.tf
198 tree
199 cat m
200 cat main.tf
201 vim main.tf
202 terraform init -upgrade
203 terraform apply --auto-approve
204 terraform destroy --auto-approve
205 rm -rf *
206 vim main.tf
207 terraform apply --auto-approve
208 terraform destroy --auto-approve
209 vim main.tf
210 terraform apply --auto-approve
211 terraform destroy --auto-approve
212 vim main.tf
213 vim s3.tf
214 vim ebs.tf
215 terraform apply --auto-approve
216 vim ebs.tf
217 terraform apply --auto-approve