Loading...
Loading...
### Terraform Version ``` 0.12.9 0.12.10-dev ``` ### Terraform Configuration Files A variable number of resource: ```hcl locals { resource_count = { type1 = { subtypeA = 2 } } resource_data = { type1 = { subtypeA = null_resource.type1A } } } resource "null_resource" "type1A" { count = local.resource_count.type1.subtypeA # ... } ``` which are passed into a module: ```hcl module "mod" { # ... resources = local.resource_data.type1.subtypeA # (Just to explain why I have thart structure, it'd actually look like: # resources = concat( # [ for r in local.resource_data.type1.subtypeA : { ... } ], # [ for r in local.resource_data. ... : { ... } ], # ... # ) # but it's not required for the reproduction.) } ``` which creates resources for each one: ```hcl locals { servers_idx = toset(range(length(var.servers))) servers = { for idx in local.servers_idx : idx => var.servers[idx] } } resource "null_resource" "whatever" { for_each = local.servers # ... } ``` ### Expected Behavior Dropping the resource count from `2` to `1` should impact all resources using it as a `count`/`for_each` in the module. ### Actual Behavior `null_resource.type1A` is correctly destroyed, but index `1` resources using the `local.resource_data` are _replaced_ (in the plan anyway - applying will as a result fail with a cycle). ### Steps to Reproduce 1. `terraform apply` 1. Alter `local.resource_count.type1.subtypeA` from `2` to `1` 1. `terraform plan` ### Additional Context The issue seems to be that the number of items for all resources is evaluated simultaneously and without considering dependencies between them - so since we already have `null_resource.type1A` in the state, it's `length( . )` is used to compute counts for other resource despite that it will change in the current plan. Thus: #### Workaround: Key `local.resource_data` directly on the static counts rather than implicit dependency through `null_resource.type*`: ```hcl locals { resource_data = { type1 = { subtypeA = { for idx in range(local.resource_count.type1.subtypeA) : idx => null_resource.type1A[idx] } } } } ``` ### References
Click on a version to see all relevant bugs
Terraform Integration
Learn more about where this data comes from
Bug Scrub Advisor
Streamline upgrades with automated vendor bug scrubs
BugZero Enterprise
Wish you caught this bug sooner? Get proactive today.