Lesson 1 of 3 Infrastructure as Code with Terraform

What Infrastructure as Code Is

Infrastructure as code (IaC) manages infrastructure through configuration files instead of manual changes. This lesson explains the idea, the declarative approach Terraform takes and the components that turn configuration into real infrastructure.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain what infrastructure as code is and the problems it solves.
  • Distinguish declarative from imperative approaches.
  • Describe how Terraform core, providers and state work together.

From manual provisioning to code

Infrastructure has traditionally been built by hand: an administrator clicks through a cloud console, runs commands or files a ticket for someone else to do it. Manual work is slow, varies between people and leaves little record of what was done or why. Two environments that are meant to be identical slowly diverge, and rebuilding one after a failure depends on memory and documentation that is often out of date.

Infrastructure as code replaces these steps with files that describe the infrastructure. A tool reads the files and calls the platform's APIs to make reality match. Because the description is text, it can be stored in version control, reviewed, tested, reused and applied again to build another copy. Terraform configuration files use the .tf extension and the HashiCorp Configuration Language (HCL); Terraform also accepts the same structure in JSON files ending in .tf.json.

Declarative and imperative approaches

IaC tools take one of two approaches. An imperative tool, such as a shell script that calls a cloud CLI, is told the steps: create this network, then this subnet, then this server. The author must handle every situation: what if the network already exists, or the script failed half-way last time? A declarative tool is told the desired end state, and the tool works out the steps.

Terraform is declarative. The following configuration declares that one file should exist with given content; it says nothing about how to create or update the file.

resource "local_file" "motd" {
  filename = "${path.module}/motd.txt"
  content  = "Welcome to the build server\n"
}

On the first terraform apply, Terraform creates the file. On the second, it compares the configuration with what it recorded and finds nothing to do. This property is called idempotence: applying the same configuration repeatedly produces the same result without further changes. If you edit the content, Terraform plans to update the file; if you remove the block, it plans to delete the file.

Imperative script
You describeThe steps to perform
Running it twiceMay duplicate or fail unless guarded
OrderingWritten by the author
Removing somethingWrite a separate delete step
Declarative Terraform configuration
You describeThe desired end state
Running it twiceMakes no further changes
OrderingWorked out from references
Removing somethingRemove the block and apply

The following diagram compares an imperative script with a declarative Terraform configuration.

An imperative script compared with a declarative Terraform configuration. A script that creates resources fails when run twice, so engineers often delete everything by hand first. Terraform is declarative: apply on an existing environment changes only what differs, a change becomes a one-line edit reviewed with its plan, and destroy removes the whole environment.
Idempotence is what makes apply safe to run again.
Sort into groups

Declarative or imperative?

Exam skills 1.1.1 · 1.1.2

Decide whether each description fits a declarative or an imperative approach to infrastructure.

Log in to try this activity Interactive activities are free with an account.

How Terraform turns configuration into infrastructure

Three components cooperate in every Terraform run. The Terraform CLI, or Terraform core, reads the configuration, builds a dependency graph and computes a plan. Providers are plugins that know how to talk to one platform's API, such as AWS, Azure, Kubernetes or GitHub. State records which real objects belong to which resources in the configuration.

  1. Read

    Terraform core loads the .tf files and the current state.

  2. Refresh

    Providers read the current settings of every managed object.

  3. Compare

    Core compares the configuration with the refreshed state and builds a plan of creates, updates and deletes.

  4. Apply

    After approval, providers call the APIs, and core records each result in state.

HCL HashiCorp Configuration Language
The human-readable language of .tf files, built from blocks, arguments and expressions.
Provider
A plugin that adds resource types and data sources for one platform and calls its API.
State
Terraform's record of the real objects it manages and their attributes.
Match the pairs

Core, providers and state

Exam skills 1.1.3

Match each part of Terraform with its job in a run.

Log in to try this activity Interactive activities are free with an account.
Worked example

Choosing an approach

An operations team keeps a 400-line shell script that creates their test environment. It fails if run twice, so engineers delete everything by hand before running it. What would IaC with Terraform change?

The decisive problem is that the script is imperative and not idempotent.

Rewritten as a Terraform configuration, the environment is described once. Running terraform apply on an existing environment changes only what differs, so the manual clean-up disappears. A change, such as a larger database, becomes a one-line edit reviewed in a pull request with its plan, and terraform destroy removes the whole environment when it is no longer needed. The team gains a reliable record of the environment in version control.

Check yourself

What happens when you apply an unchanged Terraform configuration a second time?

Show answer

Nothing changes. Terraform is declarative and idempotent, so it finds no difference and reports no changes.

Exam Tips

Remember for the exam

  • IaC defines infrastructure in files that a tool applies through APIs.
  • Terraform is declarative and idempotent: describe the end state; repeated applies make no extra changes.
  • Core plans, providers call APIs, state maps configuration to real objects.