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

Variable should be type string, got number #7962

Closed
joshuaspence opened this issue Aug 3, 2016 · 26 comments · Fixed by #9263
Closed

Variable should be type string, got number #7962

joshuaspence opened this issue Aug 3, 2016 · 26 comments · Fixed by #9263

Comments

@joshuaspence
Copy link
Contributor

joshuaspence commented Aug 3, 2016

In Terraform 0.7 I have observed an issue with handling numeric variables. To replicate this issue, you need to create two files, main.tf:

variable "some_number" {
  type    = "string"
  default = 0
}

And terraform.tfvars.json:

{
  "some_number": 1
}

Running terraform apply produces the following error:

Errors:

  * 1 error(s) occurred:

* variable some_number should be type string, got number
@denisbr
Copy link

denisbr commented Aug 4, 2016

I'm facing this one too, it also happens when supplying the value as a cli parameter:

-var node_count=1
-var node_count="1"
-var node_count='1'

In my vars.tf:

variable "node_count" {
    description = "Number of nodes/instances to provision"
    type = "string"
    default = "1"
}

Also tried without type = "string".

All the above variations give me the same error:

Errors:

  * 1 error(s) occurred:

* variable node_count should be type string, got number

@SoMuchToGrok
Copy link
Contributor

SoMuchToGrok commented Aug 4, 2016

@denisbr Seeing the same thing when supplying var on the cli. I've tried nearly every combination imaginable and haven't discovered a temporary workaround. I've reverted back v.0.6.16.

There must be a bug in whatever "magic" is performing the type validation. For my use case, I'm specifying the short version of a git sha as a terraform variable:

GIT_SHA=$(git rev-parse --short HEAD)

The result of this can contain all numbers, all letters, or a combination of letters and numbers (and also may start with either a letter or a number).

Before updating to 0.7, I did not explicitly define the variable type and did not run into any issues.

When a variable's type is explicitly defined, perhaps there should be an attempt to cast that variable to a string (numbers -> string of numbers) prior to validation?

EDIT - Pull request 7977 is open for this.

@phinze
Copy link
Contributor

phinze commented Aug 4, 2016

Hey folks - sorry for the trouble here! You can work around this by wrapping double quotes around the flag, but you have to make sure your shell passes them through to Terraform.

So, to use @denisbr's example, this should work:

terraform plan -var 'node_count="1"'

We'll be landing #7977 shortly and the fix will be out in v0.7.1 soon!

@denisbr
Copy link

denisbr commented Aug 5, 2016

@phinze Thanks for giving a workaround suggestion, I'll wait for the fix to land in 0.7.1 and stay on 0.6.x for a little longer.

@zihaoyu
Copy link

zihaoyu commented Aug 19, 2016

The same error happens with boolean vars too. The same workaround works.

Errors:

  * 1 error(s) occurred:

* variable associate_public_ip_address should be type string, got boolean

@fluzz
Copy link

fluzz commented Aug 23, 2016

Was this not supposed to be resolved in 0.7.1? as im still seeing it wen running the below code

variable "instance_ebssize" {
    type = "string"
    default = "20"
}
Errors:

  * 1 error(s) occurred:

* variable instance_ebssize should be type string, got number

@SoMuchToGrok
Copy link
Contributor

SoMuchToGrok commented Aug 23, 2016

@fluzz seeing the same thing on 0.7.1. No reference to this issue in the changelog. The workaround mentioned above won't solve my specific use-case (passing bash vars to TF vars). Ultimately I switched over to loading the variables from a file (which is dynamically generated from bash prior to terraform execution).

@KyronSr
Copy link

KyronSr commented Aug 25, 2016

Seeing the same thing on 0.7.2 as well. Can't seem to get the workaround to work on 0.7.2 either.

@denisbr
Copy link

denisbr commented Aug 26, 2016

@phinze Care to enlighten us on this issue? It's still keeping me from going to 0.7.x.

@brikis98
Copy link
Contributor

@phinze #7977 did not fix the issue. I just tested the following with Terraform 0.7.2:

variable "foo" {}

Run the following at the command line:

terraform apply -var 'foo = 5'

Get error:

* variable foo should be type string, got number

Same happens for boolean too.

@kateqchen
Copy link

I am seeing the same thing with 0.7.2

@akoeb-zalando
Copy link

Seeing the same behavior in 0.7.3

@sjourdan
Copy link

sjourdan commented Sep 10, 2016

@akoeb-zalando the workaround still works though:

terraform plan -var 'node_count="1"'

@zeplynne
Copy link

Seeing this too in 0.7.2 and 0.7.3.

@phinze workaround works well:

terraform plan -var 'deploy_account="1234567890"'

@baboune
Copy link
Contributor

baboune commented Sep 16, 2016

Seeing this on 0.7.3 as well

@denisbr
Copy link

denisbr commented Sep 16, 2016

Don't either @phinze or anyone at hashicorp care about this at all? Not a quick word on this? @mitchellh ?

@mitchellh
Copy link
Contributor

@denisbr We do! Because this bug has a workaround, we prioritize bugs that don't have workarounds first since they're obvious blockers.

But this bug is tagged and we handle core bugs as quickly as possible. If you want to see it done quicker I encourage a pull request according to our contribution guide which is detailed. If that is followed (helpful checkboxes guide you the way) then we can merge very, very quickly.

I promise we'll get to it. :)

@SoMuchToGrok
Copy link
Contributor

Just as a reminder to all, the recommended workaround doesn't help when you're passing through bash variables. For this use case, you'll need to create a variable file and load them that way.

@denisbr
Copy link

denisbr commented Sep 16, 2016

Ok, thanks. Just a very long thread without any word from hashicorp. I'm
relieved now.
On Fri, 16 Sep 2016 at 16:50, Mitchell Hashimoto [email protected]
wrote:

@denisbr https://github.com/denisbr We do! Because this bug has a
workaround, we prioritize bugs that don't have workarounds first since
they're obvious blockers.

But this bug is tagged and we handle core bugs as quickly as possible. If
you want to see it done quicker I encourage a pull request according to our contribution
guide
https://github.com/hashicorp/terraform/blob/master/.github/CONTRIBUTING.md
which is detailed. If that is followed (helpful checkboxes guide you the
way) then we can merge very, very quickly.

I promise we'll get to it. :)


You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#7962 (comment),
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAdORvlE4YoY97PHDY3UF3j8DwtNPx2Mks5qqqy7gaJpZM4JcNbA
.

Denis Brækhus Senior Engineer | Varnish Software AS
Cell: +47 48047724
We Make Websites Fly!
www.varnish-software.com

@baboune
Copy link
Contributor

baboune commented Sep 16, 2016

New to terraform here.
How would I do this workaround with a file on ubuntu (using bash)?
The doc does not describe how to use "number".

@thehydroimpulse
Copy link

Also hitting this bug after upgrading to 0.7 but we're using var-file and so the workaround doesn't work for us.

@baboune
Copy link
Contributor

baboune commented Sep 22, 2016

I found a work around by using quotes when assigning the values in the var file.

@dayer4b
Copy link
Contributor

dayer4b commented Sep 26, 2016

@baboune can you elaborate on your workaround?

@baboune
Copy link
Contributor

baboune commented Sep 26, 2016

@dayer4b In my case, the trick was in the .tfvars file by adding " " around integer values. By treating everything as String, then the parser can convert it to number, and it works.

Example:
In main.tf:

module "mymodule" {
  auditlog-on-primary-count                = "${var.auditlog-on-primary-
}

Then in variable.tf:
variable "auditlog-on-primary-count" {}

Finally in terraform.tfvars
auditlog-on-primary-count = "1"

@dayer4b
Copy link
Contributor

dayer4b commented Sep 26, 2016

ahh, I see. thanks!

@ghost
Copy link

ghost commented Apr 21, 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 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.