Merge pull request #2933 from hashicorp/docs-math-interpolation

More details about math in interpolation
This commit is contained in:
Clint 2015-08-12 13:49:58 -05:00
commit d74ea5d472
2 changed files with 35 additions and 4 deletions

View File

@ -201,3 +201,34 @@ resource "aws_instance" "web" {
With this, we will build a list of `template_file.web_init` resources which we can
use in combination with our list of `aws_instance.web` resources.
## Math
Simple math can be performed in interpolations:
```
variable "count" {
default = 2
}
resource "aws_instance" "web" {
// ...
count = "${var.count}"
// tag the instance with a counter starting at 1, ie. web-001
tags {
Name = "${format("web-%03d", count.index + 1)}"
}
}
```
The supported operations are:
- *Add*, *Subtract*, *Multiply*, and *Divide* for **float** types
- *Add*, *Subtract*, *Multiply*, *Divide*, and *Modulo* for **integer** types
-> **Note:** Since Terraform allows hyphens in resource and variable names,
it's best to use spaces between math operators to prevent confusion or unexpected
behavior. For example, `${var.instance-count - 1}` will subtract **1** from the
`instance-count` variable value, while `${var.instance-count-1}` will interpolate
the `instance-count-1` variable value.