Skip to content
Tutorials Center
My learning

Topics

Lesson 32 of 37 ยท Cloud Examples

Terraform on Azure and Google Cloud: Equivalent Examples

See how Terraform works on Azure and Google Cloud: provider setup, a resource group with storage, a GCP bucket and service account, naming rules and multi-cloud design tips.

Terraform's workflow is the same on every cloud; only the providers and resource types change. If you understand aws_s3_bucket, you are ninety percent of the way to azurerm_storage_account and google_storage_bucket. This lesson shows equivalent examples on Azure and Google Cloud, points out the differences that trip people up (authentication, naming rules, resource hierarchy) and ends with a clear-eyed view of "multi-cloud".

Cloud accounts are not available on this site, so HCL is read-only. The runnable programs check naming rules and model the cross-cloud mapping.

Azure with the azurerm provider

Azure organizes everything into subscriptions and resource groups. Almost every resource lives in a group and a location.

HCL
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~> 4.0"
    }
  }
}

provider "azurerm" {
  features {}                       # required block, even if empty
  subscription_id = var.subscription_id
}

resource "azurerm_resource_group" "app" {
  name     = "rg-demo-prod"
  location = "westeurope"
}

resource "azurerm_storage_account" "assets" {
  name                     = "acmedemoassets42"        # 3-24 lowercase letters and digits, globally unique
  resource_group_name      = azurerm_resource_group.app.name
  location                 = azurerm_resource_group.app.location
  account_tier             = "Standard"
  account_replication_type = "GRS"
  min_tls_version          = "TLS1_2"
  allow_nested_items_to_be_public = false
}

resource "azurerm_storage_container" "uploads" {
  name                  = "uploads"
  storage_account_id    = azurerm_storage_account.assets.id
  container_access_type = "private"
}

Authentication: locally, az login is enough. In CI use a service principal or a managed identity with OIDC federation (environment variables ARM_CLIENT_ID, ARM_TENANT_ID, ARM_SUBSCRIPTION_ID, ARM_USE_OIDC=true). The state backend azurerm stores state in a blob container with lease-based locking.

Google Cloud with the google provider

Google Cloud groups resources into projects. A project also decides billing and which APIs are enabled.

HCL
terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 6.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = "europe-west3"
}

resource "google_project_service" "storage" {
  service            = "storage.googleapis.com"
  disable_on_destroy = false
}

resource "google_storage_bucket" "assets" {
  name                        = "acme-demo-assets-42"
  location                    = "EU"
  uniform_bucket_level_access = true
  public_access_prevention    = "enforced"
  versioning { enabled = true }

  depends_on = [google_project_service.storage]
}

resource "google_service_account" "uploader" {
  account_id   = "uploader"
  display_name = "Uploader service"
}

resource "google_storage_bucket_iam_member" "uploader" {
  bucket = google_storage_bucket.assets.name
  role   = "roles/storage.objectCreator"
  member = "serviceAccount:${google_service_account.uploader.email}"
}

Authentication: locally gcloud auth application-default login; in CI use Workload Identity Federation instead of downloaded JSON keys. State goes in a gcs backend bucket. Note google_project_service: on Google Cloud you must enable an API before using its resources, and depends_on expresses that hidden dependency.

Side-by-side mapping

Concept AWS Azure Google Cloud
Account boundary Account Subscription Project
Grouping Tags Resource group Labels and folders
Object storage aws_s3_bucket azurerm_storage_account + container google_storage_bucket
Virtual network aws_vpc azurerm_virtual_network google_compute_network
VM aws_instance azurerm_linux_virtual_machine google_compute_instance
Identity for workloads IAM role Managed identity Service account
State backend s3 azurerm gcs
flowchart LR
  subgraph TF["Same Terraform workflow"]
    I["init"] --> P["plan"] --> A["apply"]
  end
  A --> AWS["AWS provider"]
  A --> AZ["azurerm provider"]
  A --> GCP["google provider"]

One workflow, one language, different providers.

Naming rules differ

Cloud resources have strict naming constraints, and mistakes appear only at apply time. Encode the rules in variable validation to catch them at plan time. This program checks three of the rules above.

Python Check cloud resource names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In HCL the same idea reads:

HCL
variable "storage_account_name" {
  type = string
  validation {
    condition     = can(regex("^[a-z0-9]{3,24}$", var.storage_account_name))
    error_message = "Use 3-24 lowercase letters and digits."
  }
}

Multi-cloud in practice

Terraform lets one configuration use several providers, and that is sometimes exactly right: DNS in Cloudflare, compute in AWS, monitoring in Datadog. Using Terraform does not make your infrastructure portable, though. An aws_lb and an azurerm_application_gateway have different arguments and behavior; the code is cloud-specific even if the commands are not. Sensible advice:

  • Standardize the workflow (CI, state, policy, modules structure), not the resources.
  • Write a module per cloud behind a similar interface only if you really deploy to both.
  • Use provider aliases for several accounts or regions in the same cloud (Providers covers aliases).
Python Mapping a generic request to provider resources

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must almost every Azure resource belong to?

    Azure resources live in a resource group inside a subscription.
  2. 2. Why does a google_storage_bucket often use depends_on a google_project_service?

    Google Cloud APIs are enabled per project; the dependency is not visible through references.
  3. 3. What is the recommended way to authenticate Terraform in CI to Azure or GCP?

    Federation gives short-lived credentials per job.
  4. 4. Does using Terraform make your infrastructure portable across clouds?

    Each cloud's resource types differ; Terraform standardizes the process, not the resources.

Summary

  • The Terraform workflow is identical on AWS, Azure and Google Cloud; providers and resource types differ.
  • Azure needs a resource group and a features {} block; Google needs an enabled API and a project.
  • Validate names in variables, because naming rules differ and fail late.
  • Authenticate with OIDC or workload identity, and use each cloud's storage backend for state.
  • Multi-cloud means one workflow, not one portable codebase.

Next: Kubernetes, Helm and SaaS Providers