Skip to content

Commit 1c5286c

Browse files
committed
Add how-to
1 parent 585b5c3 commit 1c5286c

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

dynamic-template-output/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,50 @@ Instead, you can use a [string template directive](https://www.terraform.io/docs
2828

2929
To see this in action, review [main.tf](./main.tf), and see how it passes the `ENV_VARS = {}` map into the function. The `for` directive inside of [user-data.sh.tmpl](./user-data.sh.tmpl) unwraps that map, renders each line into an `export`, and the resulting script sets all of variables in the shell.
3030

31+
## Using This Example
32+
33+
To start, init and apply this terraform code:
34+
35+
```shell
36+
terraform init; terraform apply -auto-approve
37+
```
38+
39+
Then observe the output:
40+
41+
```
42+
23:22 $ terraform apply -auto-approve
43+
44+
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
45+
46+
Outputs:
47+
48+
rendered = #!/bin/bash
49+
50+
export var1="test1"
51+
export var2="test2"
52+
53+
# Do stuff
54+
echo "The rendered version of this template will declare all of the keys in ENV_VARS as variables."
55+
```
56+
57+
Now, in [main.tf](./main.tf), you can add more inputs to the ENV_VARS map and re-apply to see this in action:
58+
```
59+
23:22 $ terraform apply -auto-approve
60+
61+
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
62+
63+
Outputs:
64+
65+
rendered = #!/bin/bash
66+
67+
export var1="test1"
68+
export var2="test2"
69+
export var_new="This var was added to the map in the function input, but I did not modify the template file"
70+
71+
# Do stuff
72+
echo "The rendered version of this template will declare all of the keys in ENV_VARS as variables."
73+
```
74+
3175
### Discussion
3276

3377
Relates back to [this reddit thread](https://www.reddit.com/r/Terraform/comments/jy05en/terraform_variables_in_bash_scripting/gd0dfhv/)

0 commit comments

Comments
 (0)