Terraform - Repeat Task using Count

by
Jeremy Canfield |
Updated: July 20 2023
| Terraform articles
count can be used to:
- do something based on whether a condition evaluates to true or false (see this article)
- repeat a task "x" times (this article)
Let's say you have the following files on your Terraform server.
├── locals.tf
├── modules.tf
├── outputs.tf
├── provider.tf
├── terraform.tfstate
├── variables.tf
├── child (directory, child module)
│ ├── data.tf
│ ├── outputs.tf
│ ├── resources.tf
Perhaps resources.tf in your child module is being used to create/update/delete Amazon Web Services (AWS) Virtual Private Clouds (VPC).
Here is an example of how the count function can be used. In this example, two Amazon Web Services (AWS) Virtual Private Cloud (VPC) will be created, my-vpc-0 and my-vpc-1.
resource "aws_vpc" "my-vpc" {
count = 2
cidr_block = "10.0.0.0/16"
tags = {
Name = "my-vpc-${count.index}"
}
}
Did you find this article helpful?
If so, consider buying me a coffee over at