Terraform: Variables used only in provider blocks are reported as missing

Created on 30 May 2017  ยท  11Comments  ยท  Source: hashicorp/terraform

Thanks for any help you might be able to provide. This looks very similar to https://github.com/hashicorp/terraform/issues/10711 (which I mention in references, below -- I didn't know if I should be continuing that closed/merged issue conversation or starting a new one).

Terraform Version

0.9.4, 0.9.6

Affected Resource(s)

Appears when defining postgresql provider inside a module. Works as expected if provider is defined in main tf file.

Terraform Configuration Files

In main tf file:

module "db" {
  db_location = "${var.db_location}"
  db_name = "${var.db_name}"
  db_password = "${var.db_password}"
  db_user = "${var.db_user}"
  source = "../modules/db"
}

In module variables:

variable "db_location" {
  description = "Network address of POSTGRES DB"
}
variable "db_name" {
  description = "POSTGRES DB name"
}
variable "db_password" {
  description = "POSTGRES DB password for user connection to db_name"
}
variable "db_port" {
  description = "POSTGRES DB port at db_location"
  default = 5432
}
variable "db_user" {
  description = "POSTGRES DB user string"
}

In module:

provider "postgresql" {
  host = "${var.db_location}"
  port = "${var.db_port}"
  username = "${var.db_user}"
  password = "${var.db_password}"
}

resource "postgresql_database" "default" {
  provider = "postgresql"
  name = "${var.db_name}"
}

Debug Output

Command run

terraform import   -var-file="configurations/development/my.tfvars" module.db.postgresql_database.default dbname

Output

Error importing: 1 error(s) occurred:

* module.db.provider.postgresql: 1:3: unknown variable accessed: var.db_location in:

${var.db_location}

The reported unknown variable changes each time, but is one of the ones referenced in the provider block.

Expected Behavior

I would expect to be able to import just as well if the provider is defined in the module.

Actual Behavior

I cannot import unless I've defined the provider inline (the resource can remain in the module and refer to the provider defined outside of the module).

Steps to Reproduce

Command (see above):

terraform import   -var-file="configurations/development/my.tfvars" module.db.postgresql_database.default dbname

Important Factoids

I've tried this both with -var-file and without, and it doesn't appear to make any difference.

References

bug core

Most helpful comment

For me if i put the provider on my main.tf and not on my module, this work good.
bit if i put provider on module i have the same error.

All 11 comments

I'm seeing this with pagerduty provider as well, so i don't think it's related specifically to the postgresql provider.

I've the same issue
Terraform v0.9.11

$ cat terraform.tfvars
namespace = "namespace1"
stage = "stage1"
name = "name1"
$ terraform plan  -var-file="terraform.tfvars" 
Error asking for user input: 3 error(s) occurred:

* module.tf_label.var.namespace: 1:3: unknown variable accessed: var.namespace in:

I'm hitting this issue as well with

Terraform v0.10.2

The problem seems to be if a variable is only used within a provider and module section, then it will throw the unknown variable error; However, if you slap that variable as an output, it fixes the problem.

That sounds like a good clue, @Chili-Man! Thanks for noting that detail.

It sounds like the bug is that Terraform's graph builder is doing some "pruning" here and not detecting that the variables are in fact being used in the provider block. I'll update the summary to cover that detail.

@Chili-Man At what level did you make it an output? I tried at the module level where the variable/provider are declared but still got the same error.

with v0.10.8 and when running import (the resource to import is an ECS repository), this module raised similar error:

provider "aws" {
  version = "~> 1.2"
  region = "${var.aws_region}"
  profile = "${var.aws_profile}"
}

variable "aws_region" {
}

variable "aws_profile" {
}

it seems default values in module are required and will be used by the "import" command line, which is unexpected.

For me if i put the provider on my main.tf and not on my module, this work good.
bit if i put provider on module i have the same error.

When adding the variable to the output my error does not go away.

seeing the same, how are you guys getting around this?

Hi all,

Thanks for all of the additional context here, and sorry for the long silence.

Looking at this issue again after some time away, I notice that many of you are talking about terraform import, though there was also one mention of terraform plan.

For terraform import issues, this seems like the same problem discussed in #13018: you can currently only import with a provider whose configuration is only constants, because variables are not provided to the import command. There are some workarounds discussed in that issue, but indeed also some limitations.

Given that this seems to be the same issue, I'm going to close this one just to consolidate the discussion over in #13018.

@s2504s as far as I can tell you are the only one in this thread having a problem with terraform plan rather than terraform import, and that makes me suspect that you've encountered a different problem that happens to produce the same error message. If you're still having that problem, I'd recommend opening a new top-level issue to discuss it, so we can see the config you're using and the trace logs.

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings