-
Notifications
You must be signed in to change notification settings - Fork 192
/
Copy path7. TERRAFORM
975 lines (791 loc) · 20.3 KB
/
7. 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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
INFRASTRUCTURE:
resources used to run our application on cloud.
ex: ec2, s3, elb, vpc --------------
in genral we used to deploy infra on manual
Manual:
1. time consume
2. manual work
3. commiting mistakes
Automate -- > Terraform -- > code -- > hcl (Hashicorp configuration languge)
its a tool used to make infrastructure automation.
its a free and opensource.
its platform independent.
it comes on year 2014.
who: mitchel hasimoto
ownde: hasicorp
terraform is written on go language.
We can call terraform as IAAC TOOL.
HOW IT WORKS:
terraform uses code to automate the infra.
we use HCL : HashiCorp Configuration Language.
IAAC: Infrastructure as a code.
Code --- > execute --- > Infra
ADVANTGAES:
1. Reuseable
2. Time saving
3. Automation
4. Avoiding mistakes
5. Dry run
CFT = AWS
ARM = AZURE
GDE = GOOGLE
TERRAFROM = ALL CLOUDS
INSTALLING TERRAFORM:
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
aws configure
mkdir terraform
cd terraform
vim main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "one" {
ami = "ami-03eb6185d756497f8"
instance_type = "t2.micro"
}
TERRAFORM COMMANDS:
terraform init : initalize the provider plugins on backend
terraform plan : to create execution plan
terrafrom apply : to create resources
terrafrom destroy : to delete resources
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
count = 5
ami = "ami-0b41f7055516b991a"
instance_type = "t2.micro"
}
terraform apply --auto-approve
terraform destroy --auto-approve
STATE FILE: used to store the resource information which is created by terraform
to track the resource activities
in real time entire resource info is on state file.
we need to keep it safe
if we lost this file we cant track the infra.
Command:
terraform state list
terrafrom target: used to destroy the specific resource
terraform state list
single target: terraform destroy -target="aws_instance.one[3]"
multi targets: terraform destroy -target="aws_instance.one[1]" -target="aws_instance.one[2]"
TERRAFORM VARIABLES:
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
count = var.instance_count
ami = "ami-0b41f7055516b991a"
instance_type = var.instance_type
}
variable "instance_type" {
description = "*"
type = string
default = "t2.micro"
}
variable "instance_count" {
description = "*"
type = number
default = 5
}
terraform apply --auto-approve
terraform destroy --auto-approve
history:
1 aws configure
2 sudo yum install -y yum-utils
3 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinu x/hashicorp.repo
4 sudo yum -y install terraform
5 terraform -v
6 mdkir terraform
7 mkdir terraform
8 cd terraform/
9 vim main.tf
10 terraform init
11 terraform plan
12 terraform apply
13 vim main.tf
14 terraform apply
15 ll
16 cat terraform.tfstate
17 terraform destroy
18 cat terraform.tfstate
19 vim main.tf
20 terraform plan
21 terraform apply
22 cat terraform.tfstate
23 terraform state list
24 terraform destroy -target="aws_instance.one[4]"
25 terraform state list
27 terraform destroy --auto-approve -target="aws_instance.one[3]" -target="aws_instance.one[2]" ance.one[0]"
28 terraform state list
29 terraform destroy --auto-approve
30 vim main.tf
31 terraform plan
32 terraform apply --auto-approve
33 vim main.tf
34 terraform apply --auto-approve
35 terraform state list
36 terraform destroy --auto-apporve
37 terraform destroy --auto-approve
38 history
=========================================
VARIABLES, OUTPUTS AND IMPORT
TERRAFORM VAR FILES:
these files used to store variables seperately on terraform.
cat main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "one" {
count = var.instance_count
ami = "ami-03eb6185d756497f8"
instance_type = var.instance_type
tags = {
Name = "raham-server"
}
}
cat variable.tf
variable "instance_count" {
description = "*"
type = number
default = 3
}
variable "instance_type" {
description = "*"
type = string
default = "t2.micro"
}
terraform apply --auto-approve
terraform destroy --auto-approve
cat main.tf
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
count = var.instance_count
ami = "ami-0b41f7055516b991a"
instance_type = var.instance_type
tags = {
Name = "raham-server"
}
}
cat variable.tf
variable "instance_count" {
}
variable "instance_type" {
}
cat dev.tfvars
instance_count = 1
instance_type = "t2.micro"
cat test.tfvars
instance_count = 2
instance_type = "t2.medium"
terraform apply --auto-approve -var-file="dev.tfvars"
terraform destroy --auto-approve -var-file="dev.tfvars"
terraform apply --auto-approve -var-file="test.tfvars"
terraform destroy --auto-approve -var-file="test.tfvars"
TERRAFORM CLI:
we can pass inputs for terraform from cli.
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
ami = "ami-0b41f7055516b991a"
instance_type = var.instance_type
tags = {
Name = "raham-server"
}
}
variable "instance_type" {
}
terraform apply --auto-approve -var="instance_type=t2.medium"
terraform destroy --auto-approve -var="instance_type=t2.medium"
TERRAFORM OUTPUTS: used to print the resource properties.
ex: public-ip, dns, instance type
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
ami = "ami-0b41f7055516b991a"
instance_type = "t2.medium"
tags = {
Name = "raham-server"
}
}
output "raham" {
value = [aws_instance.one.public_ip, aws_instance.one.private_ip, aws_instance.one.public_dns, aws_instance.one.private_dns]
}
TERRAFORM IMPORT: used to import and track the resources which is created manually.
cat main.tf
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
}
terraform import aws_instance.one i-0f4c0d5d3bb6dc758
1 sudo yum install -y yum-utils shadow-utils
2 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
3 sudo yum -y install terraform
4 aws configure
5 mkdir terraform
6 cd terraform/
7 vim main.tf
8 vim variable.tf
9 cat main.tf
10 cat variable.tf
11 terraform init
12 terraform plan
13 cd
14 ls -al
15 ll .aws/
16 vim .aws/credentials
17 cd terraform/
18 terraform plan
19 vim main.tf
20 terraform plan
21 vim main.tf
22 terraform plan
23 terraform apply --aut-approve
24 terraform apply --auto-approve
25 vim main.tf
26 terraform apply --auto-approve
27 terraform destroy --auto-approve
28 vim variable.tf
29 vim dev.tfvars
30 vim test.tfvars
31 cat main.tf
32 cat variable.tf
33 cat dev.tfvars
34 cat test.tfvars
35 terraform apply --auto-approve -var-file="dev.tfvars"
36 terraform apply --auto-approve -var-file="test.tfvars"
37 terraform destroy --auto-approve -var-file="test.tfvars"
38 vim dev.tfvars
39 terraform apply --auto-approve
40 terraform apply --auto-approve -var-file="dev.tfvars"
41 terraform destroy --auto-approve -var-file="dev.tfvars"
42 rm -rf dev.tfvars test.tfvars variable.tf
43 vim main.tf
44 ll
45 terraform apply --auto-approve
46 terraform destroy --auto-approve
47 terraform apply --auto-approve -var="instance_type=t2.medium"
48 terraform destroy --auto-approve -var="instance_type=t2.medium"
49 vim main.tf
50 terraform apply --auto-approve
51 vim main.tf
52 terraform apply
53 vim main.tf
54 terraform apply --auto-approve
55 terraform destroy --auto-approve
56 vim main.tf
57 cat terraform.tfstate
58 vim main.tf
59 cat main.tf
60 terraform import aws_instance.one i-0f4c0d5d3bb6dc758
61 cat terraform.tfstate
62 terraform import --help
63 terraform import -h
===================================================
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "one" {
bucket = "rahamshaik9988tetrrbcuket"
}
resource "aws_ebs_volume" "two" {
size = 20
availability_zone = "us-east-1b"
tags = {
Name = "raham-ebs"
}
}
resource "aws_iam_user" "three" {
name = "rahams"
}
resource "aws_instance" "four" {
ami = "ami-03eb6185d756497f8"
instance_type = "t2.micro"
tags = {
Name = "Raham-terraserver"
}
}
Terraform taint: used to recreate specific objects
in real time some resource we need to recrete if it will not work properly
then we can use taint concetp
terraform taint aws_instance.four
terraform apply --auto-approve
Terraform Lifecycle:
Prevent_destroy: used to keep secure our resources without destroying.
provider "aws" {
region = "us-east-1"
}
resource "aws_s3_bucket" "one" {
bucket = "rahamshaik9988tetrrbcuket"
}
resource "aws_ebs_volume" "two" {
size = 20
availability_zone = "us-east-1b"
tags = {
Name = "raham-ebs"
}
}
resource "aws_iam_user" "three" {
name = "rahams"
}
resource "aws_instance" "four" {
ami = "ami-03eb6185d756497f8"
instance_type = "t2.micro"
tags = {
Name = "Raham-terraserver"
}
lifecycle {
prevent_destroy = true
}
TERRAFORM FMT: used to provide the indentation for terraform.
VERSION CONSTARINT: used to change the provider version.
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">5.19.0"
}
}
}
terraform init -upgrade
Local resources:
provider "aws" {
region = "us-east-1"
}
resource "local_file" "one" {
filename = "abc.txt"
content= "Hai all this file is created by terraform"
}
provider "aws" {
region = "us-east-1"
}
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "2.2.0"
}
}
}
HISTORY:
1 sudo yum install -y yum-utils shadow-utils
2 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
3 sudo yum -y install terraform
4 aws configure
5 mkdir terraform
6 cd terraform/
7 vim main.tf
8 terraform init
9 terraform plan
10 vim main.tf
11 terraform plan
12 terraform apply --auto-approve
13 vim main.tf
14 terraform apply --auto-approve
15 cat main.tf
16 vim main.tf
17 terraform apply --auto-approve
18 terraform state list
19 cat main.tf
20 terraform taint aws_instance.four
21 terraform apply --auto-approve
22 terraform state list
23 terraform taint aws_instance.four aws_ebs_volume.two
24 terraform taint aws_instance.four
25 terraform taint aws_ebs_volume.two
26 terraform apply --auto-approve
27 vim main.tf
28 terraform destroy --auto-approve
29 vim main.tf
30 terraform destroy --auto-approve
31 terraform plan
32 terraform destroy --auto-approve
33 terraform state list
34 vim main.tf
35 terraform destroy --auto-approve
36 vim main.tf
37 terraform destroy --auto-approve
38 cat main.tf
39 vim main.tf
40 terraform destroy --auto-approve
41 terraform apply --auto-approve
42 terraform destroy --auto-approve
43 terraform init
44 vim main.tf
45 cat main.tf
46 terraform fmt
47 cat main.tf
48 vim main.tf
49 terraform init -upgrade
50 vim main.tf
51 terraform init -upgrade
52 vim main.tf
53 terraform init -upgrade
54 cat main.tf
55 vim main.tf
56 terraform apply --auto-approve
57 terraform init -upgrade
58 terraform apply --auto-approve
59 ll /root/
60 ll
61 cd
62 ll
63 cd terraform/
64 ll
65 cat abc.txt
66 cat main.tf
67 vim main.tf
68 terraform init -upgrade
69 vim main.tf
70 terraform init -upgrade
71 cat main.tf
72 history
======================================================================================
TERRAFORM LOCALS:
its a block used to define the values.
we can define the vaule once and used it for multiple times.
provider "aws" {
region = "ap-south-1"
}
locals {
env = "test"
}
resource "aws_vpc" "one" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "${local.env}-vpc"
}
}
resource "aws_subnet" "two" {
cidr_block = "10.0.0.0/16"
availability_zone = "ap-south-1a"
vpc_id = aws_vpc.one.id
tags = {
Name = "${local.env}-subnet"
}
}
resource "aws_instance" "three" {
subnet_id = aws_subnet.two.id
ami = "ami-06006e8b065b5bd46"
instance_type = "t2.micro"
tags = {
Name = "${local.env}-server"
}
}
TERRAFORM WORKSPACE:
WORKSPACE: Where we write the code and execute operations.
it is used to isolate the env.
in real time all the works we do on workspaces only.
if we perform an operation on one workspace it wont affect another workspace.
NOTE:
1. we can't delete our current workspace.
2. we can't delete our workspace without deleting resources.
3. we cant't delete default workspace.
COMMANDS:
terraform workspace list : to list workspaces
terraform workspace new dev : to create a new workspace
terraform workspace show : to show current workspace
terraform workspace select prod : to switch the workspaces
terraform workspace delete prod : to delete the workspaces
provider "aws" {
region = "ap-south-1"
}
locals {
env = "${terraform.workspace}"
}
resource "aws_vpc" "one" {
cidr_block = "10.0.0.0/16"
tags = {
Name = "${local.env}-vpc"
}
}
resource "aws_subnet" "two" {
cidr_block = "10.0.0.0/16"
availability_zone = "ap-south-1a"
vpc_id = aws_vpc.one.id
tags = {
Name = "${local.env}-subnet"
}
}
resource "aws_instance" "three" {
subnet_id = aws_subnet.two.id
ami = "ami-06006e8b065b5bd46"
instance_type = "t2.micro"
tags = {
Name = "${local.env}-server"
}
}
TERRAFORM GRAPH: used to show the flow chart of our infra
terraform graph
copy paste the content in graphviz online
ALIAS AND PROVIDER: used to create resources on mutliple regions in single file.
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "three" {
ami = "ami-06006e8b065b5bd46"
instance_type = "t2.large"
tags = {
Name = "mumbai-server"
}
}
provider "aws" {
region = "ap-northeast-1"
alias = "tokyo"
}
resource "aws_instance" "four" {
provider = aws.tokyo
ami = "ami-0bcf3ca5a6483feba"
instance_type = "t2.large"
tags = {
Name = "tokyo-server"
}
}
HISTORY:
1 sudo yum install -y yum-utils shadow-utils
2 sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/AmazonLinux/hashicorp.repo
3 sudo yum -y install terraform
4 aws configure
5 vim .aws/config
6 mkdir terraform
7 cd
8 cd terraform/
9 vim main.tf
10 terraform init
11 vim main.tf
12 terraform init
13 terraform validate
14 vim main.tf
15 terraform validate
16 vim main.tf
17 terraform validate
18 terraform plan
19 terraform apply --auto-approve
20 terraform state list
21 vim main.tf
22 terraform apply --auto-approve
23 terraform destroy --auto-approve
24 vim main.tf
25 terraform workspace list
26 terraform workspace new dev
27 terraform workspace list
28 terraform workspace show
29 vim main.tf
30 terraform apply --auto-approve
31 terraform state list
32 terraform workspace new test
33 terraform state list
34 vim m
35 vim main.tf
36 terraform apply --auto-approve
37 terraform workspace new prod
38 vim main.tf
39 terraform apply --auto-approve
40 vim main.tf
41 terraform apply --auto-approve
42 vim main.tf
43 terraform apply --auto-approve
44 terraform workspace show
45 terraform workspace delete prod
46 terraform workspace delete test
47 terraform destroy --auto-approve
48 terraform workspace select test
49 terraform workspace delete prod
50 terraform workspace list
51 terraform destroy --auto-approve
52 terraform workspace select dev
53 terraform workspace delete test
54 terraform state list
55 terraform graph
56 terraform destroy --auto-approve
57 vim main.tf
58 terraform apply --auto-approve
59 vim main.tf
60 terraform apply --auto-approve
61 cat main.tf
62 history
=====================================================
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
ami = "ami-06006e8b065b5bd46"
instance_type = "t2.micro"
tags = {
Name = "raham"
}
lifecycle {
prevent_destroy = true
}
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
ami = "ami-06006e8b065b5bd46"
instance_type = "t2.micro"
tags = {
Name = "raham"
}
lifecycle {
prevent_destroy = false
}
}
Prevent Destroy: it will not delete the resources
Ignore changes: it will not replicate the changes we have done to server
Depends on: it will depend on another resource to create.
tERRAFORM REMOTE BACKEND SETUP:
when we create infra the information of resources will store on state file.
so it will be tracking the infra information.
so we need to take backup of that file.
if we lost that file we cant track the infra.
so we prefer to locate the state file on remote loaction.
here im using s3 as remote backend.
provider "aws" {
region = "ap-south-1"
}
terraform {
backend "s3" {
bucket = "rahamshaikterraprodbcuket0088"
key = "prod/terraform.tfstate"
region = "ap-south-1"
}
}
resource "aws_instance" "one" {
ami = "ami-06006e8b065b5bd46"
instance_type = "t2.micro"
tags = {
Name = "raham"
}
}
create Bucket manually
Note: while using new block always we need to run terraform init
otherwise, plugins will not be downloaded
after removing backend setup run this command:
terraform init -migrate-state
TERRAFORM REFRESH: used to refresh and replicate the changes to state file.
it will compare terraform state file with resource.
if it is exsits it will show, otherwise it will not show.
LOCAL RESOURCES:
provider "aws" {
region = "ap-south-1"
}
resource "local_file" "one" {
filename = "abc.txt"
content = "hai all this file is creatd from terraform"
}
VERSION CONSTRAINTS:
it is used to change the provider version plugins.
can be applicable for all providers.
provider "aws" {
region = "ap-south-1"
}
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "<5.22.0"
}
}
}
provider "aws" {
region = "ap-south-1"
}
terraform {
required_providers {
local = {
source = "hashicorp/local"
version = "<2.2.0"
}
}
}
DYNAMIC BLOCK: it is used to reduce the length of code and used for reusabilty of code in loop.
provider "aws" {
}
locals {
ingress_rules = [{
port = 443
description = "Ingress rules for port 443"
},
{
port = 80
description = "Ingree rules for port 80"
},
{
port = 8080
description = "Ingree rules for 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"
}
}
provider "aws" {
region = "ap-south-1"
}
resource "aws_instance" "one" {
count = length(var.instance_type)
ami = "ami-06006e8b065b5bd46"
instance_type = var.instance_type[count.index]
tags = {
Name = var.instance_name[count.index]
}
}
variable "instance_type" {
default = ["t2.medium", "t2.micro", "t2.nano"]
}
variable "instance_name" {
default = ["webserver", "appserver", "dbserver"]
}
FOR_EACH:
provider "aws" {
}
resource "aws_instance" "two" {
for_each = toset(["web-server", "app-server", "db-server"])
ami = "ami-04beabd6a4fb6ab6f"
instance_type = "t2.micro"
tags = {
Name = "${each.key}"
}
}