hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.32k stars 9.49k forks source link

Resource count index resets when other resources are in-between #25189

Closed jooh-lee closed 4 years ago

jooh-lee commented 4 years ago

Terraform Version

Terraform v0.12.26
+ provider.archive v1.3.0
+ provider.aws v2.63.0
+ provider.datadog v2.7.0
+ provider.mongodbatlas v0.5.1
+ provider.null v2.1.2
+ provider.random v2.2.1
+ provider.template v2.1.2

Expected Behavior

When using count on a resource, the count index number should be going up regardless of whether there are other resources interspersed in the plan. example:

resource[0] will be created
resource[1] will be created
diff_resource_1 will be deleted
resource[2] will be created
diff_resouce_2 will be deleted
resource[3] will be created

Actual Behavior

In the plan, when a resource that is not the count resource is inbetween, the count.index is reset. I can't show the actual plan, so i'll replicate what I'm seeing with different names and what not:

 #module.foo.aws_security_group_rule.ingress[0] will be created
 #module.foo.aws_security_group_rule.ingress[1] will be created
 #module.foo.aws_security_group_rule.ingress[2] will be created
 #module.foo.aws_security_group_rule.ingress.key1 will be deleted
 #module.foo.aws_security_group_rule.ingress.key2 will be deleted
 #module.foo.aws_security_group_rule.ingress.key3 will be deleted
 #module.foo.aws_security_group_rule.ingress[0] will be created
 #module.foo.aws_security_group_rule.ingress[1] will be created
 #module.foo.aws_security_group_rule.ingress.key4 will be deleted
 #module.foo.aws_security_group_rule.ingress.key5 will be deleted
 #module.foo.aws_security_group_rule.ingress[0] will be created
 #module.foo.aws_security_group_rule.ingress[1] will be created
 ...

The above is a portion of the plan and is the exact renamed replica of what I'm seeing

Steps to Reproduce

  1. terraform init
  2. create resources with for_each
  3. terraform plan
  4. terraform apply
  5. replace the resource for_each with count with any relevant changes to get it valid
  6. terraform plan
apparentlymart commented 4 years ago

Hi @jooh-lee,

Unfortunately I think what you were trying to communicate has been lost in the editing you did, because the edited example you've shared describes an impossible situation: module.foo.aws_security_group_rule.ingress.key1 is not a resource address that can exist, because it has too many dot-separated parts.

I understand that you cannot show the plan from the configuration you are currently working on, but in order to understand the behavior you're seeing I'd ask you to write a new, smaller configuration that includes only enough to illustrate the problem (feel free to use null_resource resources instead of "real" resource types to avoid depending on real remote APIs), and then share that configuration in full along with the exact terraform plan output you saw in response to it, and what you had expected it to show instead.

Thanks!

apparentlymart commented 4 years ago

Hi again, @jooh-lee!

We didn't see another message for you after my earlier comment asking for more information, so I'm going to close this assuming that you either cannot provide that information or you've found a different way to resolve the problem. If you'd like to continue working on this bug report with us, please let us know and we'll reopen this. Thanks!

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.