Error: Invalid index

Hi,

Actually I have this problem, to execute terraform plan, and I not have idea to solve, I review all configurations and is correct and the code I not have problems, is desesperating beucause not exists more information for this problems.

Any idea to solve, please?? and Thank you for helm me.


Error: Invalid index

on .terraform/modules/eks/terraform-aws-modules-terraform-aws-eks-1be1a02/cluster.tf line 52, in resource “aws_security_group_rule” “cluster_egress_internet”:
52: security_group_id = aws_security_group.cluster[0].id
|----------------
| aws_security_group.cluster is empty tuple

The given key does not identify an element in this collection value.


1 Like

It means that there is nothing in the aws_security_group.cluster[0] variable, because it’s not been created, and so there is no id attribute.
Probably because you’re missing a parameter in the call to the module, so check the variables defined inside it.
Is it a module you wrote yourself?

I had similar issue and for me I was working in test environment. So I did a complete cleanup:

  1. Deleted the .terraform folder
  2. Deleted the S3 state file
  3. Delete the DynamoDB state lock record.

Then did terraform init and terraform plan and it finally worked for me.

For me the issue was that I changed the state file and the dynamoDB record which was probably causing the issue.

3 Likes

Thanks for posting this…Just had a similar issue in Azure. Deleted the remote state file in the Blob Container and the local .tfstate and terraform planned as expected again. I had forgotten that I manually added some resources to the environment via the Azure Portal which weren’t part of the original script, then destroyed it and manually deleted the remnants. All of that extra activity confused the heck out of the state files and rightly so.

1 Like

For me it was a resource that I manually deleted but still record in remote tfstate. I have to use azure state rm to remove it. Don’t forget your quotes around the resource when using azure state rm.