Merge pull request #2140 from ketzacoatl/patch-3

Update interpolation.md, doc pattern combining template_file and count
This commit is contained in:
Paul Hinze 2015-05-29 13:52:04 -05:00
commit 6749ea29a4
1 changed files with 39 additions and 0 deletions

View File

@ -160,3 +160,42 @@ ${hello} ${world}!
Then the rendered value would be `goodnight moon!`.
You may use any of the built-in functions in your template.
### Using Templates with Count
Here is an example that combines the capabilities of templates with the interpolation
from `count` to give us a parametized template, unique to each resource instance:
```
variable "count" {
default = 2
}
variable "hostnames" {
default = {
"0" = "example1.org"
"1" = "example2.net"
}
}
resource "template_file" "web_init" {
// here we expand multiple template_files - the same number as we have instances
count = "${var.count}"
filename = "templates/web_init.tpl"
vars {
// that gives us access to use count.index to do the lookup
hostname = "${lookup(var.hostnames, count.index)}"
}
}
resource "aws_instance" "web" {
// ...
count = "${var.count}"
// here we link each web instance to the proper template_file
user_data = "${element(template_file.web_init.*.rendered, count.index)}"
}
```
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.