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.

Home Page:https://www.terraform.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot parse value for variable for CIDR format

tmtk75 opened this issue · comments

After upgrading to v0.7.0, I can see next error.

$ terraform -v
Terraform v0.7.0
...
$ terraform plan -var cidr=127.0.0.1/32
invalid value "cidr=127.0.0.1/32" for flag -var: Cannot parse value for variable cidr ("cidr=127.0.0.1/32") as valid HCL: At 1:11: expected: IDENT | STRING | ASSIGN | LBRACE got: FLOAT
Usage: terraform plan [options] [dir]

  Generates an execution plan for Terraform.

  This execution plan can be reviewed prior to running apply to get a
  sense for what Terraform will do. Optionally, the plan can be saved to
  a Terraform plan file, and apply can take this plan file to execute
  this plan exactly.

Options:

  -destroy            If set, a plan will be generated to destroy all resources
...

I could not see the error with v0.6.16.

$ /usr/local/bin/terraform -v
Terraform v0.6.16

Your version of Terraform is out of date! The latest version
is 0.7.0. You can update by downloading from www.terraform.io
...
$ /usr/local/bin/terraform plan -var cidr=127.0.0.1/32
Refreshing Terraform state prior to plan...

No changes. Infrastructure is up-to-date. This means that Terraform
could not detect any differences between your configuration and
the real physical resources that exist. As a result, Terraform
doesn't need to do anything.

Affected Resource(s)

variable, so it may be a terraform's core issue.

Terraform Configuration Files

I can reproduce with this one.

variable cidr {}

Expected Behavior

It works as 0.6.16.

Actual Behavior

I got next error.

invalid value "cidr=127.0.0.1/32" for flag -var: Cannot parse value for variable cidr ("cidr=127.0.0.1/32") as valid HCL: At 1:11: expected: IDENT | STRING | ASSIGN | LBRACE got: FLOAT

Hi @tmtk75. Sorry for the problem here.

It looks like in Terraform 0.7 the expected syntax here was changed to support passing maps and lists as variables, in addition to strings.

I'm not 100% sure (I didn't hit this issue myself yet, since I tend to use environment variables to set Terraform variables) but I think you could get around this by making the -var argument a valid HCL declaration, like this:

terraform plan -var 'cidr="127.0.0.1/32"'

An alternative, that might be easier to read and less confusing, would be to create a separate variables file using HCL syntax and then use -var-file to reference it.

Hi @apparentlymart, thanks a lot to let me know the workaround!
It's pretty helpful to me!

Regarding my use-case, I'd like to dynamically give my current IP address into cidr_blocks of aws_security_group for a tentative instance.

I could fix my code like this and saw it worked as expected. Thank you so much!

terraform plan -var 'cidr="'`curl -s echoip.net`/32'"'
variable cidr {}

resource "aws_security_group" "test" {
...
  ingress {
      cidr_blocks = [ "${var.cidr}" ]

@tmtk75 you gave me an idea for interesting data source :)

Closing since we got this figured out.

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.