Skip to content

Commit eef0191

Browse files
committed
add terraform book example
1 parent 98effc2 commit eef0191

4 files changed

Lines changed: 87 additions & 1 deletion

File tree

docs/terraform.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ Terraform's core building blocks:
3030
Docker example
3131
```sh
3232
# formatting/linting
33-
terraform fmt -check
33+
terraform fmt
34+
terraform fmt -check
3435

3536
# initializing a configuration directory downloads and installs the providers defined in the configuration
3637
cd terraform/docker-example

terraform/book/.terraform.lock.hcl

Lines changed: 21 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: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
terraform {
2+
required_providers {
3+
random = {
4+
source = "hashicorp/random"
5+
}
6+
}
7+
}
8+
9+
# type coercion is the ability to convert any primitive type to its string representation
10+
variable "words" {
11+
description = "A word pool to use for Mad Libs"
12+
type = object({
13+
nouns = list(string),
14+
adjectives = list(string),
15+
verbs = list(string),
16+
adverbs = list(string),
17+
numbers = list(number),
18+
})
19+
20+
validation {
21+
condition = length(var.words["nouns"]) >= 10
22+
error_message = "At least 10 nouns must be supplied."
23+
}
24+
}
25+
26+
resource "random_shuffle" "random_nouns" {
27+
input = var.words["nouns"]
28+
}
29+
resource "random_shuffle" "random_adjectives" {
30+
input = var.words["adjectives"]
31+
}
32+
resource "random_shuffle" "random_verbs" {
33+
input = var.words["verbs"]
34+
}
35+
resource "random_shuffle" "random_adverbs" {
36+
input = var.words["adverbs"]
37+
}
38+
resource "random_shuffle" "random_numbers" {
39+
input = var.words["numbers"]
40+
}
41+
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
49+
})
50+
}

terraform/book/templates/alice.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
ALICE'S UPSIDE-DOWN WORLD
2+
Lewis Carroll's classic, "Alice's Adventures in Wonderland", as well
3+
as its ${adjectives[0]} sequel, "Through the Looking ${nouns[0]}",
4+
have enchanted both the young and old ${nouns[1]}s for the last
5+
${numbers[0]} years, Alice's ${adjectives[1]} adventures begin
6+
when she ${verbs[0]}s down a/an ${adjectives[2]} hole and lands
7+
in a strange and topsy-turvy ${nouns[2]}. There she discovers she
8+
can become a tall ${nouns[3]} or a small ${nouns[4]} simply by
9+
nibbling on alternate sides of a magic ${nouns[5]}. In her travels
10+
through Wonderland, Alice ${verbs[1]}s such remarkable
11+
characters as the White ${nouns[6]}, the ${adjectives[3]} Hatter,
12+
the Cheshire ${nouns[7]}, and even the Queen of ${nouns[8]}s.
13+
Unfortunately, Alice's adventures come to a/an ${adjectives[4]}
14+
end when Alice awakens from her ${nouns[8]}.

0 commit comments

Comments
 (0)