@@ -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+
2651resource "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}
2956resource "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}
3261resource "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}
3566resource "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}
3871resource "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+ }
0 commit comments