Breaking News: Grepper is joining You.com. Read the official announcement!
Check it out

count and for_each Have Limitations

Sumit Rawal answered on May 14, 2023 Popularity 1/10 Helpfulness 1/10

Contents


More Related Answers


count and for_each Have Limitations

0

In the examples in this blog post, you made extensive use of the count parameter and for_each expressions in loops and if-statements. This works well, but there’s an important limitation that you need to be aware of: you cannot reference any resource outputs in count or for_each.

Imagine that you want to deploy multiple EC2 Instances, and for some reason you didn’t want to use an ASG. The code might look like this:

resource "aws_instance" "example_1" {

count = 3

ami = "ami-0fb653ca2d3203ac1"

instance_type = "t2.micro"

}

Because count is being set to a hardcoded value, this code will work without issues, and when you run apply, it will create three EC2 Instances. Now, what if you want to deploy one EC2 Instance per Availability Zone (AZ) in the current AWS region? You could update your code to fetch the list of AZs using the aws_availability_zones data source and use the count parameter and array lookups to “loop” over each AZ and create an EC2 Instance in it:

Popularity 1/10 Helpfulness 1/10 Language whatever
Source: Grepper
Tags: count whatever
Link to this answer
Share Copy Link
Contributed on May 14 2023
Sumit Rawal
0 Answers  Avg Quality 2/10


X

Continue with Google

By continuing, I agree that I have read and agree to Greppers's Terms of Service and Privacy Policy.
X
Grepper Account Login Required

Oops, You will need to install Grepper and log-in to perform this action.