🚥

Using Input Variables and Outputs

Overview

Working with Data in Terraform

  1. Input variables
    1. which pass info to terraform configuration
    1. defined inside configuration, and supplied when terraform is executed, like adding arguments when running a c program?
  1. Local values, or locals
    1. computed values inside the config which can be referenced throughout
    1. like variables
    1. aren’t inputted, but can be based on input variables or internal references
  1. Data is returned by Terraform with output values
    1. outputs defined in config
    1. depends on reference
    1. can be constructed from one or more elements, like locals

Input variable syntax

Example

Terraform Data Types

  1. Primitive
    1. string, number, boolean
  1. Collection
    1. list (ordered group), set (unordered), map (group of key-value pairs)
    1. a grouping of the primitive data types, like an array in C
    1. for each collection, all values must be same data type
  1. Structural
    1. similar to collection, but allow you to mix data types
    1. like a struct in C
    1. Tuples (like lists), objects (like maps)
    1. not used in basic configs

Referencing Collection values in a list

Referencing Collection values in a map

Globomantics Scenario

Potential improvements

  1. Remove AWS credentials
  1. Replace hard coded values to make config dynamic and reusable!
  1. Tags for company, project and billing
  1. Generate output of public DNS hostname

Local Values Syntax

Example

Terraform locals reference

starts with local keyword, not plura

The above image shows how to use interpolation syntax to reference two variables together as a string

Output Values Syntax

EXAMPLE

Validate and Update the configuration

Supplying Variable Values

  1. using defaults
  1. or in the command line, with - var flag
  1. or in a -var-file flag and all values in that file
  1. terraform.tfvars or terraform.tfvars.json, if in ssme directory as config file
  1. .auto.tfvars or .auto.tfvars.json, if in same directory as config file
  1. Environment variable TF_VAR_

The Evaluation preference

Summary