>_ IzzyLab

Hands-on task

This task is step 2 of the course “Terraform and Ansible”

Terraform: variable and output

medium~15 min

In /root/tf write a config with the local provider: a variable filename (defaulting to /opt/out/hello.txt) and a local_file resource that creates this file with the content hi. Add an output path that returns the path to the file. Run apply. There's no internet in the sandbox: only the hashicorp/local provider is available (it sits in a local mirror, terraform init will find it on its own). Write the config in the Editor tab (the /root/tf directory is already created) and run terraform in the terminal.

What you'll practise:
  • terraform
  • variable
  • output
  • local_file
  • terraform apply

Topic breakdown

Terraform describes infrastructure declaratively: you declare the desired state, and the tool brings the system to it on apply. The three basic building blocks of a configuration are the variable block (an input parameter referenced via var.<name>), resource (what exactly to create), and output (what to return outward after applying). Values from some blocks are substituted into others via references to their attributes, tying the config into a single graph. The working cycle always starts with terraform init, which pulls in the providers.

What's checked

  1. /opt/out/hello.txt contains hi
  2. output path returns the path to the file
  3. the file path comes from the filename variable
  4. infrastructure matches the config (plan shows no changes)

What it looks like