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
41.67k stars 9.41k forks source link

Possible Regression -> Error: Reference to "count" in non-counted context #21477

Closed cappetta closed 5 years ago

cappetta commented 5 years ago

I'm seeing this error on terraform v0.12.0 in my circleci env. When I revert the terraform container image back to v0.11.4 it passes as expected. I suspect there is a regression issue. Also note curl had to be added into the container on line 40

Output on build failure 287

Error: Reference to "count" in non-counted context

on ../../modules/infrastructure/modules/secdevops/malware.tf line 53, in resource "aws_instance" "cuckoo-host-ubuntu": 53: subnet_id = "${element(local.malware_subnet_ids, count.index)}"

The "count" object can be used only in "resource" and "data" blocks, and only when the "count" argument is set.

jbardin commented 5 years ago

Hi @cappetta,

Can you confirm that count.index is set on the resource that is causing the error? A complete sample config may help track down the the issue. It also appears that you may have missed running the 0.12upgrade tool on this particular config, which should point out this type of error. Double check that you've gone through the upgrade guide when switching to 0.12.

cappetta commented 5 years ago

Thanks for highlighting the upgrade tool. I'll perform that check/analysis and review the release notes a bit closer. Hoping to circleback over the weekend. RE: sample config - here is a link to the count.index reference

apparentlymart commented 5 years ago

Hi @cappetta!

The resource indicated in the error doesn't seem to have count set in your config, so the error you got here is correct. In Terraform 0.11 count.index would've always been zero here, but we made it into an explicit error in 0.12 to help users see when they have made a (common, it turns out) error if forgetting to set count and then inadvertently connecting all instances of a downstream resource to only the first instance of upstream.

The fix here would be to replace count.index with 0 if that is the behavior you were expecting, or to set the count meta-argument if not. I think in this case the upgrade tool may not be able to totally fix this, but I would still recommend running it first and using it as a baseline for subsequent updates.

cappetta commented 5 years ago

Thanks Martin. Great eyes - I upgraded multiple installs, performed the 0.12upgrade chk, confirmed CircleCI was green, & all is looking good. Error was on my end, nice to see the alert of the issue & appreciate the quick assist.

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.