Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Variables used only in provider blocks are reported as missing #14940

Closed
elliottyates opened this issue May 30, 2017 · 11 comments
Closed

Variables used only in provider blocks are reported as missing #14940

elliottyates opened this issue May 30, 2017 · 11 comments

Comments

@elliottyates
Copy link

Thanks for any help you might be able to provide. This looks very similar to #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

@l85m
Copy link

l85m commented Jun 24, 2017

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

@s2504s
Copy link

s2504s commented Aug 1, 2017

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:

@Chili-Man
Copy link
Contributor

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.

@apparentlymart
Copy link
Contributor

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.

@apparentlymart apparentlymart changed the title Module using postgresql provider fails on import with "unknown variable accessed" Variables used only in provider blocks are reported as missing Aug 26, 2017
@denniswebb
Copy link
Contributor

@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.

@bbbush
Copy link

bbbush commented Nov 7, 2017

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.

@sebglon
Copy link

sebglon commented Nov 13, 2017

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.

@sstarcher
Copy link

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

@kaijparo
Copy link

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

@apparentlymart
Copy link
Contributor

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.

@ghost
Copy link

ghost commented Apr 5, 2020

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.

@ghost ghost locked and limited conversation to collaborators Apr 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

10 participants