Skip to main content

Compute Across Clouds

In our Lego city analogy, compute resources are the buildings and factories where work gets done. Now imagine building across different countries each has its own construction style, but you want a consistent design.

In Terraform, this means deploying EC2 instances (AWS), Virtual Machines (Azure), and Compute Engine instances (GCP) using a unified workflow. This chapter explores how to provision compute across multiple clouds while keeping configurations consistent and scalable.


Key Concepts

1. AWS EC2

  • Elastic Compute Cloud (EC2) provides resizable compute capacity.
  • Common configuration: AMI, instance type, security groups, user data.
resource "aws_instance" "web" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.public.id
  user_data     = file("bootstrap.sh")
}

2. Azure Virtual Machines

  • Azure VMs provide scalable compute in VNets.
  • Common configuration: image, size, resource group, NSG.
resource "azurerm_linux_virtual_machine" "app_vm" {
  name                = "app-vm"
  resource_group_name = azurerm_resource_group.rg.name
  location            = "East US"
  size                = "Standard_B1s"
  admin_username      = "azureuser"
  network_interface_ids = [azurerm_network_interface.app_nic.id]
  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }
  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }
}

3. GCP Compute Engine

  • GCP VMs run workloads in global VPCs.
  • Common configuration: machine type, image, network, firewall rules.
resource "google_compute_instance" "gcp_vm" {
  name         = "gcp-vm"
  machine_type = "e2-micro"
  zone         = "us-central1-a"
  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }
  network_interface {
    network = google_compute_network.gcp_vpc.name
    access_config {}
  }
}

4. Auto‑Scaling Across Clouds

  • AWS: Auto Scaling Groups with Launch Templates.
  • Azure: Virtual Machine Scale Sets.
  • GCP: Managed Instance Groups.
  • All provide elasticity - scale up during traffic spikes, scale down during idle times.

Hands‑On Lab / Demo

Lab: Deploying Compute Across Clouds

  1. Deploy EC2 instance in AWS public subnet.
  2. Deploy Azure VM in East US VNet.
  3. Deploy GCP VM in us‑central1 VPC.
  4. Test connectivity across clouds (ping, HTTP requests).
  5. Configure auto‑scaling in each provider.

Pro Tips & Best Practices

  • Use Terraform modules for consistent VM configurations across providers.
  • Tag resources for easy identification and cost tracking.
  • Keep user data scripts lightweight - use configuration management for complex setups.
  • Spread instances across multiple AZs/regions for resilience.
  • Document differences in provider defaults (e.g. firewall rules vs security groups).

Summary & Cheatsheet

  • AWS EC2, Azure VMs, GCP Compute Engine = Compute resources.
  • Auto‑scaling: ASG (AWS), VMSS (Azure), MIG (GCP).
  • Best practice: Use modules, tag resources, document differences.
Quick mnemonic: EC2 → VM → GCE → Scale

The Hackers Notebook

Compute across clouds is the engine of multi‑cloud deployments. By deploying EC2, Azure VMs, and GCP instances consistently, you ensure workloads run smoothly across providers.


Tips, Tricks, Roadmaps, Resources, Networking, Motivation, Guidance, and Cool Stuff ♥

Updated on Dec 31, 2025