{"action":"created","issue":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/16580","repository_url":"https://api.github.com/repos/hashicorp/terraform","labels_url":"https://api.github.com/repos/hashicorp/terraform/issues/16580/labels{/name}","comments_url":"https://api.github.com/repos/hashicorp/terraform/issues/16580/comments","events_url":"https://api.github.com/repos/hashicorp/terraform/issues/16580/events","html_url":"https://github.com/hashicorp/terraform/issues/16580","id":271852546,"number":16580,"title":"Accessing attributes from resources with count = 0 errors out since version 0.11","user":{"login":"janschumann","id":276364,"avatar_url":"https://avatars0.githubusercontent.com/u/276364?v=4","gravatar_id":"","url":"https://api.github.com/users/janschumann","html_url":"https://github.com/janschumann","followers_url":"https://api.github.com/users/janschumann/followers","following_url":"https://api.github.com/users/janschumann/following{/other_user}","gists_url":"https://api.github.com/users/janschumann/gists{/gist_id}","starred_url":"https://api.github.com/users/janschumann/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/janschumann/subscriptions","organizations_url":"https://api.github.com/users/janschumann/orgs","repos_url":"https://api.github.com/users/janschumann/repos","events_url":"https://api.github.com/users/janschumann/events{/privacy}","received_events_url":"https://api.github.com/users/janschumann/received_events","type":"User","site_admin":false},"labels":[{"id":85946957,"url":"https://api.github.com/repos/hashicorp/terraform/labels/bug","name":"bug","color":"f7c6c7","default":true},{"id":608979767,"url":"https://api.github.com/repos/hashicorp/terraform/labels/config","name":"config","color":"0e8a16","default":false}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2017-11-07T14:34:42Z","updated_at":"2017-11-07T18:17:54Z","closed_at":null,"author_association":"CONTRIBUTOR","body":"Hi,\r\n\r\nconsider a configuration where specific resources should only be applied for specific environments. This is accomplished by conditionally set the count argument of a resource to 0 or 1 respectively:\r\n\r\n```hcl \r\nresource \"aws_s3_bucket\" \"b\" {\r\n  count = \"${terraform.workspace == \"production\" ? 0 : 1}\"\r\n  bucket = \"my-tf-test-bucket\"\r\n  acl    = \"private\"\r\n\r\n  tags {\r\n    Name        = \"My bucket\"\r\n    Environment = \"Dev\"\r\n  }\r\n}\r\n```\r\n\r\nSince terraform 0.11, testing on a resource with `count = 0` errors out with ```Resource 'aws_s3_bucket.b' not found for variable 'aws_s3_bucket.b.bucket'```\r\n\r\nIs this the expected behavior? From my point of view, the resource should exist and a test on the resources `count` attribute should be possible.\r\n\r\n### Terraform Version\r\nTerraform v0.11.0-dev\r\n\r\n### Terraform Configuration Files\r\n```hcl\r\nresource \"aws_s3_bucket\" \"b\" {\r\n  count = 0\r\n  bucket = \"my-tf-test-bucket\"\r\n  acl    = \"private\"\r\n\r\n  tags {\r\n    Name        = \"My bucket\"\r\n    Environment = \"Dev\"\r\n  }\r\n}\r\n\r\noutput \"testbucket\" {\r\n  value = \"${aws_s3_bucket.b.count > 0 ? aws_s3_bucket.b.bucket : \"\"}\"\r\n}\r\n```\r\n\r\n### Expected Behavior\r\nOutput `testbucket` should be empty string w/o error\r\n\r\n### Actual Behavior\r\nPlan reports the following error:\r\n```\r\n* output.testbucket: Resource 'aws_s3_bucket.b' not found for variable 'aws_s3_bucket.b.bucket'\r\n```\r\n\r\n### Steps to Reproduce\r\nPlease list the full steps required to reproduce the issue, for example:\r\n1. `terraform plan`\r\n"},"comment":{"url":"https://api.github.com/repos/hashicorp/terraform/issues/comments/342573652","html_url":"https://github.com/hashicorp/terraform/issues/16580#issuecomment-342573652","issue_url":"https://api.github.com/repos/hashicorp/terraform/issues/16580","id":342573652,"user":{"login":"apparentlymart","id":20180,"avatar_url":"https://avatars1.githubusercontent.com/u/20180?v=4","gravatar_id":"","url":"https://api.github.com/users/apparentlymart","html_url":"https://github.com/apparentlymart","followers_url":"https://api.github.com/users/apparentlymart/followers","following_url":"https://api.github.com/users/apparentlymart/following{/other_user}","gists_url":"https://api.github.com/users/apparentlymart/gists{/gist_id}","starred_url":"https://api.github.com/users/apparentlymart/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apparentlymart/subscriptions","organizations_url":"https://api.github.com/users/apparentlymart/orgs","repos_url":"https://api.github.com/users/apparentlymart/repos","events_url":"https://api.github.com/users/apparentlymart/events{/privacy}","received_events_url":"https://api.github.com/users/apparentlymart/received_events","type":"User","site_admin":false},"created_at":"2017-11-07T18:17:54Z","updated_at":"2017-11-07T18:17:54Z","author_association":"CONTRIBUTOR","body":"Hi @janschumann! Sorry for this annoying behavior.\r\n\r\nI'm surprised to hear that this behavior feels new on 0.11.0 since it's actually a known bug that has existed ever since the conditional expression was implemented, due to some limitations of the interpolation language. (See hashicorp/hil#50 for more details.)\r\n\r\nThe common workaround today is to exploit the fact that the \"splat syntax\" can return an empty list when `count = 0` and handle this via list operations:\r\n\r\n```hcl\r\noutput \"testbucket\" {\r\n  value = \"${element(concat(aws_s3_bucket.b.*.bucket, list(\"\")), 0)}\"\r\n}\r\n```\r\n\r\nThe additional single-item list here is to ensure that there's always at least one item in this list so that `element` will work.\r\n\r\nThe plan is to address this as part of implementing [the new version of HCL](https://github.com/hashicorp/hcl2) which incorporates the HIL (interpolation language) functionality while addressing many of the limitations of HIL's type system and evaluator that lead to issues like this. We'll have an opt-in preview version of this soon to gather feedback and find bugs before making Terraform use it by default.\r\n\r\n"}}
{"actor":{"display_login":"apparentlymart"}}