chapter three

3 Functional Programming and Advanced Templating Techniques

 

This chapter covers:

  • Functional programming with declarative functions and expressions
  • Templating patterns and techniques with local only resources
  • Parameterizing configuration files with input variables
  • Mix and matching multiple providers in a single project
  • Creating dynamic Terraform configurations with count

In this chapter, we’ll be building an engine for generating Mad Libs from template files. Mad Libs, in case you don’t know, is a phrasal templating word game in which one player prompts another for words to fill in the blanks of a story, before reading the story aloud. If you’ve ever played Apples to Apples or Cards against Humanity, it’s a lot like that except with a whole story rather than a single sentence or two. What makes these kinds of games fun is the juxtaposition that exists between an otherwise normal story, and the odd words chosen to fill in the blanks. An example input for a Mad Libs is shown below. Given this input, it should ask for a noun, an adjective, and another noun, then replace the blanks with these values.

To make a pizza, you need to take a lump of <noun>, and make a thin, round <adjective> <noun>.

3.1       First Attempt at Mad Libs

3.1.1   Making the Word Pool

3.1.2   Shuffling the Words

3.1.3   Using Template Data Sources

3.1.4   Outputting to a Local File

3.2       Improving Mad Libs with Expressions

3.2.1   Getting Fancy with For Expressions

3.2.2   Implicit Dependencies

3.2.3   Scaling Resources by Incrementing Count

3.2.4   A Better Way to Template

3.2.5   Compressing Files with an Archive Resource 

3.2.6   Applying Changes

3.3       Overview

3.4       Summary