From the course: Developing Infrastructure as Code with Terraform
Unlock this course with a free trial
Join today to access over 24,900 courses taught by industry experts.
Using for_each and count properties - Terraform Tutorial
From the course: Developing Infrastructure as Code with Terraform
Using for_each and count properties
- So, we've talked about how there's no looping construct in a declarative approach, but sometimes we need to make a bunch of copies of some resource, like say we wanted 10 VMs or something like that in a purely declarative approach. That can be pretty cumbersome, right? So, we wind up with 10 blocks of pretty much the same code over and over again. So, there's a couple of ways that, a couple of methods that Terraform provides us to ease that a little bit. The first one is the count property. So, any resource can have a count property, and that just tells Terraform how many of those things to create. So, in our first example here, we can create 10 of those or 20 of those, and it'll just stamp out as many of those as we say. We get this, when we have the count defined, we get this when variable within the block, which is the count.index, right? So in this case, the buckets all have to have unique names, and so we could just append the number, right? So that would be, we would wind up…
Contents
-
-
-
-
-
-
(Locked)
Learning objectives47s
-
(Locked)
The basics: HCL structure1m 54s
-
Terraform providers4m 8s
-
(Locked)
Terraform data sources and resources4m 3s
-
(Locked)
Dependencies in Terraform7m 39s
-
(Locked)
About expressions in HCL12m 25s
-
(Locked)
Terraform variables and outputs9m 15s
-
(Locked)
Using for_each and count properties9m 47s
-
(Locked)
Resource lifecycle5m 53s
-
(Locked)
Refactoring features3m 15s
-
(Locked)
Provisioners3m 18s
-
(Locked)
-
-
-
-
-
-