terraform init -var "foo=bar" not supported

Created on 26 Sep 2017  ยท  4Comments  ยท  Source: hashicorp/terraform

Hello,

I'd like to add dynamic variable into my terraform config that should be passed as -var 'git_branch=whatever' to terraform apply but terraform init fails because this variable is not defined with the following error:
required variable "git_branch" not set

This turns out to be a serious lack in configuration. I can bypass this issue defining the default value for git_branch variable but this is side walk solution.

Terraform Version

Terraform v0.10.6

Terraform Configuration Files

```variable "git_branch" {
description = "The name of the branch that executed the pipline"
}
resource "aws_instance" "ec2" {
count = "1"
ami = "ami-123456"
instance_type = "t2.nano"
vpc_security_group_ids = ["sg-123456"]
key_name = "ssh_key_name"
iam_instance_profile = "ec2_iam"

tags {
    git_branch          = "${var.git_branch}"
}
volume_tags {
    git_branch          = "${var.git_branch}"
}

}
```

Crash Output

Error getting plugins: module root: module ec2: required variable "git_branch" not set

Expected Behavior

terraform init should complete successfully

Actual Behavior

terraform init failed because `-var "foor=bar" is not supported

Steps to Reproduce

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

  1. Update values of ami and security groups in my example
  2. terraform init -var 'git_branch=master'

Important Factoids

Terraform is being executed in gitlab IC pipeline so git_branch = $CI_COMMIT_REF_NAME

cli question

Most helpful comment

Thanks for the suggestions. Not I need to remmember where this issue appeared and test if it is going to work with terraform init.

I know that variables mast b set but I'd like to specify it as argument to terraform apply. This doesn't work for terraform init because there is no cli argument -var for init operation and it fails.

I defined the git_branch variable default = "" to bypass this error but it doesn't see to be the elegant solution.

All 4 comments

I think you need
variable "git_branch" { type = "string" }

in your variables.tf

Hi @yyarmoshyk!

The variable mentioned in the error message here is a variable for your child module ec2 rather than for the root. The message is therefore talking about the module block in the root module:

module "ec2" {
  source = "..."

  # this must be set
  git_branch = "master"
}

If you want to set this variable on the terraform plan command line, it must be declared in the root module and then passed through to the child module, like this:

variable "git_branch" {
}

module "ec2" {
  source = "..."

  git_branch = "${var.git_branch}"
}

The root module variables don't need to be set for terraform init; it should work as long as all of the child modules and other objects within the configuration have correct arguments.

Thanks for the suggestions. Not I need to remmember where this issue appeared and test if it is going to work with terraform init.

I know that variables mast b set but I'd like to specify it as argument to terraform apply. This doesn't work for terraform init because there is no cli argument -var for init operation and it fails.

I defined the git_branch variable default = "" to bypass this error but it doesn't see to be the elegant solution.

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