Terraform: ERROR: "Count"set, its attributes must be accessed on specific instances

Created on 30 Oct 2019  ยท  3Comments  ยท  Source: hashicorp/terraform

Description

When using the aws_iam_role_policy_attachment resource AND using the count attribute I keep getting an error stating :

 on pipeline-gheintegrated/iam.tf line 294, in resource "aws_iam_role_policy_attachment" "codebuild_acct_specific":
 294:   policy_arn = aws_iam_policy.codebuild_acct_specific.arn

Because aws_iam_policy.<resource_name> has "count" set, its attributes
must be accessed on specific instances.

For example, to correlate with indices of a referring resource, use:
    aws_iam_policy.codebuild_acct_specific[count.index]

Not sure why I would need to use [count.index] in this scenario since it either creates the resource or it doesn't.

Configuration Snippet

resource "aws_iam_policy" "codebuild_acct_specific" {
  count = var.target_acct_id == "NOT_APPLICABLE" ? 0 : 1
  name = "pipeline-cb-acct-${var.app_key}-${var.environment}"
  path = "/pipeline/"

policy = <<POLICY
 {...}
POLICY
}

resource "aws_iam_role_policy_attachment" "codebuild_acct_specific" {
  count = var.target_acct_id == "NOT_APPLICABLE" ? 0 : 1
  role       = aws_iam_role.codebuild.id
  policy_arn = aws_iam_policy.codebuild_acct_specific.arn
}

Expected Behavior

Create aws_iam_role_policy_attachment resource without count error

TF Version

Terraform v0.12.12

Most helpful comment

Not sure why this was closed as this was working in TF v11.14 and was an issue as the result of an upgrade.

All 3 comments

Hello!

We use GitHub issues for tracking bugs and enhancements, rather than for questions. While we can sometimes help with certain simple problems here, it's better to use the community forum where there are more people ready to help. The GitHub issues here are monitored only by our few core maintainers.

Since this issue doesn't represent a specific bug or feature request, We're going to close it. Please do feel free to ask your question in the community forum. Thanks!

Not sure why this was closed as this was working in TF v11.14 and was an issue as the result of an upgrade.

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.

Was this page helpful?
0 / 5 - 0 ratings