hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.42k stars 9.5k forks source link

Variable interpolation bug when using terraform plan -target=module.XXXX #12009

Closed panmanphil closed 7 years ago

panmanphil commented 7 years ago

We have hit what we believe to be a bug with the -target option to terraform plan. I've put a sample the shows the problem at https://github.com/panmanphil/terraform-module-target-bug

The essence of the problem is that if you have a multiple module configuration, and want to plan and apply a subset to more properly layer the deployment, it fails because modules you have not targeted that also have a provider section, generate an error if they use variables.

terraform plan -target=module.cluster -var "aws_profile=yourvalidprofie" -var "aws_region=us-east-1" -var "env_name=dev"

Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.

Error running plan: 1 error(s) occurred:

* 1:3: unknown variable accessed: var.aws_profile in:

${var.aws_profile}

Terraform Version

Run terraform -v to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.

Terraform v0.8.6

Affected Resource(s)

Not specific to any resource

Terraform Configuration Files

https://github.com/panmanphil/terraform-module-target-bug

Expected Behavior

The module listed in the -target option should have been planned with any dependencies

Actual Behavior

Error message about a missing variable. This is only true for variables used in the provider block of modules not targeted:

provider aws {
  profile = "${var.aws_profile}"
  region = "${var.aws_region}"
}

Steps to Reproduce

Please list the steps required to reproduce the issue, for example: shown in the description above

Important Factoids

this is an aws stack. In my two module example, using the target as show, some setup for an ecs cluster, the other module, and ecs task and service, is not a dependency but it's provider block causes the problem. If I put valid values in that task module's provider sections, it works as expected, even though the same variables are used elsewhere in the module.

If I target the task, because it depends on the cluster, the error does not occur and both the task and cluster are in the plan.

apparentlymart commented 7 years ago

Hi @panmanphil! Sorry for the issue here.

I think this is the same problem as #10911, which was just fixed in the 0.8.7 release. Could you give this a try on 0.8.7 to confirm whether it is indeed the same issue and fixed?

panmanphil commented 7 years ago

Tried it, and unfortunately, no change, the error is still there

terraform plan -target=module.cluster -var "aws_profile=myprofile" -var "aws_region=us-east-1" -var "env_name=dev"
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but
will not be persisted to local or remote state storage.

Error running plan: 1 error(s) occurred:

* 1:3: unknown variable accessed: var.aws_profile in:

${var.aws_profile}
PhilipNelsonMacbook:tf-target-bug philipnelson$ terraform -v
Terraform v0.8.7
panmanphil commented 7 years ago

I updated the issue as well, but with 0.8.7 the issue remains. dang!

On Thu, Feb 16, 2017 at 10:39 AM, Martin Atkins notifications@github.com wrote:

Hi @panmanphil https://github.com/panmanphil! Sorry for the issue here.

I think this is the same problem as #10911 https://github.com/hashicorp/terraform/issues/10911, which was just fixed in the 0.8.7 release. Could you give this a try on 0.8.7 to confirm whether it is indeed the same issue and fixed?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hashicorp/terraform/issues/12009#issuecomment-280384610, or mute the thread https://github.com/notifications/unsubscribe-auth/ACG9Efu6j9F8c4rUUJOIfNEVyhmuW61dks5rdHvGgaJpZM4MDDYQ .

apparentlymart commented 7 years ago

Thanks for trying it, @panmanphil. Probably a new issue with similar symptoms then, so I'll leave this here to track it.

ashald commented 7 years ago

Coming here from #12024 - ran into the same issue earlier today. I'm wondering whether it ever worked (in the sense that maybe we can try some older version to figure out when it became broken).

mitchellh commented 7 years ago

Have a fix in a PR, will backport to 0.8.x once merged.

beenanner commented 7 years ago

I'm still seeing issue with this in v0.9.11. Is there something I can do to provide more debugging details?

terraform plan -var image="helloworld" works and shows the image is changing on everything, but terraform plan -target=module.A -var image="helloworld" returns "No changes. Infastructure is up-to-date."

beenanner commented 7 years ago

Strangely hardcoding the value in the downstream modules doesn't work either via the folder in .terraform/modules.

beenanner commented 7 years ago

Confirmed -target working on resources in main tf that are not within a module.

terraform plan -target=google_compute_instance.test

apparentlymart commented 7 years ago

Hi @beenanner!

Lots has changed in Terraform's graph builder code since this bug was originally filed and fixed, so I expect the details are different for you now. It would be helpful if you could open a new issue and fill in the details you'll see requested in the issue template, and then we can hopefully figure out what's going on here.

beenanner commented 7 years ago

@apparentlymart absolutely thanks for the quick response!

beenanner commented 7 years ago

Sorry for the delay @apparentlymart. I've filed https://github.com/hashicorp/terraform/issues/15622 and tried to strip it down as much as I could to help with debugging. Appears to be related to variables and using target on first level modules. Anyways we can move the discussion over to that issue.

ghost commented 4 years ago

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.