From 9a0c733c181859e91094596138796f44397ecc28 Mon Sep 17 00:00:00 2001 From: ketzacoatl Date: Fri, 29 May 2015 13:19:52 -0400 Subject: [PATCH] Update interpolation.md, doc pattern combining template_file and count This example is derived from @phinze's [work seen here](https://github.com/hashicorp/terraform/issues/1893#issuecomment-100945306) --- .../docs/configuration/interpolation.html.md | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/website/source/docs/configuration/interpolation.html.md b/website/source/docs/configuration/interpolation.html.md index b7b8c9d65e..0dbff526b0 100644 --- a/website/source/docs/configuration/interpolation.html.md +++ b/website/source/docs/configuration/interpolation.html.md @@ -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.