From 30d57bf1ecbc4f5f0db3395835ef5933db1de7d6 Mon Sep 17 00:00:00 2001 From: Cameron Stokes Date: Tue, 4 Aug 2015 11:09:43 -0700 Subject: [PATCH 1/2] Clarify math operations available for interpolation As reported in #2782, the math operations, specifically subtraction, can cause unexpected behavior when resource or variable names use hyphens. I added clarification about using spaces with math operators as well as which operations are available. --- .../docs/configuration/interpolation.html.md | 35 +++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index 74b946cf16..0d15cc9dd6 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -17,7 +17,7 @@ The interpolation syntax is powerful and allows you to reference variables, attributes of resources, call functions, etc. You can also perform simple math in interpolations, allowing -you to write expressions such as `${count.index+1}`. +you to write expressions such as `${count.index + 1}`. You can escape interpolation with double dollar signs: `$${foo}` will be rendered as a literal `${foo}`. @@ -92,7 +92,7 @@ The supported built-in functions are: format. The syntax for the format is standard `sprintf` syntax. Good documentation for the syntax can be [found here](http://golang.org/pkg/fmt/). Example to zero-prefix a count, used commonly for naming servers: - `format("web-%03d", count.index+1)`. + `format("web-%03d", count.index + 1)`. * `formatlist(format, args...)` - Formats each element of a list according to the given format, similarly to `format`, and returns a list. @@ -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. From ca252ccee98eb3a670d31433394d0242ffc9bed0 Mon Sep 17 00:00:00 2001 From: Cameron Stokes Date: Tue, 4 Aug 2015 11:12:24 -0700 Subject: [PATCH 2/2] Changed math example to use spaces between math operator --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3fc3d822d..ae87a5a3a6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,7 +17,7 @@ IMPROVEMENTS: * provider/aws: Allow configuration of the DynamoDB Endpoint [GH-2825] * provider/aws: Compute private ip addresses of ENIs if they are not specified [GH-2743] * provider/azure: Allow `settings_file` to accept XML string [GH-2922] - * provider/azure: Provide a simpler error when using a Platform Image without a + * provider/azure: Provide a simpler error when using a Platform Image without a Storage Service [GH-2861] * provider/google: `account_file` is now expected to be JSON. Paths are still supported for backwards compatibility. [GH-2839] @@ -557,7 +557,7 @@ FEATURES: less likely to become corrupt in a catastrophic case: terraform panic or system killing Terraform. * **Math operations** in interpolations. You can now do things like - `${count.index+1}`. [GH-1068] + `${count.index + 1}`. [GH-1068] * **New AWS SDK:** Move to `aws-sdk-go` (hashicorp/aws-sdk-go), a fork of the official `awslabs` repo. We forked for stability while `awslabs` refactored the library, and will move back to the officially