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
43.15k stars 9.58k forks source link

Remote terraforom plan reports missing input vars #23115

Closed imranismail closed 5 years ago

imranismail commented 5 years ago

Hi,

I'm trying to run terraform plan on a remote backend (terraform cloud) with 0.12.11 however it always says variable not set. I suspect this is related to https://github.com/hashicorp/terraform/issues/21659

Terraform Version

Terraform v0.12.11
+ provider.aws v2.33.0

Terraform Configuration Files

terraform {
  backend "remote" {
    organization = "fave"

    workspaces {
      prefix = "data-pipeline_staging_main_v1_"
    }
  }
}

provider "aws" {
  version = "~> 2.0"

  region     = var.aws_region
  access_key = var.aws_access_key
  secret_key = var.aws_secret_key

  assume_role {
    role_arn = var.aws_role_arn
  }
}

resource "aws_s3_bucket" "target" {
  bucket_prefix = "my-bucket"
  force_destroy = true
}

Debug Output

Crash Output

Error: No value for required variable

  on variables.tf line 1:
   1: variable "spotinst_token" {

The root module input variable "spotinst_token" is not set, and has no default
value. Use a -var or -var-file command line argument to provide a value for
this variable.

Error: No value for required variable

  on variables.tf line 5:
   5: variable "spotinst_account" {

The root module input variable "spotinst_account" is not set, and has no
default value. Use a -var or -var-file command line argument to provide a
value for this variable.

Error: No value for required variable

  on variables.tf line 9:
   9: variable "aws_access_key" {

The root module input variable "aws_access_key" is not set, and has no default
value. Use a -var or -var-file command line argument to provide a value for
this variable.

Error: No value for required variable

  on variables.tf line 13:
  13: variable "aws_secret_key" {

The root module input variable "aws_secret_key" is not set, and has no default
value. Use a -var or -var-file command line argument to provide a value for
this variable.

Error: No value for required variable

  on variables.tf line 17:
  17: variable "aws_region" {

The root module input variable "aws_region" is not set, and has no default
value. Use a -var or -var-file command line argument to provide a value for
this variable.

Error: No value for required variable

  on variables.tf line 21:
  21: variable "aws_role_arn" {

The root module input variable "aws_role_arn" is not set, and has no default
value. Use a -var or -var-file command line argument to provide a value for
this variable.

Error: No value for required variable

  on variables.tf line 25:
  25: variable "admin_role_arn" {

The root module input variable "admin_role_arn" is not set, and has no default
value. Use a -var or -var-file command line argument to provide a value for
this variable.

Error: No value for required variable

  on variables.tf line 29:
  29: variable "accepter_vpc_id" {

The root module input variable "accepter_vpc_id" is not set, and has no
default value. Use a -var or -var-file command line argument to provide a
value for this variable.

Expected Behavior

Remote terraform plan should be run in the cloud where all variables are already set

Actual Behavior

Terraform plan reports missing variables

Steps to Reproduce

  1. terraform init with backend set to terraform cloud
  2. terraform plan

References

lae commented 5 years ago

I'm running into this same issue and earlier opened a ticket with Hashicorp support about it.

This doesn't just happen on a CLI-initialized terraform plan but also on a VCS-triggered plan. I'm not sure, but I think Terraform does generate a valid plan, then does something else that breaks? For me, it showed what changes would happen, correct variables and all, then errors out saying it couldn't find the input variables.

Edit: I just tried 0.12.10 and it works as-expected. So regression, I guess.

WillBeebe commented 5 years ago

We ran into this too, but there's a cure! You have to set vars for every variable you use. It's a bit annoying that in what's technically a patch release there was a breaking change, but oh well. From an interface perspective this makes a lot of sense. If like us, you set variables in the backend that aren't declared in the repo, you just have to give them defaults.

variable "some_var" {
  default = "sane default"
}

# for vars that don't have sane defaults
variable "who_killed_the_muffin_man" {
  default = null
}
pselle commented 5 years ago

Thanks for the reports! We're looking into this, and have a suspected source (https://github.com/hashicorp/terraform/pull/23040) but will update as we progress on our investigation. Thank you for noting a workaround, @WillBeebe!

apparentlymart commented 5 years ago

Hi all! Thanks again for reporting this and sharing workarounds.

The fix for this regression is in the v0.12.12 release, which is now available for selection on Terraform Cloud.

Because this bug is an interaction between Terraform CLI and Terraform Cloud, it's unfortunately important to upgrade both your local CLI executable and the version selected on the remote workspace. v0.12.11 is not functional either as a remote operations client or within the Terraform Cloud remote execution environment.

ghost commented 5 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.