The https://terraform.io/docs/configuration/interpolation.html docs note:
You can also perform simple math in interpolations, allowing you to write expressions such as ${count.index+1}.
I guess this works with count, but it surely doesn't work with ${var.name-1} or multiplication/division.. so I think this doc is misleading in what is possible.
Here is a simple use case: I have a terraform module used in an elastic environment, and each run of the environment may have different storage requirements, so I have variables for the size of docker and data volumes. These variables are first provided to AWS in creating EBS volumes, but they are then seeded into YAML (via Terraform's rendered templates) which configures the fdisk and lvm provisioning done by Saltstack on cloud init.. but because of the nature of LVM, I need to tell those LVM volumes and filesystem to be slightly smaller than the full EBS volume. If I could multiply or subtract a decimal/float, I could easily use one variable in both AWS EBS and the LVM formula and handle the small bit of math needed to properly instruct LVM what to do.
Maybe there is another or better way?
+1 :smile:
Hi @ketzacoatl,
You're right that the docs around arithmetic support are a little sparse. We currently support:
Does this address your need or is there something else we're missing here?
If that is the case @phinze, I would recommend we update the docs to include examples and make it more clear how to apply these capabilities. I'm willing to do the leg work, but I'd need some help in understanding how to do this correctly.
I started with something simple.. I have a variable: ${var.name}, and I want to subtract 1. How do I do this?
I started with something simple.. I have a variable: ${var.name}, and I want to subtract 1. How do I do this?
Sure! Config:
variable "name" {
default = "123"
}
output "minusone" {
value = "${var.name - 1}"
}
Output:
โฏ terraform apply
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
minusone = 122
@ketzacoatl I was having the same problem as you and as @phinze showed, I think you just need spaces between the operator. It seems because Terraform allows hyphens in resource and variables names, it's interpreting ${var.name-1} as a variable named name-1. Try changing it to ${var.name - 1} and I think you'll be good to go. I've just tested this and it works for me now. :100:
I'll put in a change to the docs to make this clearer.
Thanks @phinze!
Yes, that all makes sense, thank you for the example @phinze.
@clstokes, if you update the docs, may I request we note @phinze's:
as basic operations which are available to users?
@clstokes, were you able to knock this out, or should I take a pass on it this weekend?
@phinze can you confirm my hunch about the hyphens in resource and variables names is correct (https://github.com/hashicorp/terraform/issues/2782#issuecomment-125391728)? I can update the docs, but don't want to put invalid info in there.
@clstokes yep - that's precisely correct! for that reason i'd expect ${var.name+1} to work without spaces, but probably easiest to just document examples using spaces for all operators for symmetry
@ketzacoatl documentation updated (pending PR) in #2933. Look detailed enough for you?
Looks _great_ @clstokes! A+
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.
Most helpful comment
If that is the case @phinze, I would recommend we update the docs to include examples and make it more clear how to apply these capabilities. I'm willing to do the leg work, but I'd need some help in understanding how to do this correctly.
I started with something simple.. I have a variable:
${var.name}, and I want to subtract1. How do I do this?