Error: Duplicate variable declaration - Need to delete .terraform folder

I have a separate module with its own State File for the Networking resources in AWS. I’m using the terraform-aws-modules/vpc/aws and terraform-aws-modules/vpc/aws//modules/vpc-endpoints modules for this.

I have an S3 bucket for my backend and a DynamoDB table to control the locking. There is already infrastructure deployed, and when I modify something and try to apply the terraform plan command, I get the following messages:

╷
│ Error: Duplicate variable declaration
│
│   on .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/variables.tf line 25:
│   25: variable "subnet_ids" {
│
│ A variable named "subnet_ids" was already declared at .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/variables 3.tf:25,1-22. Variable names must be unique within a module.
╵
╷
│ Error: Duplicate variable declaration
│
│   on .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/variables.tf line 31:
│   31: variable "tags" {
│
│ A variable named "tags" was already declared at .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/variables 3.tf:31,1-16. Variable names must be unique within a module.
╵
╷
│ Error: Duplicate variable declaration
│
│   on .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/variables.tf line 37:
│   37: variable "timeouts" {
│
│ A variable named "timeouts" was already declared at .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/variables 3.tf:37,1-20. Variable names must be unique within a module.
╵
╷
│ Error: Duplicate required providers configuration
│
│   on .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/versions.tf line 4, in terraform:
│    4:   required_providers {
│
│ A module may have only one required providers configuration. The required providers were previously configured at .terraform/modules/vpc_endpoints_nocreate/modules/vpc-endpoints/versions 3.tf:4,3-21.

(and many more like the above)

If I do the following:

  1. rm -rf .terraform && .terraform.lock.hcl
  2. terraform init
  3. terraform workspace select prod
  4. terraform refresh
  5. terraform plan
  6. terraform apply

It will work. But then after the apply is completed, if I modify something again and try to perform terraform plan I get the same errors… then, I need to redo the steps above in order for me to be able to plan/apply the changes.

What can I do here? Any help is much appreciated!

Terraform v1.1.5
on darwin_arm64
+ provider registry.terraform.io/hashicorp/aws v3.74.1
+ provider registry.terraform.io/hashicorp/template v2.2.0

Hi @lpossamai,

Looking at the source code of that vpc-endpoints module I don’t see any file named endpoints 3.tf in that repository, but yet one is mentioned in the error message here.

It seems that something on your system is duplicating the variables.tf file into another file named variables 3.tf, but I don’t know what would be doing that. Terraform itself does not create new .tf files as part of terraform apply, and I don’t see anything in that module that would cause it to write new files into its own source directory.

I’m quite honestly stumped at what might be causing this. That naming scheme of adding a space and then a number seems like something an automated system might do, but I don’t know what sort of automated system would be modifying Terraform module source code. Do you have anything installed on your computer that might do something like that?

Yeah… I don’t know either… I think there is something broken here. But the interesting part is that it only happens with that module. I have other modules that are working fine.