Deploying Your First Terraform Configuration

Overview

What is Terraform

Core components

  1. Executable
    1. single binary file you call to run Terraform, contains all core functionality
  1. HCL Configuration files
    1. the config you are deploying is contained in HCL Configuration files, with extension .tf usually
    1. when there are multiple .tf files in a directory, Terraform stitches them together
  1. Provider Plugins
    1. executables invoked by Terraform to interact with a service’s API, ie AWS
    1. most common plugins are hosted on the public terraform registry
      1. registry.terraform.io
  1. State data
    1. how Terraform keeps track of what’s been deployed
    1. state data contains current info about deployment
    1. a mapping of whats defined in config to what exists in target env.
    1. Terraform, when deploying:
      1. compares updated config files to state data file
      1. calculates changes needed to make them match,
      1. makes changes
      1. then updates state data

Installing Terraform

  1. Download executable for platform
  1. Add to your PATH environment variable

You can find the installer info for Terraform here:

https://www.terraform.io/downloads.htm

NOTE

source:

https://www.coachdevops.com/2020/04/how-to-install-terraform-on-mac-os.html

Using the CLI

Usage:

  • terraform [global options] <subcommand> [args]
  • terraform -help
    • Show this help output, or the help for a specified subcommand
  • terraform -version
    • An alias for the "version" subcommand.

Terraform Object Types

  1. Providers
    1. define information about provider you want to use.
    1. i.e. AWS, which account, which region
  1. Resources
    1. things to create in target env ie Ec2, network, database
    1. bulk of what you write
    1. each resource is associated with a provider and usually requires additional config information
  1. Data Sources
    1. a way to query information from provider
    1. not creating anything
    1. just asking for data may need to use in config
    1. also linked to a provider
    1. e.g. ami for an ec2 instance, list of Availability Zones in a region

General Block Syntax

  1. each lines begins with block_type
    1. which specifies type of object being described
  1. next are a series of labels which depend on what object we are dealing with
    1. last label is usually the name label, provides a way to refer back to object in config
  1. next in block we have one or more key value pairs which make use of available arguments for object type
    1. each key is a string and value can be any of Terraform’s data types
  1. can also have nested blocks inside main block
    1. inside will be more key value pairs

Example

Base configuration file

  1. Providers
    1. identify provider, with key value pairs storing AWS account details and how we will access, and also specifying region
  1. Data
    1. lists the data sources
      1. in example, data source type is an AWS Service Manager parameter with the name label ami
      1. within config block we have a key called name and the value is the path of a parameter, which in this case grabs the latest Amazon Linux AMI ID for region we’re in
      1. this value will be used later when we create our AWS Ec2 instance

  1. Resources
    1. creates each individual resource
      1. in example we start with Networking,
      1. creating a VPC with specified Cidr block and dns hostnames setting
      1. creating an internet gateway linked to vpc using vpc.id
      1. creating a subnet in vpc with a specfied CIDR block and public IP address
      1. and more… such as a route table, in which we can create within a nested block a particular route to populate the table

    Terraform Workflow

    • terraform makes use of provider plugins as noted
    • before it uses plugins it gets them

    1. this is done by in initialisation process

    $> terraform init

    • init looks for config files inside current working directory
    • examines them to find any needed plugins
    • try and download those plugins from public Terraform Registry
    • unless an alternate location is specified
    • also prepares the back end of the state data file,
      • if not specified Terraform creates this in cd
    • once complete, ready to deploy infra

    1. next step is plan out deployment with terraform plan

    $> terraform plan

    • in this step terraform looks at current config, state date contents, determine differences, and make plan to update target env to match desired config
    • terraform prints out plan so you can verify changes to be made
    • not absolutely necessary to run, but pretty useful
    • you can save the changes of the plan to a file and then feed into terraform for next step
    • terraform indicates what will be created with a green + sign

    1. Next step is making changes in target env, done with terraform apply

    $> terraform apply

    • executes changes saved in plan using provider plugins
    • resources are created or modified
    • state data is then updated
    • if no changes are needed, terraform notifies us and makes none

    1. One last command, when done with env is terraform destroy

    $> terraform destroy

    • a dangerous command!
    • the red minus sign indicates what resources will be destroyed

    Summary

    1. Terraform is infrastructure automation
    1. Terraform is a single binary which can be used on almost any OS
    1. Configurations used by Terraform is HCL or JSON
    1. basic workflow is: init, plan and apply (and maybe destroy)