Skip to main content

Terraform and Azure

Introduction #

Terraform is one of the most widely used tools for Infrastructure as Code. Each cloud provider has its own native deployment options, such as ARM templates and Bicep in Azure, but Terraform gives teams a common language and workflow that can work across multiple clouds, SaaS platforms, and on-premises infrastructure.

In this post, we’ll look at the basics of Terraform and how it fits with Azure. The goal is not to spend too much time on local installation, but to understand what Terraform does and what you need before using it with Azure.

Why Terraform #

Terraform is popular because many teams want a consistent way to deploy and manage infrastructure.

Even if you mainly work with Azure, you may still see Terraform used by customers, clients, or platform teams because it supports more than one environment. Instead of learning a different deployment language for every platform, teams can use Terraform as a common Infrastructure as Code workflow.

That becomes especially useful when infrastructure changes are part of DevOps pipelines. Terraform can be used locally while learning, then later integrated with Azure DevOps or GitHub Actions for more automated deployments.

Infrastructure as Code #

Infrastructure as Code means defining infrastructure in files instead of manually creating resources through a portal.

As infrastructure becomes more virtualized, we no longer manage only physical servers, routers, switches, and vendor-specific hardware tools. Cloud providers expose those resources through APIs. Terraform uses code to describe what should exist, and then works through those APIs to create, update, or remove resources.

With Terraform, the configuration is declarative. You define the desired state of the infrastructure, and Terraform works out the changes needed to reach that state.

For Azure, that could include resources such as:

  • Resource groups
  • Virtual networks
  • Subnets
  • Storage accounts
  • Virtual machines
  • AKS clusters
  • Load balancers

Terraform also lets you preview changes before applying them. This is one of the main reasons it is useful: you can run a plan, review what will happen, and then decide whether to apply the change.

State Management #

Terraform uses state to track the infrastructure it manages.

State helps Terraform understand what already exists and what needs to change. Without state, Terraform would not have a reliable way to compare your configuration with the real environment.

This is also how Terraform helps reduce configuration drift. If something changes outside Terraform, the next plan can show that the real environment no longer matches the configuration.

For a first local lab, state can be stored locally. For team use or production workflows, state should be stored remotely and protected so multiple people and pipelines can work safely.

Providers #

Terraform interacts with cloud platforms through providers.

A provider translates Terraform configuration into API calls that the target platform understands. In Azure, the main provider for Azure Resource Manager resources is azurerm.

Providers are published through the Terraform Registry. The registry is where you can find provider documentation, supported resources, arguments, and examples. When working with Azure and Terraform, the azurerm provider documentation becomes an important reference.

Providers are also updated as new platform features become available, so the registry is often the place to check what Terraform currently supports for a service.

Basic Workflow #

The usual Terraform workflow is:

  1. Write the Terraform configuration.
  2. Run terraform init to initialize the working directory and download the provider.
  3. Run terraform plan to preview the changes.
  4. Run terraform apply to apply the approved changes.

The plan step is important because it shows how the change will affect the environment before anything is created, changed, or removed.

Getting Ready for Azure #

To start using Terraform with Azure, you need a few tools:

  • Terraform
  • Azure CLI
  • A code editor such as Visual Studio Code
  • Terraform language support in the editor

Terraform is distributed as a single executable. After it is available on your system path, you can check it with:

1
terraform version

The Azure CLI is used to connect to Azure and confirm your Azure tooling is available:

1
az version

Visual Studio Code is optional, but it is a good starting editor because it has useful extensions for Terraform and Azure CLI work.

The important thing is not the installer clicks. The important thing is that your workstation can run Terraform, authenticate to Azure, and edit Terraform configuration files cleanly.

What Comes Next #

Once the basic environment is ready, the next step is writing Terraform configuration for Azure resources and learning how the provider, state file, plan, and apply workflow work together.

After that, Terraform can be moved into a DevOps workflow using Azure DevOps or GitHub Actions, where infrastructure changes can be reviewed and applied through a pipeline.