Day 0 Beginners Guide to Terraform

Motivation

The most frustrating aspect of Terraform is the “Day One” learning curve. When first starting out most of the examples I found (by which I mean: every single one), seemed to assume that you understand the basic operation of Terraform and focused on the more complex use-cases. The problem with that is that I held certain misconceptions that none of those guides actually addressed. The most significant one was “How do I download the package (provider) to interact with a specific service?”. The answer is “You don’t”, but all of the guides I found seemed to assume that knowledge and didn’t address the topic. In hindsight I understand why, but it’s been over two years and I still remember the intense frustration of that first day working with Terraform (TF).

This guide is intended to answer those very basic beginner questions, as well as walk you through the creation of your first (extremely) simple Terraform Run so you can see how the various pieces for together.

Organization

This guide is broken into several small parts intended to let you skip the frustration of discovering them yourself. It definitely does not replace the Terraform Documentation, but as you’ll discover if you click the link, this guide will reduce the time you need to “internalize” how to use Terraform.

Sections 12-15 contains simple examples to illustrate how several Terraform concepts fit together. The code for each example is available not only on the example page, but in the corresponding …/example_*/ directory.

Terrform

As stated, this guide is a simple introduction to Terraform, and we will be going over several of the basic elements of a basic Terraform deployment. Those elements are:

  1. tl;dr Quick Start

  2. Providers

  3. Registry

  4. Configurations

  5. Resources

  6. Modules

  7. Runs

  8. Variables

  9. Initialization

  10. Execution: Plan, Apply, Destroy

  11. Tips and Tricks

  12. Example 1
    • Variables, local values, null_resource resource, and outputs

  13. Example 2
    • Variables, local values with embedded values, outputs

  14. Example 3
    • Module creation and usage, module outputs

  15. Example 4
    • Module usage with ternary conditional and ‘count’ meta-argument

tl;dr Quick Start

  1. Write your Terraform code

    • I describe this below; however, if you are actually starting with this know that I have experienced your pain and have no sympathy. Helping you avoid that pain is the whole purpose of this guide.

  2. terraform init

    • Initialize your TF environment, which includes a basic syntax check.

    • NOTE: This will download the required providers and enumerate your modules.

  3. terraform plan

    • More thorough syntax check

    • Determine order of operations based on dependencies

    • Logic check - checks for unmet dependencies, circular logic, missing variables, etc…

  4. terraform apply [--auto-approve]

    • Deploy the configuration.

    • Communication/authentication/authorization issues will be caught at this stage.

    • Failures will not be rolled back automatically.

  5. terraform destroy [--auto-approve]

    • Destroy/delete deployed objects

    • Reverses the order of operations determined at the time of deployment

Examples

Example 1

  • Create variable

  • Create local value using variable value

  • Create null_resource to call a bash command

  • Create Output blocks to print the values of the variable and local value

Example 2

  • Create variable

  • Create local values containing several embedded values using the variable to build the names

  • Create Output blocks to print the variable and local values

Example 3

  • Create and run a module that creates an Azure resource-group

  • Create output blocks that print the values of the module

Example 4

  • Example usage of the ternary conditional and the count meta-argument

NEXT