Getting Started - Azure

Create Configuration

The set of files used to describe infrastructure in Terraform is known as a Terraform configuration. You're going to write your first configuration now to create and configure a resource group in Azure.

Configuration files can be in either of two formats: HashiCorp Configuration Language (HCL), or JSON. HCL is a structured language created with DevOps in mind; it is machine-friendly yet easy for humans to read, and it supports comments. JSON is sometimes preferable when configurations are generated by a machine. A configuration can be composed of both .tf and .tf.json files. In general, we recommend that you work with HCL. The format of configuration files is documented here.

The entire configuration for this article is shown below. Copy the configuration and save it to a file named main.tf.

» Providers

The provider block is used to configure the named provider, in this instance the Azure provider (azurerm). The Azure provider is responsible for creating and managing resources on Azure.

Terraform can create configurations across multiple cloud providers. For example, a single configuration can span both Azure and AWS. Multi-cloud configurations are a common scenario. In such cases, there will be multiple cloud provider blocks present in the configuration.

A basic Azure provider block looks like this:

provider "azurerm" {
    version = "=1.20.0"
}

The version argument is optional, but recommended. It is used to constrain the provider to a specific version or a range of versions in order to prevent downloading a new provider that may possibly contain breaking changes. If the version isn't specified, Terraform will automatically download the most recent provider during initialization.

Provider blocks typically contain additional fields for identity, environment, role, and many others. A complete list of provider fields for Azure is here.

» Resources

A resource block defines a resource that exists within the infrastructure. A resource might be a physical component such as a network interface, or it can be a logical resource such as a Heroku application.

A resource block has two string parameters before opening the block: the resource type (first parameter) and the resource name (second parameter). The combination of the type and name must be unique in the configuration.

The following example uses a resource block to provision a new Azure resource group. A resource group is a fundamental container in the Azure Resource Manager deployment model, and it is required to create, modify, or destroy infrastructure on Azure. A configuration can create a new resource group or use an existing group, and often does both.

resource "azurerm_resource_group" "rg" {
    name     = "myTFResourceGroup"
    location = "eastus"
}

In this example, the resource type is azurerm_resource_group and the name is "rg". The resource name is used to refer to the object created in the resource block throughout the configuration, using interpolation (described later in this guide). It is not the same as the name of the resource group in Azure. The prefix of the type maps to the provider, "azurerm" in our case. This tells Terraform that the resource is managed by the Azure provider.

Within the resource block enclosed by {} is the configuration for that resource. The configuration options available depend on the provider. This example specified name and location, both required fields.

» Complete configuration

The following configuration will constrain the Azure provider to version 1.20.0, and then create a new resource group.

# Configure the provider
provider "azurerm" {
    version = "=1.20.0"
}

# Create a new resource group
resource "azurerm_resource_group" "rg" {
    name     = "myTFResourceGroup"
    location = "eastus"
}