Dokumentationen

Terraform

Terraform is a tool that helps describe infrastructure in Azure using text files. Terraform reads these files and automatically creates the Azure resources.

Note

This topic is not required to use Azure. Everything described here can also be done via the portal.

The following provides a brief introduction to Terraform. For more detailed information, refer to the official documentation. The Microsoft course Terraform on Azure Fundamentals Lab is also recommended as a more in-depth introduction to the topic.

State

Terraform requires storage for the current state of the infrastructure (also called State). This is described in detail in the State chapter of the official manual. In this article, the state is stored in the local file terraform.tfstate. However, this is not recommended for productive use. See the Remote State chapter of the official manual for more information.

Installation

For installation instructions, see this link.

Example

In the following example, a simple Resource Group is created using Terraform. Any text editor is sufficient to create the text files. It is best to place the Terraform files in a new directory. In this directory, create a Terraform file (here main.tf):

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

provider "azurerm" {}

resource "azurerm_resource_group" "example" {
  name     = "rg-example"
  location = "switzerlandnorth"
}

Execution

Note

The above configuration requires that you are logged in to Azure with az login. This is described in more detail here.

In the directory with the above file, you can then run Terraform. Open the command line and navigate to this directory. In the directory, you can display a plan of what Terraform would change:

terraform plan

The plan will show all detected changes.

Note

If you have multiple Subscriptions, you must switch to the one where this infrastructure should be created using az account set -n "<NAME>".

After verifying the plan, you can let Terraform apply the changes:

terraform apply

The plan will be displayed again before execution, and you can cancel one last time.

Azure Verified Modules (AVM)

Microsoft provides a collection of verified modules for Terraform (and Bicep), which makes it easier to create more complex infrastructures. This collection is called Azure Verified Modules. These modules are open source and help you create resources according to Microsoft's best practices.

The course Introduction to using Azure Verified modules for Terraform offers an introduction to the topic and guides you through several Terraform modules to implement a more complex setup using Terraform.

Templates

The IT services provide some templates that can be executed using Terraform.

Note

These will be published here at a later date.

References