From the course: Advanced Terraform

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Looping with count

Looping with count

- [Instructor] If you're familiar with any programming language you may have worked with loops. Loops repeat a set of instructions. Loops simplify code by reducing repetition and reducing the number of lines necessary to perform an action. Some loops use a simple numerical counter. Other loops iterate over a collection of items. Terraform and the HashiCorp Configuration Language support both these types of loops. We have three nearly identical compute instances in this configuration, so let's use a loop to cut it down to size. The only thing that differs between our three web servers are their names, which follow a web number convention, web1, web2, and web3. We can easily shorten this using the count meta-argument. First, let's run a quick check to make sure you don't have any instances deployed. In your terminal execute gcloud compute instances list. You should see listed zero items. If you have instances from a…

Contents