What Terraform is and how it works
Terraform is an infrastructure-as-code tool. You describe the resources you need in .tf files, and Terraform creates, changes and deletes them through the APIs of clouds and services. Before every change it shows a plan and waits for your confirmation.
Terraform suits anything that can be created through an API: virtual machines, networks and storage in the cloud, DNS records, SaaS settings. One configuration can bring up several environments in the same way — dev, staging and production. Installing and configuring software inside an existing machine is not its job; configuration management tools such as Ansible do that.
Terraform itself cannot work with any cloud. That is done by providers — plugins, each for the API of its own platform. Terraform Core reads the configuration and the state — a file that records which real objects correspond to the resources in the configuration. Core builds a plan from the difference between them, and terraform apply carries it out through the providers and records the result in the state.
The configuration is declarative: it describes the end result, not the steps. Terraform works out the order itself: it builds a dependency graph of the resources and creates or changes independent resources in parallel. Reusable sets of resources are packaged as modules.
How Terraform differs from Ansible
| Aspect | Terraform | Ansible |
|---|---|---|
| Purpose | creates and changes infrastructure: machines, networks, DNS, services | configures systems: installs and updates software, rolls out applications |
| How it is described | declaratively, in HCL | YAML playbooks describing the desired state of a system |
| What it manages through | the APIs of clouds and services, via providers | SSH, with OS credentials |
| Agent on the target system | not needed | not needed (agentless) |
| Running it again | compares the configuration with the state and the real objects, and changes only the difference | changes nothing if the system is already in the described state |
Choose Terraform to create cloud infrastructure and keep it in line with the code, and Ansible to configure the OS and software on machines that already exist. They are often combined: Terraform creates the machine and passes it cloud-init, which starts the configuration on first boot.
OpenTofu is a fork of Terraform that appeared after HashiCorp moved from an open-source licence to the BUSL (Business Source License), and it was handed over to the Linux Foundation. It is run with the tofu command, works with the same providers through its own registry, and aims for feature parity with Terraform. OpenTofu can read state created by Terraform up to and including version 1.5.x without conversion.
What you need to know first
- The basics of the cloud you work with: accounts, regions, networks and permissions.
- The command line and environment variables — see the Bash reference, section “Variables and parameter expansion”.
- Git: the configuration is kept in a repository and changed through pull requests — see the Git reference.
- Addresses and subnets — see the TCP/IP and DNS reference, section “IPv4: addresses, masks and subnets”.
Section sources
the section is written from these pages- What is Terraformdeveloper.hashicorp.com/terraform/intro/v1.17.x(opens in a new tab)
- Terraform vs. Chef, Puppet, etc.developer.hashicorp.com/terraform/intro/vs/chef-puppet(opens in a new tab)
- Introduction to Ansibledocs.ansible.com/projects/ansible/14/getting_started/introduction.html(opens in a new tab)
- FAQ | OpenTofuopentofu.org/faq/(opens in a new tab)