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.
The following diagram compares an imperative script with a declarative Terraform configuration.

Declarative or imperative?
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.
-
Read
Terraform core loads the .tf files and the current state.
-
Refresh
Providers read the current settings of every managed object.
-
Compare
Core compares the configuration with the refreshed state and builds a plan of creates, updates and deletes.
-
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.
Core, providers and state
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.
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.