Deploying Linux Cloud Infrastructure with Terraform

This article explores HashiCorp Terraform, a leading Infrastructure as Code (IaC) tool, and details how it automates the provisioning of cloud infrastructure hosting Linux-based environments. Readers will learn the core concepts of Terraform, understand its execution lifecycle, and discover the specific mechanisms used to deploy, configure, and manage Linux virtual machines across major cloud service providers.

What is Terraform?

Terraform is an open-source Infrastructure as Code tool created by HashiCorp. It allows system administrators, DevOps engineers, and developers to define, provision, and manage cloud and on-premises resources using a human-readable, declarative configuration language known as HashiCorp Configuration Language (HCL).

Instead of manually creating virtual machines, networks, and storage through a web console or running disjointed API scripts, Terraform allows teams to treat their infrastructure like software source code. This configuration can be versioned in Git, tested, shared, and reused.

Key concepts of Terraform include:

How Terraform Deploys Cloud Infrastructure

Terraform interacts with cloud provider APIs to create the underlying hardware abstraction, networking, and virtual instances required to run an operating system like Linux. The deployment process follows a standard four-step lifecycle:

  1. Write: You write .tf files defining the target cloud provider, networking components (VPCs, subnets, firewalls), and compute instances specifying a Linux-based image.
  2. Initialize (terraform init): Terraform scans the configuration files and downloads the required provider plugins to interact with the chosen cloud platform.
  3. Plan (terraform plan): Terraform compares the current state of the infrastructure with the proposed code and outputs an execution plan showing what will be created, modified, or destroyed.
  4. Apply (terraform apply): Terraform executes the plan, making authenticated API calls to the cloud provider to provision the resources.

Deploying Linux-Based Cloud Resources

To deploy an operating system like Linux, Terraform manages both the underlying cloud compute resource and the initial OS bootstrapping process.

1. Selecting the Linux Operating System Image

Cloud providers maintain libraries of pre-built machine images containing various Linux distributions (such as Ubuntu, Red Hat Enterprise Linux, Debian, or Rocky Linux). In Terraform, these are queried or hardcoded using image identifiers (e.g., Amazon Machine Images or Azure OS disk references).

Terraform uses data sources to automatically fetch the latest certified image based on defined filters, ensuring the provisioned virtual machine always launches with the intended Linux distribution and architecture.

2. Injecting SSH Keys and Access Controls

Terraform configures the security context for the Linux machine before it boots. By passing a public SSH key via configuration attributes, the cloud platform injects the credentials directly into the Linux default user's ~/.ssh/authorized_keys file during the initial instantiation. Terraform also creates the necessary firewall rules (such as Security Groups) to allow inbound SSH traffic on port 22.

3. Bootstrapping with Cloud-Init and User Data

Once the virtual machine starts, the Linux operating system initializes and runs a standard multi-distribution package called cloud-init. Terraform leverages this capability through the user_data argument.

Through user_data, administrators pass shell scripts or cloud-config directives that run automatically with root privileges upon first boot. This allows Terraform to automate early-stage Linux configurations, such as:

Lifecycle Management and Teardown

Once deployed, Terraform tracks the Linux infrastructure inside its state file. If a configuration parameter changes—such as increasing the instance size, adding storage, or updating network rules—Terraform modifies the running environment accordingly.

When the infrastructure is no longer needed, the terraform destroy command reverses the process, safely terminating the Linux instances and associated cloud resources in the correct dependency order to prevent orphaned components and unnecessary billing.