Terraform Workflow: Write, Plan and Apply
The Terraform mental model
Terraform is an IaC tool. Its configuration describes the desired properties of managed objects. A provider connects Terraform with a particular service or system. A module is a group of configuration files used as a unit. State records information about managed objects, including their relationship to resource addresses. Terraform uses this information when planning and managing changes.
Write: describe the desired configuration
Begin with a dedicated configuration directory. Write the settings you intend to manage. For now, imagine the bookshop needs one test server; the paper example below is not HCL and cannot be executed. Later chapters introduce real Terraform files and initialization.
Paper configuration: test environment; desired application servers: 1; catalog access unchanged.
Plan: inspect proposed actions
A normal plan compares configuration, state and available observations of managed resources to determine proposed operations. A plan may contain creates, updates, deletions or replacements, or no changes. Some values can remain unknown until apply. Review the intended scope, especially replacements and deletions. Planning may contact provider APIs or involve remote execution depending on configuration; it is not a universal offline or cost-free operation.
Apply: make approved changes
Apply executes a plan. An ordinary interactive apply can create a fresh plan and ask for confirmation; an approved saved plan is a separate workflow. In real infrastructure, operations can fail partway through, leaving some changes completed. Terraform is not a database transaction that guarantees all changes are undone on failure. Review results, protect state, and verify the application independently.
Example: add one test server
The following paper plan assumes an empty initial inventory, one desired server and no other dependencies. It illustrates the intended result of a successful create; it is not captured Terraform output. Nothing is executed.
Illustrative plan summary
- Current managed servers: 0
- Desired managed servers: 1
- Create: 1; update: 0; delete: 0
- Expected managed server count after successful apply: 1
An actual plan would contain resource addresses, attributes and other details.
What if a plan proposes replacement?
Suppose the next plan proposes replacing the test server rather than updating it in place. The correct response is to find out why, review possible downtime or data loss, and confirm the change is acceptable. A plan does not grant authorization to make changes. Do not apply an unexplained or out-of-scope action.
What Terraform does not do automatically
Terraform does not continually monitor and correct changes just because you wrote a configuration. Another workflow must trigger it. A no-change plan also does not establish that your website is healthy; an application can be misbehaving while its managed settings match the configuration. Check the service itself.
Practice and next step
Review a paper plan that creates one test server and deletes one existing storage resource. The request was to add a server only. Decide whether to approve it and explain your reasoning. Continue to Chapter 2 for the CLI, providers, state and safe local workspace setup.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Terraform Workflow: Write, Plan and Apply — knowledge check
Check the mental models and decisions introduced in this lesson.
What is the main purpose of a Terraform plan?
What connects Terraform to a supported service or system?
A paper plan proposes one create and no deletes or updates. What is the expected result if it succeeds from zero resources?
A request is to add a test server, but the plan also deletes storage. What is appropriate?
An apply fails after creating one resource. What should the operator assume?