Terraform: azurerm and google
Terraform is the one IaC tool that speaks all three clouds, so it is where translation becomes code. The language, the plan/apply workflow and modules are identical; the providers (hashicorp/aws, hashicorp/azurerm, hashicorp/google) differ in authentication, required settings, state backends and resource names.
On AWS this is… the
awsprovider and an S3 backend, which the Terraform course covers in depth. This lesson assumes you can read HCL.
Provider setup
terraform {
required_version = ">= 1.10"
required_providers {
azurerm = { source = "hashicorp/azurerm", version = "~> 5.0" }
google = { source = "hashicorp/google", version = "~> 8.0" }
}
}
provider "azurerm" {
features {} # required, even if empty
subscription_id = var.azure_subscription_id # required since azurerm 4.0 (or ARM_SUBSCRIPTION_ID)
# azurerm 5.0 no longer registers Azure resource providers automatically:
resource_providers_to_register = ["Microsoft.ContainerService", "Microsoft.DBforPostgreSQL"]
}
provider "google" {
project = var.gcp_project_id
region = "europe-west1"
}The version constraints were current when this course was written (azurerm 5.x, google 8.x); check the registry and each provider's upgrade guide before you pin. Points that differ from the AWS provider:
azurermrequires thefeatures {}block (it holds behaviour switches such as whether deleting a resource group may delete resources Terraform doesn't know about) and asubscription_id.- Since azurerm 5.0, the provider doesn't register Azure resource providers (
Microsoft.ContainerServiceand so on) for you. Register the ones you use, throughresource_providers_to_register,az provider register, or by the platform team at subscription creation. - On Google Cloud, APIs must be enabled per project. Do it in Terraform with
google_project_serviceso a fresh project works on the first apply.
Authentication
| AWS provider | azurerm | ||
|---|---|---|---|
| Laptop | aws sso login, profile |
az login (Azure CLI credentials) |
gcloud auth application-default login |
| CI with OIDC | aws-actions/configure-aws-credentials |
ARM_USE_OIDC=true, ARM_CLIENT_ID, ARM_TENANT_ID, ARM_SUBSCRIPTION_ID (or after azure/login, the CLI credentials) |
google-github-actions/auth writes a credentials file; ADC picks it up |
| Inside the cloud | instance role | ARM_USE_MSI=true (managed identity) |
attached service account via the metadata server |
Remote state
# Azure: state in a blob container, locked with a blob lease, authenticated with Entra ID
terraform {
backend "azurerm" {
resource_group_name = "rg-tfstate"
storage_account_name = "sttfstateshop01"
container_name = "tfstate"
key = "shop/prod/network.tfstate"
use_azuread_auth = true
}
}# Google Cloud: state in a GCS bucket; locking is built in
terraform {
backend "gcs" {
bucket = "shop-tfstate-4821"
prefix = "shop/prod/network"
}
}Neither needs a separate lock table (the AWS S3 backend used DynamoDB for that until it gained native S3 locking). Turn on versioning on the state bucket (GCS object versioning; blob versioning or soft delete on Azure) so a bad apply can be rolled back.
The same network on two clouds
The VPC from the AWS course, translated. Azure:
resource "azurerm_resource_group" "net" {
name = "rg-shop-prod-network"
location = "westeurope"
tags = local.tags
}
resource "azurerm_virtual_network" "shop" {
name = "vnet-shop-prod"
location = azurerm_resource_group.net.location
resource_group_name = azurerm_resource_group.net.name
address_space = ["10.30.0.0/16"]
tags = local.tags
}
resource "azurerm_subnet" "app" {
name = "snet-app"
resource_group_name = azurerm_resource_group.net.name
virtual_network_name = azurerm_virtual_network.shop.name
address_prefixes = ["10.30.16.0/20"]
default_outbound_access_enabled = false # the provider defaults to true
}
resource "azurerm_public_ip" "nat" {
name = "pip-nat-shop"
location = azurerm_resource_group.net.location
resource_group_name = azurerm_resource_group.net.name
allocation_method = "Static"
sku = "Standard"
}
resource "azurerm_nat_gateway" "shop" {
name = "ng-shop-prod"
location = azurerm_resource_group.net.location
resource_group_name = azurerm_resource_group.net.name
}
resource "azurerm_nat_gateway_public_ip_association" "shop" {
nat_gateway_id = azurerm_nat_gateway.shop.id
public_ip_address_id = azurerm_public_ip.nat.id
}
resource "azurerm_subnet_nat_gateway_association" "app" {
subnet_id = azurerm_subnet.app.id
nat_gateway_id = azurerm_nat_gateway.shop.id
}Google Cloud:
resource "google_project_service" "compute" {
service = "compute.googleapis.com"
}
resource "google_compute_network" "shop" {
name = "shop-vpc"
auto_create_subnetworks = false # custom mode
depends_on = [google_project_service.compute]
}
resource "google_compute_subnetwork" "app_ew1" {
name = "app-ew1"
network = google_compute_network.shop.id
region = "europe-west1"
ip_cidr_range = "10.40.16.0/20"
private_ip_google_access = true
}
resource "google_compute_router" "ew1" {
name = "shop-router-ew1"
network = google_compute_network.shop.id
region = "europe-west1"
}
resource "google_compute_router_nat" "ew1" {
name = "shop-nat-ew1"
router = google_compute_router.ew1.name
region = "europe-west1"
nat_ip_allocate_option = "AUTO_ONLY"
source_subnetwork_ip_ranges_to_nat = "ALL_SUBNETWORKS_ALL_IP_RANGES"
}
resource "google_compute_firewall" "https" {
name = "allow-https-web"
network = google_compute_network.shop.id
direction = "INGRESS"
priority = 1000
source_ranges = ["0.0.0.0/0"]
target_service_accounts = ["web@shop-prod-4821.iam.gserviceaccount.com"]
allow {
protocol = "tcp"
ports = ["443"]
}
}The shapes reveal the clouds: Azure needs a resource group and threads location and resource_group_name through every resource; Google Cloud needs an enabled API and has no location on the network at all.
Model: a resource type phrasebook
PHRASEBOOK = [
# AWS azurerm google
("aws_vpc", "azurerm_virtual_network", "google_compute_network"),
("aws_subnet", "azurerm_subnet", "google_compute_subnetwork"),
("aws_nat_gateway", "azurerm_nat_gateway", "google_compute_router_nat"),
("aws_security_group", "azurerm_network_security_group", "google_compute_firewall"),
("aws_instance", "azurerm_linux_virtual_machine", "google_compute_instance"),
("aws_autoscaling_group", "azurerm_linux_virtual_machine_scale_set", "google_compute_region_instance_group_manager"),
("aws_s3_bucket", "azurerm_storage_account + _container", "google_storage_bucket"),
("aws_db_instance", "azurerm_postgresql_flexible_server", "google_sql_database_instance"),
("aws_ecr_repository", "azurerm_container_registry", "google_artifact_registry_repository"),
("aws_eks_cluster", "azurerm_kubernetes_cluster", "google_container_cluster"),
("aws_iam_role", "azurerm_user_assigned_identity", "google_service_account"),
("aws_iam_role_policy_attachment", "azurerm_role_assignment", "google_project_iam_member"),
("aws_kms_key", "azurerm_key_vault_key", "google_kms_crypto_key"),
("aws_secretsmanager_secret", "azurerm_key_vault_secret", "google_secret_manager_secret"),
]
def translate(aws_type):
for a, az, g in PHRASEBOOK:
if a == aws_type:
return az, g
return "?", "?"
plan = ["aws_vpc", "aws_subnet", "aws_subnet", "aws_nat_gateway", "aws_db_instance",
"aws_ecr_repository", "aws_eks_cluster", "aws_iam_role"]
print(f"{'AWS':<20}{'Azure':<38}Google Cloud")
for t in dict.fromkeys(plan):
az, g = translate(t)
print(f"{t:<20}{az:<38}{g}")
print(f"\n{len(plan)} AWS resources, {len(set(plan))} types; Azure also needs azurerm_resource_group,"
" Google Cloud needs google_project_service per API")AWS Azure Google Cloud
aws_vpc azurerm_virtual_network google_compute_network
aws_subnet azurerm_subnet google_compute_subnetwork
aws_nat_gateway azurerm_nat_gateway google_compute_router_nat
aws_db_instance azurerm_postgresql_flexible_server google_sql_database_instance
aws_ecr_repository azurerm_container_registry google_artifact_registry_repository
aws_eks_cluster azurerm_kubernetes_cluster google_container_cluster
aws_iam_role azurerm_user_assigned_identity google_service_account
8 AWS resources, 7 types; Azure also needs azurerm_resource_group, Google Cloud needs google_project_service per API
A phrasebook gets you started, but the counts never match one-to-one: aws_nat_gateway becomes a router and a NAT config on Google Cloud, and a NAT gateway, a public IP and two associations on Azure. Translate intent, then let each provider's documentation tell you which resources express it.
Gotchas
google_container_clusterandgoogle_sql_database_instancehavedeletion_protectionset totrueby default;terraform destroyfails until you set it tofalseand apply.google_project_iam_policyandgoogle_project_iam_bindingare authoritative (they remove grants they don't list);google_project_iam_memberis additive. Mixing them for the same role causes endless diffs.azurerm_role_assignmentneeds the principal's object ID (principal_idof an identity), not its client ID.- New Azure role assignments take time to propagate; resources that use them right away may need
depends_onplus a retry, or a shorttime_sleep.
Test yourself
Which block is required in every azurerm provider configuration, even if empty?
features {}regionprojectbackend
Why add google_project_service resources to a Google Cloud configuration?
- To create service accounts
- To enable the APIs (compute, container, sqladmin…) the other resources need in that project
- To set the region
- For billing
Which Terraform resource adds a single IAM binding member without removing other grants?
- google_project_iam_policy
- google_project_iam_binding
- google_project_iam_member
- google_project