-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Problems referencing a new resource using for_each #23026
Description
I added the aws_iam_policy and aws_iam_role_policy_attachment resources shown below to an existing Terraform configuration. They both use for_each to create a resource per entry in the local.node_group_names_with_rexray_plugins map.
During plan, I'm getting an Invalid index error. The aws_iam_role_policy_attachment is saying that a aws_iam_policy resource doesn't already exist. But, it should be created as part of this plan & apply.
Terraform Version
Terraform v0.12.10
+ provider.archive v1.3.0
+ provider.aws v2.31.0
+ provider.dns v2.2.0
+ provider.local v1.4.0
+ provider.template v2.1.2
+ provider.tls v2.1.1
Terraform Configuration Files
Added the following:
resource "aws_iam_policy" "rexray" {
for_each = local.node_group_names_with_rexray_plugins
name = "${module.cluster.config.cluster_name}-${each.key}-rexray"
description = "REX-Ray policies for Docker volumes"
policy = data.aws_iam_policy_document.rexray[each.key].json
}
resource "aws_iam_role_policy_attachment" "rexray" {
for_each = local.node_group_names_with_rexray_plugins
role = local.node_group_iam_roles[each.key].name
policy_arn = aws_iam_policy.rexray[each.key].arn
}Debug Output
Debug Output
2019/10/08 15:21:33 [TRACE] vertex "aws_iam_role_policy_attachment.rexray": entering dynamic subgraph
2019/10/08 15:21:33 [TRACE] dag/walk: updating graph
2019/10/08 15:21:33 [TRACE] dag/walk: added new vertex: "aws_iam_role_policy_attachment.rexray"
2019/10/08 15:21:33 [TRACE] dag/walk: added new vertex: "root"
2019/10/08 15:21:33 [TRACE] dag/walk: added new vertex: "aws_iam_role_policy_attachment.rexray[\"metadata\"]"
2019/10/08 15:21:33 [TRACE] dag/walk: added edge: "root" waiting on "aws_iam_role_policy_attachment.rexray[\"metadata\"]"
2019/10/08 15:21:33 [TRACE] dag/walk: added edge: "root" waiting on "aws_iam_role_policy_attachment.rexray"
2019/10/08 15:21:33 [TRACE] dag/walk: dependencies changed for "root", sending new deps
2019/10/08 15:21:33 [TRACE] dag/walk: visiting "aws_iam_role_policy_attachment.rexray[\"metadata\"]"
2019/10/08 15:21:33 [TRACE] vertex "aws_iam_role_policy_attachment.rexray[\"metadata\"]": starting visit (*terraform.NodeRefreshableManagedResourceInstance)
2019/10/08 15:21:33 [TRACE] NodeRefreshableManagedResourceInstance: aws_iam_role_policy_attachment.rexray["metadata"] has no existing state to refresh
2019/10/08 15:21:33 [TRACE] vertex "aws_iam_role_policy_attachment.rexray[\"metadata\"]": evaluating
2019/10/08 15:21:33 [TRACE] [walkRefresh] Entering eval tree: aws_iam_role_policy_attachment.rexray["metadata"]
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalSequence
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalGetProvider
2019/10/08 15:21:33 [TRACE] dag/walk: visiting "aws_iam_role_policy_attachment.rexray"
2019/10/08 15:21:33 [TRACE] vertex "aws_iam_role_policy_attachment.rexray": starting visit (*terraform.NodeRefreshableManagedResourceInstance)
2019/10/08 15:21:33 [TRACE] NodeRefreshableManagedResourceInstance: aws_iam_role_policy_attachment.rexray will be refreshed
2019/10/08 15:21:33 [TRACE] vertex "aws_iam_role_policy_attachment.rexray": evaluating
2019/10/08 15:21:33 [TRACE] [walkRefresh] Entering eval tree: aws_iam_role_policy_attachment.rexray
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalSequence
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalGetProvider
2019/10/08 15:21:33 [TRACE] EvalWriteOutput: Saving value for output.nodes in state
2019-10-08T15:21:33.819Z [DEBUG] plugin: plugin process exited: path=/Users/king.huang/Repositories/gitlab.transzap.com/spendanalytics/kafka/sa-kafka-ext/.terraform/plugins/linux_amd64/terraform-provider-local_v1.4.0_x4 pid=381
2019-10-08T15:21:33.819Z [DEBUG] plugin: plugin exited
2019/10/08 15:21:33 [TRACE] [walkRefresh] Exiting eval tree: provider.local (close)
2019/10/08 15:21:33 [TRACE] vertex "provider.local (close)": visit complete
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalReadState
2019/10/08 15:21:33 [TRACE] EvalReadState: reading state for aws_iam_role_policy_attachment.rexray["metadata"]
2019/10/08 15:21:33 [TRACE] EvalReadState: no state present for aws_iam_role_policy_attachment.rexray["metadata"]
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalDiff
2019/10/08 15:21:33 [TRACE] GetResourceInstance: aws_iam_policy.rexray is a single instance
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalReadState
2019/10/08 15:21:33 [TRACE] EvalReadState: reading state for aws_iam_role_policy_attachment.rexray
2019/10/08 15:21:33 [TRACE] UpgradeResourceState: schema version of aws_iam_role_policy_attachment.rexray is still 0; calling provider "aws" for any other minor fixups
2019/10/08 15:21:33 [TRACE] GRPCProvider: UpgradeResourceState
2019/10/08 15:21:33 [ERROR] <root>: eval: *terraform.EvalDiff, err: Invalid index: The given key does not identify an element in this collection value.
2019/10/08 15:21:33 [ERROR] <root>: eval: *terraform.EvalSequence, err: Invalid index: The given key does not identify an element in this collection value.
2019/10/08 15:21:33 [TRACE] [walkRefresh] Exiting eval tree: aws_iam_role_policy_attachment.rexray["metadata"]
2019/10/08 15:21:33 [TRACE] vertex "aws_iam_role_policy_attachment.rexray[\"metadata\"]": visit complete
2019/10/08 15:21:33 [TRACE] <root>: eval: *terraform.EvalRefresh
2019/10/08 15:21:33 [TRACE] GRPCProvider: ReadResource
aws_iam_role_policy_attachment.rexray: Refreshing state... [id=sa-kafka-ext-metadata-2019081918572918300000000b]
2019/10/08 15:21:33 [TRACE] EvalWriteOutput: Saving Create change for output.nodes in changeset
2019/10/08 15:21:33 [TRACE] [walkRefresh] Exiting eval tree: output.nodes
2019/10/08 15:21:33 [TRACE] vertex "output.nodes": visit complete
2019/10/08 15:21:34 [TRACE] <root>: eval: *terraform.EvalWriteState
2019/10/08 15:21:34 [TRACE] EvalWriteState: writing current state object for aws_iam_role_policy_attachment.rexray
2019/10/08 15:21:34 [TRACE] [walkRefresh] Exiting eval tree: aws_iam_role_policy_attachment.rexray
2019/10/08 15:21:34 [TRACE] vertex "aws_iam_role_policy_attachment.rexray": visit complete
2019/10/08 15:21:34 [TRACE] dag/walk: upstream of "root" errored, so skipping
2019/10/08 15:21:34 [TRACE] vertex "aws_iam_role_policy_attachment.rexray": dynamic subgraph encountered errors
2019/10/08 15:21:34 [TRACE] vertex "aws_iam_role_policy_attachment.rexray": visit complete
2019/10/08 15:21:34 [TRACE] dag/walk: upstream of "provider.aws (close)" errored, so skipping
2019/10/08 15:21:34 [TRACE] dag/walk: upstream of "root" errored, so skipping
Error: Invalid index
on iam.tf line 110, in resource "aws_iam_role_policy_attachment" "rexray":
110: policy_arn = aws_iam_policy.rexray[each.key].arn
|----------------
| aws_iam_policy.rexray is object with 7 attributes
| each.key is "metadata"
The given key does not identify an element in this collection value.
2019-10-08T15:21:34.234Z [DEBUG] plugin: plugin process exited: path=/Users/king.huang/Repositories/gitlab.transzap.com/spendanalytics/kafka/sa-kafka-ext/.terraform/plugins/linux_amd64/terraform-provider-aws_v2.31.0_x4 pid=395
2019-10-08T15:21:34.235Z [DEBUG] plugin: plugin exited
Crash Output
N/A
Expected Behavior
aws_iam_policy and aws_iam_role_policy_attachment resources should be created, based on the local.node_group_names_with_rexray_plugins map.
Actual Behavior
Terraform reports that the aws_iam_policy policy "metadata" that aws_iam_role_policy_attachment is referencing doesn't already exist. But, it should be created as part of this plan & apply.
Steps to Reproduce
I had an existing Terraform configuration that I added the above two resources to.
If I destroy the rest of the resources in the project and apply everything together, then no error occurs.
Additional Context
N/A
References
N/A