count
What is count?
count: Creates N copies of a resource. Each instance is addressed by index — resource[0], resource[1]. count in Terraform creates several copies of a resource from one block. count = 3 creates three servers, addressed as aws_instance.web[0], [1] and [2]. count.index gives each one its number. It is also used as a simple on and off switch: count = var.enable_monitoring ? 1 : 0 creates the resource only when the variable is true.
Frequently asked questions about count
What should you keep in mind with count?
Be careful with lists. If you use count over a list of names and remove one from the middle, every item after it shifts position. Terraform then destroys and recreates resources that did not really change. For collections with names, use for_each instead, which keys each instance by name. Keep count for identical copies and the on and off pattern.
What does an example of count look like?
resource "aws_instance" "web" { count = 3 instance_type = "t3.micro" } At a glance
| Category | Terraform |
|---|---|
| Explained by | Reinier de la Parra |
| Updated |