Skip to content

terraform destroy error when referencing a random_id resource in a module's locals block #18424

@metmajer

Description

@metmajer

Terraform Version

Terraform v0.11.7
+ provider.aws v1.26.0
+ provider.random v1.3.1

Terraform Configuration Files

provider "aws" {}
provider "random" {}

resource "random_id" "random" {
  byte_length = 8
}

module "aws-ec2-instance" {
  source = "github.com/metmajer/terraform-aws-ec2"

  name = "${var.name}"
  uuid = "${random_id.random.hex}"
}

Debug Output

See https://gist.github.com/metmajer/11063a52fb166a1d228fe115b637da91#file-terraform-destroy-log.

Expected Behavior

terraform destroy should not complain about a nil reference in random_id.

Actual Behavior

In a newly initialized workspace, terraform destroy issues the following error:

Error: Error applying plan:

1 error(s) occurred:

* module.aws-ec2-instance.var.uuid: variable "random" is nil, but no error was reported

Steps to Reproduce

  1. terraform init
  2. terraform destroy

Additional Context

I could only reproduce the issue if ${random_id.random.hex} is passed on to a module AND if the module references the passed-on variable in a locals block. See ${var.uuid} in https://github.com/metmajer/terraform-aws-ec2/blob/master/main.tf.

If https://github.com/metmajer/terraform-aws-ec2/blob/master/main.tf was changed to https://github.com/metmajer/terraform-aws-ec2/blob/no-locals/main.tf, the error does not occur.

It appears that I needed to rm -rf terraform.tfstate* .terraform; terraform init before validating that the error does not occur with https://github.com/metmajer/terraform-aws-ec2/blob/no-locals/main.tf. Otherwise, the error persisted.

References

Related issues are:

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions