cookbook.geuer-pollmann.de
  • Introduction
  • Command line utilities
    • bash scripting
    • cURL command line utility
    • ffmpeg - Processing Media
    • JOSE from the command line
    • jq
    • Misc. command line tools
    • Zettelkasten / Markdown
  • Azure
    • Logging in to Azure
    • Working with the REST API
    • Tracing HTTP requests with Fiddler
    • Upload a file from bash
    • Azure CLI
    • terraform
    • Azure Logic Apps
    • Azure Web Apps
    • Azure Python code snippets
    • SSH keys in ARM
    • Minimal "Azure AD Workload identity federation"
    • Federated credentials from GitHub and GitLab pipelines to Azure
    • Azure Marketplace Metered Billing- Picking the correct ID when submitting usage events
    • Manually submitting values to the Azure Metering API
    • How can a publisher/ISV access the data plane of an Azure managed application?
    • The checkZonePeers API: Is your availability zone "1" equal to my "1"?
    • Token authentication with "Azure Verizon Premium CDN"
    • Getting the right storage container name in a Bicep template
    • Event-sourcing into working memory to improve data access latency
    • Postgrex on Azure - Connecting to Azure PostgreSQL from Elixir
  • Productivity
    • Excel
    • Desktop Setup
    • Time handling and Scheduling
    • Elgato from the shell
    • Typora
Powered by GitBook
On this page
  • ARM and terraform - Side by Side
  • Providers
  • Authentication
  • The "azurerm" Provider (Azure Resource Manager)
  • The "azure_ad" Provider (Azure Resource Manager)
  • Azure-specific environment variables
  • Remote state: the "azurerm" backend
  • Authenticating to remote state backend
  • Data Sources
  • Azure Modules in the Terraform Registry
  • ARM / Terraform Interoperability
  • "azurerm_template_deployment": use ARM templates within Terraform
  • Terraform Resource Provider (RP) in Azure ARM
  • Available environments
  • Learning resources for terraform on Azure
Edit on GitHub
  1. Azure

terraform

PreviousAzure CLINextAzure Logic Apps

Last updated 3 years ago

ARM and terraform - Side by Side

Terraform

JSON w/ comments

HCL

Variables

Local variables

Resources

Resources

Modules

Explicit

Automatic

Refer by reference or resourceId

Refer by resource or data source

Providers

  • Azure (i.e. Azure Resource Manager)

  • the 'old' Azure Service Management (ASM) provider

  • Azure Active Directory (AAD)

  • Azure Stack (on-premises)

Authentication

https://www.terraform.io/docs/providers/azurerm/index.html#authenticating-to-azure

  • AZ CLI - if environment has az CLI installed, re-use existing session

  • Azure Managed Identity (on Azure compute resource)

  • Azure Service Principals (with client secrets or X.509 certs)

  • https://www.terraform.io/docs/providers/azurerm/

  • http://aka.ms/terraform

provider "azurerm" {
  version         = "~> 1.40"
  alias           = "networking"
  subscription_id = var.subscription_id
  client_id = var.client_id
  client_secret = var.client_secret
}

https://www.terraform.io/docs/providers/azuread/index.html

provider "azure_rm" {
  version         = "~> 0.7"
  subscription_id = var.subscription_id
  client_id       = var.client_id
  client_secret   = var.client_secret
}

Azure-specific environment variables

  • ARM_ENVIRONMENT - public, usgovernment, german, china

  • ARM_SUBSCRIPTION_ID - Azure subscription ID

  • ARM_TENANT_ID - Azure AD tenant ID for service principal

  • ARM_USE_MSI - Use Managed Service Identity

  • ARM_CLIENT_ID - Service principal ID

  • ARM_CLIENT_SECRET - Service principal secret

Stores state in a blob, in a container, in an Azure storage account.

terraform {
  backend "azurerm" {
    resource_group_name  = "longterm"
    storage_account_name = "chgeuer"
    container_name       = "terraformstate"
    key                  = "demo2.tfstate"
  }
}

Authenticating to remote state backend

  • Inherit authN info from outer environment, such as az CLI or service principal

  • use_msi: Managed identity within Azure Compute

  • access_key: The storage account's access key

  • sas_token: A 'shared access signature' token

terraform init –backend-config="sas_token=gh67il=="`

Alternatively, Azure CosmosDB provides an etcd protocol head.

Data Sources

Many data sources, including

  • azurerm

    • KeyVault, Networking, API Management, Compute, ...

  • azuread

    • Users, Groups, Roles, ...

Azure Modules in the Terraform Registry

http://aka.ms/tfmodules

ARM / Terraform Interoperability

Example: https://github.com/chgeuer/azure-snippets/blob/master/logic-app-reading-xml/terraform/modules/logicapp/main.tf

resource "azurerm_template_deployment" "logicapp" {
  name                   = "deployment-${formatdate("YYYY-MM-DD--hh-mm-ss", timestamp())}"
  resource_group_name    = var.resource_group_name
  deployment_mode        = "Incremental"
  template_body          = file(local.arm_template_file)
  parameters = {
    "logicAppName"       = var.logic_app_name
    "logicAppDefinition" = var.logic_app_definition
  }
}

Terraform Resource Provider (RP) in Azure ARM

  • Private preview supporting three providers: Kubernetes, Cloudflare and Datadog

  • https://azure.microsoft.com/en-us/blog/introducing-the-azure-terraform-resource-provider/

Available environments

Learning resources for terraform on Azure

The Provider (Azure Resource Manager)

The Provider (Azure Resource Manager)

Remote state: the backend

: information about all the Subscriptions currently available

: information about an existing Subscription.

: use ARM templates within Terraform

TF in the Azure Cloud Shell

and the

"azurerm"
"azure_ad"
"azurerm"
"azurerm_subscriptions"
"azurerm_subscription"
"azurerm_resource_group"
"azuread_application"
"azuread_service_principal"
"azurerm_template_deployment"
installed
shell.azure.com
Marketplace VM images w/ terraform and MSI
VS Code Plugin for terraform
aka.ms/tfhub -- docs.microsoft.com
Hashicorp Azure learning track
chrismatteson/hashicorp_azure_training
slides
CardinalNow/TerraformWorkshop
Source Code aka.ms/tfgit
Using Azure DevOps pipelines to deploy via Terraform
ARM Templates
Parameters
Variables
Functions
Functions
Nested templates
Azure loves Terraform
Screenshot from aka.ms/tfmodules
Screenshot from the Terraform VM marketplace image