Skip to content
Log In Sign Up info@muvcloud.com

Infrastructure as Code

Clicking servers together in a control panel works until you need the same setup twice, or need to know who changed what. Infrastructure as code puts the description in files. The files go into Git, changes are reviewed like any other code, and the same files can build a test copy of production.

Terraform files use HCL, a configuration language with blocks and key-value pairs. They describe an end state, not a list of steps: you say "two servers and one DNS record", and Terraform works out what to create, change, or delete.

Providers and Resources

  • Provider: a plugin that talks to one API, such as a cloud platform, a DNS service, a virtualization host, or a database. Providers are downloaded from a registry.
  • Resource: one thing a provider manages: a server, a firewall rule, a record.
  • Data source: something Terraform reads but does not manage, such as an existing image.

A Short Example

This example uses the local provider, which manages files on your own computer, so it runs anywhere without an account:

terraform {
  required_providers {
    local = {
      source = "hashicorp/local"
    }
  }
}

resource "local_file" "motd" {
  filename = "${path.module}/motd.txt"
  content  = "Managed by Terraform\n"
}

A real setup has the same shape: a provider block for your platform, and resources for servers, networks, and records with their settings.

The Cycle: init, plan, apply, destroy

CommandWhat it does
terraform initDownloads providers
terraform planShows what would change
terraform applyMakes the changes
terraform destroyRemoves everything
terraform fmtFormats the files

Read the plan every time. A changed setting can mean "replace the server", which deletes the old one and its disk. Lines marked for destruction deserve a second look before you type yes. Terraform runs on your computer or in a pipeline and opens no port on the servers it creates; which ports they open is up to the firewall rules you declare.

The State File

Terraform records what it created in a state file, terraform.tfstate, and compares your files with it on every run. Three rules follow:

  • It can hold secrets: passwords and keys that resources return are stored in plain text. Never commit it to Git; add it to .gitignore.
  • Share it remotely with locking: a team keeps the state in a remote backend, such as S3-compatible object storage or a PostgreSQL database, with a lock so two runs cannot change it at once.
  • Do not edit it by hand: use terraform state subcommands, and back it up.

Variables and Modules

Variables keep values such as sizes and names out of the resource blocks, and outputs print results such as a new server's address. A module is a folder of Terraform files used as a building block, for example "a web server with its firewall rules", called several times with different variables.

The License Change and OpenTofu

In 2023 HashiCorp moved Terraform from an open-source license to the Business Source License. In response, a community fork named OpenTofu was started under the Linux Foundation. It keeps an open-source license, uses the same language, and is run with the tofu command. For most users the files work in both; check provider and feature support for your case.

Terraform and Configuration Management

Terraform creates the server; it is not built to manage the packages and files inside it. That is the job of a configuration tool such as Ansible (see what Ansible is), or of cloud-init at first boot. How the two kinds of tool split the work is covered in Ansible vs Terraform.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations