Skip to content

terraform import on module resource fails to read values in terraform.tfvars, even though plan/apply work #16139

@pcockwell

Description

@pcockwell

When using a module to define resources that get used multiple times over, and wanting to import existing use cases for for this module (for example github user membership), the import fails to recognize variables defined at the top level outside of the module. This is despite a working plan or apply.

Terraform Version

Terraform v0.9.11

Terraform Configuration Files

# test_user.tf
variable "github_org_owner_token" {
  description = "The account corresponding to this token will need 'owner' privileges for the GitHub organization"
}

module "test_user" {
  source = "./modules/github_membership/"

  github_org_owner_token = "${var.github_org_owner_token}"
  github_username = "pcockwell"
}
# terraform.tfvars
github_org_owner_token = "REDACTED"
# modules/github_membership/variables.tf
variable "github_org_owner_token" {
  description = "The account corresponding to this token will need 'owner' privileges for the GitHub organization"
}
variable "github_username" {}
variable "github_role" {
  default = "member"
}
# modules/github_membership/github.tf
provider "github" {
  token        = "${var.github_org_owner_token}"
  organization = "REDACTED"
}

resource "github_membership" "membership" {
  username = "${var.github_username}"
  role     = "${var.github_role}"
}

Debug Output

$ terraform plan -target=module.test_user
+ module.test_user.github_membership.membership
    role:     "member"
    username: "pcockwell"


Plan: 1 to add, 0 to change, 0 to destroy.
$ terraform import module.test_user.github_membership.membership REDACTED:pcockwell
Error importing: 1 error(s) occurred:

* module.test_user.provider.github: 1:3: unknown variable accessed: var.github_org_owner_token in:

${var.github_org_owner_token}

Expected Behavior

Terraform successfully imports resource

Actual Behavior

Terraform fails complaining about an unknown variable being accessed

Steps to Reproduce

Please list the full steps required to reproduce the issue, for example:

  1. terraform init
  2. terraform plan -target=module.test_user.github_membership.membership
  3. terraform import module.test_user.github_membership.membership REDACTED:pcockwell

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