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

Units of measure in numbers don't work as documented. #3287

Closed
arianvp opened this issue Sep 19, 2015 · 10 comments
Closed

Units of measure in numbers don't work as documented. #3287

arianvp opened this issue Sep 19, 2015 · 10 comments

Comments

@arianvp
Copy link

arianvp commented Sep 19, 2015

Version of terraform: v0.6.3

The documentation says that numbers should support units of measure. Meaning 1gb should be a valid number. Except currently that doesn't seem to parse.

  • Numbers are assumed to be base 10. If you prefix a number with 0x, it is treated as a hexadecimal number.
  • Numbers can be suffixed with kKmMgG for some multiple of 10. For example: 1k is equal to 1000.
  • Numbers can be suffixed with [kKmMgG]b for power of 2 multiples, example: 1kb is equal to 1024.

Say we have:

variable "size" {
    default = 10gb
}

output "size_in_bytes" {
    value = "${var.size}"
}

it should work but instead we get:

Error loading config: Error parsing example.tf: Line 4, column 1: syntax error

I also tried default = "10gb" (enclosed in double quotes) but then we get:

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  size_in_bytes = 10gb

i.e. the size is parsed as string.

@arianvp
Copy link
Author

arianvp commented Sep 19, 2015

If you guys can give me some pointers where to look, I can give a shot at trying to tackle this.

@jjshoe
Copy link

jjshoe commented Sep 21, 2015

https://terraform.io/docs/providers/aws/r/ebs_volume.html - "size - (Optional) The size of the drive in GB."

@arianvp
Copy link
Author

arianvp commented Sep 21, 2015

I know. This was just a random example of a "valid" configuration file. The bug is not with the amazon provider, but with the fact that the documentation says units of measure are supported in number literals while they aren't

What I'm trying to say is that the documenation states that in the syntax of configuration files (agnostic of provisioner or provider) integers are defined according to the following EBNF grammar:

<int_literal> =  (0x<hex_literal> | <dec_literal>)  [kKmMgG] [b]

which means the following .tf file should parse:

variable "blah" { default = 10gb }

But instead we get

Error loading config: Error parsing /home/arian/Projects/golang/src/github.com/hex-sh/terraform-provider-scaleway/test.tf: Line 4, column 1: syntax error

So the current implementation seems to be

<int_literal> =  (0x<hex_literal> | <dec_literal>)

Which is clearly a mismatch between documentation and implementation.

So we have two ways to fix this. Update the documentation that units of measure aren't supported. Or add the feature to the configuration file parser.

@radeksimko
Copy link
Member

Per discussion on IRC, I modified your original example, so it's more obvious that this problem is provider/resource unrelated, yet still valid.

@radeksimko
Copy link
Member

We also discussed on IRC, that it would be useful to have a way to convert bytes to other units, so that one variable can be used for multiple resources, each expecting different units.

variable "size" {
  default = "1TB"
}

resource "aws_ebs_volume" "example" {
  size = "${in_gb(var.size)}"
}

https://botbot.me/freenode/terraform-tool/2015-09-21/?msg=50178901&page=1

@arianvp that's actually pretty useful I think. For example scaleway defines their volumes in bytes. But you could then use the same variable for defining volumes sizes on their cloud and amazon's cloud.

@maxbeatty
Copy link

I just came up against this when trying to create an elasticsearch domain on AWS and setting the volume_size for ebs

resource "aws_elasticsearch_domain" "example" {
  domain_name = "example"

  ebs_options {
    ebs_enabled = true
    volume_type = "gp2" # General Purpose
    volume_size = 10Gb
  }

  snapshot_options {
    automated_snapshot_start_hour = 23
  }
}
$ terraform plan
Error loading config: Error parsing /path/to/es.tf: Line 12, column 3: syntax error

It would be great if "10Gb" expanded to the proper integer for the AWS API call

@dmacvicar
Copy link

With Terraform 0.6.12 I get no syntax error, but it looks like the suffixes do not have anny effect

variable "size" {
    default = 10kb
}

output "size_in_bytes" {
    value = "${var.size}"
}
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

  size_in_bytes = 10

dmacvicar added a commit to dmacvicar/terraform-provider-libvirt that referenced this issue Mar 12, 2016
@dennybaa
Copy link

dennybaa commented Aug 5, 2016

+1

@mitchellh
Copy link
Contributor

@dmacvicar Verified that that will give a syntax error now.

So this is a huge misdocumentation. We had this feature as part of Terraform 0.1 and 0.2 but it has probably been broken since then since when we switched to HCL as the format, this didn't come along with it. We've chatted about it and decided to not support this feature (which has never really worked in any meaningful moment of history).

Closing this and will update docs. Sorry!

@ghost
Copy link

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

No branches or pull requests

7 participants