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. For this, the Remote State chapter of the official manual should be consulted.

Installation

The installation instructions can be found at 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. The Terraform files are best placed in a new directory. In this directory, a Terraform file (here main.tf) is then created:

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 a login to Azure has been performed with az login. This is described in more detail in the article CLI.

In the directory with the above file, Terraform can then be executed. The command line can be opened and navigation to this directory can be performed. In the directory, a plan can be displayed of what Terraform would change:

terraform plan

The plan will show all detected changes.

Note

If multiple Subscriptions are present, a switch must be made to the one in which this infrastructure should be created using az account set -n "<NAME>".

After the plan has been verified, the changes can be applied by Terraform:

terraform apply

The plan will be displayed again before execution and can be cancelled 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 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 provides guidance 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