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

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