DevOps

Terraform

Terraform is an infrastructure as code tool: you describe cloud resources in configuration and it creates and changes them.

What it is

Terraform by HashiCorp lets you describe infrastructure (servers, networks, databases, DNS) in the declarative language HCL and manage it through providers for AWS, Azure, Google Cloud, Cloudflare and hundreds of other services.

The workflow: terraform plan shows what will change, terraform apply applies it. The current state is kept in a state file, usually moved to remote storage with locking. Configuration is split into modules for reuse.

About the license. In 2023 HashiCorp moved from MPL-2.0 to the Business Source License 1.1, which led to the OpenTofu fork under the Linux Foundation. In 2025 HashiCorp became part of IBM.

Use it when

  • Infrastructure must be reproducible and versioned in Git.
  • You manage several environments and clouds the same way.
  • You need to review changes (plan) before applying.
  • You automate provisioning through CI.

Avoid it when

  • You need a fully open source license: consider OpenTofu.
  • The task is configuring software inside servers rather than creating resources: use Ansible.
  • One or two resources that are easier to create by hand.

Key concepts

Provider
A plugin that talks to a specific platform API (aws, google, cloudflare).
State
A file mapping configuration to real resources; it is protected and stored remotely.
Plan / apply
First show the changes, then execute them, so consequences are visible in advance.
Module
A reusable set of resources with input variables and outputs.
Drift
A mismatch between configured state and real infrastructure after manual changes.

Example

The AWS provider and one S3 bucket. Then terraform plan and terraform apply.

Cheat sheet

Terraform cheat sheet
Command or codeWhat it does
terraform initDownload providers and initialize the directory
terraform fmtFormat configuration files
terraform validateValidate configuration syntax
terraform planShow what will change
terraform applyApply the changes
terraform destroyDestroy all managed infrastructure
terraform outputShow output values
terraform state listResources tracked in state
terraform import aws_s3_bucket.b my-bucketBring an existing resource under management
terraform plan -out=tfplan && terraform apply tfplanApply exactly the plan you reviewed

How to learn it

  1. Understand infrastructure as code: you describe the desired state and Terraform brings the cloud to it.
  2. Install Terraform and create a first resource through a provider (for example Docker or a local file) with no cloud.
  3. Learn the init, plan, apply, destroy cycle and read the plan before applying it.
  4. Master variables, outputs, locals and data blocks, and split config into files.
  5. Understand state: why you must not lose or commit it, and move it to a remote backend with locking.
  6. Extract repeated code into modules and use a ready made module from the registry.
  7. Build real cloud infrastructure (VPC, server, database) in a separate test account.
  8. Run plan in CI on pull requests and manage environments with workspaces or separate state.

Interview questions

  1. 1. What is state and why does it matter?

    State links resources in the configuration to real objects in the cloud. Without it Terraform does not know what is already created. It is stored in a remote backend with locking because it can hold secrets.

  2. 2. What does terraform plan do?

    It compares configuration with state and reality and shows what will be created, changed or destroyed, without making changes. You can review consequences before apply.

  3. 3. How does Terraform differ from Ansible?

    Terraform declaratively creates and changes infrastructure resources and keeps state. Ansible mostly configures software on servers with procedural playbooks and no state.

  4. 4. What changed with the Terraform license?

    In 2023 HashiCorp moved from MPL-2.0 to BSL 1.1, which restricts competing commercial use. The community created the OpenTofu fork under MPL-2.0 at the Linux Foundation.

Interview questions from the bank

Comparisons with this technology

Logos belong to their owners and are used for identification only. Icons: Simple Icons (CC0), Devicon (MIT).