Infrastructure as Code: Terraform and OpenTofu

Why Infrastructure as Code (IaC) is needed

Even after teams automated application builds with CI/CD (see DevOps and CI-CD Pipelines), provisioning underlying IT infrastructure—virtual machines (VMs), networks, load balancers, and databases—often remained a manual task. System administrators logged into cloud web dashboards or ran ad-hoc API commands to click together servers by hand.

Manual “click-ops” is disorganized, fragile, and opaque: other team members cannot see what changed, environments drift out of sync, and rebuilding an identical setup is slow and error-prone. Infrastructure as Code (IaC) solves this by applying DevOps practices to infrastructure: resources are codified into human-readable text files committed to a version control system such as Git, giving teams oversight, change management, automated testing, version history, fast deployment, and high scalability.

How Terraform works

HashiCorp Terraform is an IaC tool that manages both cloud and on-premises infrastructure across its entire lifecycle—from low-level compute, storage, and networking resources to high-level DNS entries and SaaS features.

Platform independence through Providers

Terraform does not hardcode cloud-specific logic. Instead, it communicates with target platforms through plugins called Providers that translate your configuration into calls against each Target API (e.g., AWS, Google Cloud, Azure, Datadog, or Proxmox). Any platform with an accessible API can be managed through a provider.

Your Config Files ──▶ Terraform ◀──▶ Terraform Provider (e.g., AWS / Proxmox) ◀──▶ Target Platform API
  • Added analogy (not from the slides): Think of a Provider like a travel plug adapter—your workflow and configuration language stay identical while the adapter translates them for whichever cloud or hypervisor socket you plug into. (Limit: different providers still expose different resource schemas, so an AWS EC2 block cannot be pasted unchanged onto a Proxmox provider.)

The core 3-stage workflow: Write → Plan → Apply

Every infrastructure change in Terraform follows three stages (PDF pp. 19–20 / printed slides 19–20):

StageInputs & mechanismOutput & purpose
1. WriteYou define desired resources in configuration files (e.g., VMs inside a Virtual Private Cloud (VPC) with security groups and a load balancer).Human-readable, version-controlled infrastructure definition alongside the Terraform State File.
2. Plan (terraform plan)Terraform compares your configuration files against the Terraform State File and the live environment.Prints an execution plan detailing every resource it will create, update, or destroy before anything changes.
3. ApplyAfter approval, Terraform executes the plan against provider APIs in the correct dependency order.Provisions/updates live resources and updates the state file to match reality.
  • Worked dependency trace (PDF p. 19 / printed slide 19): Suppose your configuration defines a VPC and a group of VMs inside that VPC, and you edit both the VPC’s properties (requiring replacement) and the number of VMs. During Plan, Terraform warns you of the VPC replacement and VM scaling; during Apply, it respects resource dependencies by recreating the VPC first and scaling the VMs afterward.

Added practical clarification: how environment variables reach a VM

The high-level slides show Terraform provisioning a VM, which often raises a practical question: how does an environment variable or config file actually get inside the running VM? (Clarification outside the slides):

  1. At VM boot (cloud-init / user-data): The Terraform config attaches a startup script or metadata block; when the hypervisor or cloud boots the VM, cloud-init writes variables into /etc/environment or an application .env file.
  2. After boot (configuration management such as Ansible): Terraform creates the VM and outputs its IP address, after which a tool like Ansible connects over SSH, writes the .env or service configuration file, and restarts the service.

Terraform State File and secrets caveat (added practical context)

If Terraform writes sensitive values (API keys or database passwords) directly into VM metadata or resources, those values can be stored inside the Terraform State File. Teams must encrypt and restrict access to remote state—or better, pass only secret names and let the VM fetch real credentials at runtime from a secret manager (see GitOps Workflow Components and Tradeoffs).

From Terraform to OpenTofu

Because HashiCorp changed Terraform’s license in 2023, the open-source community created OpenTofu:

  • Timeline (PDF p. 21 / printed slide 21):
    • August 10, 2023: HashiCorp announces Terraform’s move to the BSL (Business Source License).
    • August 15–25, 2023: The OpenTF Manifesto is released and an open-source fork of Terraform is announced.
    • September 5–20, 2023: The OpenTF repository goes public (32K+ GitHub stars) and is accepted by the Linux Foundation, becoming OpenTofu.
    • October 4, 2023 – January 10, 2024: Alpha, 1.6.0 beta (with a new public open-source registry), release candidate, and first stable production release.
  • Drop-in compatibility (PDF pp. 22–23 / printed slides 22–23): OpenTofu preserves Terraform’s workflows (Write -> Plan -> Apply) and HashiCorp Configuration Language (HCL) files—for example declaring required_providers (hashicorp/aws), provider "aws", and resource "aws_s3_bucket" "example". The slides also outline an OpenTofu demonstration using Proxmox as the provider to clone a VM from a template and verify readiness via SSH.

Common confusion: IaC vs. GitOps

Using Terraform/OpenTofu files in Git is Infrastructure as Code, not automatically GitOps. With plain IaC, an engineer or pipeline still manually or imperatively triggers plan and apply. GitOps adds an automated reconciliation loop where a deployment tool continuously watches Git and syncs live infrastructure to match (see GitOps Workflow Components and Tradeoffs).

Retrieval

  1. You edit a Terraform configuration to modify a VPC setting (which requires recreating the VPC) and increase the VMs inside that VPC from 3 to 5. Why is running terraform plan critical before apply, and in what order does apply execute these two changes?
    Running terraform plan previews every create, update, and destroy operation against the state file, alerting you that the VPC will be destroyed and recreated before any live outage occurs. During apply, Terraform resolves resource dependencies and recreates the VPC first before scaling the VMs inside it.
  2. A team currently uses OpenTofu to manage AWS buckets and wants to use the same tool to provision on-premises VMs on Proxmox. Which architectural component makes this possible without changing the core tool?
    Providers. A Proxmox Terraform/OpenTofu provider translates the configuration blocks into calls to Proxmox’s API, just as the AWS provider translates configurations into AWS API calls.
  3. Why did OpenTofu emerge in 2023–2024, and what does “drop-in replacement” mean for existing Terraform users?
    OpenTofu was forked under the Linux Foundation after HashiCorp changed Terraform’s license to the BSL in August 2023. Being a drop-in replacement means existing Terraform HCL configurations, provider blocks, and Write -> Plan -> Apply workflows continue to work without rewriting infrastructure definitions.

Source: Lecture 14 - DevOps and GitOps, PDF pages 13–23 (printed slides 13–23).