Skip to content

Commit e7f34a5

Browse files
committed
update terraform example
1 parent eef0191 commit e7f34a5

5 files changed

Lines changed: 140 additions & 12 deletions

File tree

docs/terraform.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,9 @@ terraform plan -out=plan.out
6161
terraform show -json plan.out
6262
terraform apply plan.out
6363
```
64+
65+
Mad Libs example
66+
```sh
67+
terraform init && terraform apply -auto-approve
68+
terraform destroy
69+
```

terraform/book/.terraform.lock.hcl

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

terraform/book/madlibs.tf

Lines changed: 73 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ terraform {
33
random = {
44
source = "hashicorp/random"
55
}
6+
local = {
7+
source = "hashicorp/local"
8+
}
9+
archive = {
10+
source = "hashicorp/archive"
11+
}
612
}
713
}
814

@@ -23,28 +29,83 @@ variable "words" {
2329
}
2430
}
2531

32+
variable "num_files" {
33+
default = 100
34+
type = number
35+
}
36+
37+
locals {
38+
uppercase_words = { for k, v in var.words : k => [for s in v : upper(s)] }
39+
}
40+
41+
# "count" is a meta argument, which means all resources intrinsically support it
42+
# the address of a managed resource uses the format <RESOURCE TYPE>.<NAME>
43+
# if "count" is set, the value of this expression becomes a list of objects
44+
# representing all possible resource instances
45+
# therefore, we could access the Nth instance in the list
46+
# with bracket notation: <RESOURCE TYPE>.<NAME>[N]
47+
48+
# the expression "[][0]" always throws an error if it's evaluated
49+
# (since it attempts to access the first element of an empty list)
50+
2651
resource "random_shuffle" "random_nouns" {
27-
input = var.words["nouns"]
52+
#input = var.words["nouns"]
53+
count = var.num_files
54+
input = local.uppercase_words["nouns"]
2855
}
2956
resource "random_shuffle" "random_adjectives" {
30-
input = var.words["adjectives"]
57+
#input = var.words["adjectives"]
58+
count = var.num_files
59+
input = local.uppercase_words["adjectives"]
3160
}
3261
resource "random_shuffle" "random_verbs" {
33-
input = var.words["verbs"]
62+
#input = var.words["verbs"]
63+
count = var.num_files
64+
input = local.uppercase_words["verbs"]
3465
}
3566
resource "random_shuffle" "random_adverbs" {
36-
input = var.words["adverbs"]
67+
#input = var.words["adverbs"]
68+
count = var.num_files
69+
input = local.uppercase_words["adverbs"]
3770
}
3871
resource "random_shuffle" "random_numbers" {
39-
input = var.words["numbers"]
72+
#input = var.words["numbers"]
73+
count = var.num_files
74+
input = local.uppercase_words["numbers"]
75+
}
76+
77+
# output "mad_libs" {
78+
# value = templatefile("${path.module}/templates/alice.txt", {
79+
# nouns = random_shuffle.random_nouns.result
80+
# adjectives = random_shuffle.random_adjectives.result
81+
# verbs = random_shuffle.random_verbs.result
82+
# adverbs = random_shuffle.random_adverbs.result
83+
# numbers = random_shuffle.random_numbers.result
84+
# })
85+
# }
86+
87+
locals {
88+
templates = tolist(fileset(path.module, "templates/*.txt"))
4089
}
4190

42-
output "mad_libs" {
43-
value = templatefile("${path.module}/templates/alice.txt", {
44-
nouns = random_shuffle.random_nouns.result
45-
adjectives = random_shuffle.random_adjectives.result
46-
verbs = random_shuffle.random_verbs.result
47-
adverbs = random_shuffle.random_adverbs.result
48-
numbers = random_shuffle.random_numbers.result
91+
resource "local_file" "mad_libs" {
92+
count = var.num_files
93+
filename = "madlibs/madlibs-${count.index}.txt"
94+
content = templatefile(element(local.templates, count.index),
95+
{
96+
nouns = random_shuffle.random_nouns[count.index].result
97+
adjectives = random_shuffle.random_adjectives[count.index].result
98+
verbs = random_shuffle.random_verbs[count.index].result
99+
adverbs = random_shuffle.random_adverbs[count.index].result
100+
numbers = random_shuffle.random_numbers[count.index].result
49101
})
50102
}
103+
104+
# terraform destroy will not delete madlibs.zip because this file isn’t a managed resource
105+
# "data sources" do not implement Delete()
106+
data "archive_file" "mad_libs" {
107+
depends_on = [local_file.mad_libs]
108+
type = "zip"
109+
source_dir = "${path.module}/madlibs"
110+
output_path = "${path.cwd}/madlibs.zip"
111+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
THE OBSERVATORY
2+
Out class when on a field trip to a ${adjectives[0]} observatory. It
3+
was located on top of a ${nouns[0]}, and it looked like a giant
4+
${nouns[1]} with a slit down its ${nouns[2]}. We went inside and
5+
looked through a ${nouns[3]} and were able to see ${nouns[4]}s in
6+
the sky that were millions of ${nouns[5]}s away. The men and
7+
women who ${verbs[0]} in the observatory are called
8+
${nouns[6]}s, and they are always watching for comets, eclipses,
9+
and shooting ${nouns[7]}s. An eclipse occurs when a ${nouns[8]}
10+
comes between the earth and the ${nouns[9]} and everything
11+
gets ${adjectives[1]}. Next week, we place to ${verbs[1]} the
12+
Museum of Modern ${nouns[10]}.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
HOW TO BE A PHOTOGRAPHER
2+
Many ${adjectives[0]} photographers make big money
3+
photographing ${nouns[0]}s and beautiful ${nouns[1]}s. They sell
4+
the prints to ${adjectives[1]} magazines or to agencies who use
5+
them in ${nouns[2]} advertisements. To be a photographer, you
6+
have to have a ${nouns[3]} camera. You also need an
7+
${adjectives[2]} meter and filters and a special close-up
8+
${nouns[4]}. Then you either hire professional ${nouns[1]}s or go
9+
out and snap candid pictures of ordinary ${nouns[5]}s. But if you
10+
want to have a career, you must study very ${adverbs[0]} for at
11+
least ${numbers[0]} years.

0 commit comments

Comments
 (0)