Microsoft Azure is one of the worlds leading cloud providers. While it is possible to point and click your way though the web interface to manage your Azure environment that is not how I want to live my life. This post will cover how to prepare your local development machine to enable Terraform to connect to your Azure environment and manage it as code.

For reference the following software will be used in this post.

  • azure-cli - 2.0.49
  • Terraform - 0.11.10
  • Python - 3.6.6


This post assumes you have a working Terraform installation. See here for instructions on getting Terraform installed.

Azure CLI

The azure-cli utility is required in order for Terraform to connect to and manage an Azure environment. While it is possible to install the azure-cli using your systems package manager the azure-cli utility is a written in Python so it can also be installed with pip. I prefer the pip installation method so I will go ahead and do that.

First create and activate a Python virtual environment.

mkdir -p ~/code/python/azure-cli && cd ~/code/python/azure-cli
python3 -m venv .venv
source .venv/bin/activate

Now use the pip utility to install the azure-cli package.

pip install azure-cli

Once the azure-cli is installed you will have access to the az command which is used to control your Azure environment from the cli.

az --version

# output
azure-cli (2.0.49)

Terraform Integration

Before we can start using Terraform to manage our Azure environment we need to authenticate to Azure. There are two ways to do this.

  • Service Principal - Recommended for CI/CD environments.
  • User Account - Recommended when running Terraform locally.

Since I am developing on my laptop I will use the user account method.

To login to Azure use the az login command. This will open up a web browser where you will need to enter your user credentials.


Once logged in the azure-cli will output the details pertaining to the account you are connected to.

az login

Note, we have launched a browser for you to login. For old experience with device code, use "az login --use-device-code"
You have logged in. Now let us find all the subscriptions to which you have access...

    "cloudName": "AzureCloud",
    "id": "laf82kj4-laf82kj4-laf82kj4-laf82kj4-laf82kj4",
    "isDefault": true,
    "name": "Pay-As-You-Go",
    "state": "Enabled",
    "tenantId": "laf82kj4-laf82kj4-laf82kj4-laf82kj4-laf82kj4",
    "user": {
      "name": "username@email",
      "type": "user"


With the python virtual environment still activated, create and change into a test directory.

mkdir -p ~/code/terraform/azure-test && cd ~/code/terraform/azure-test

Terraform looks for files ending with the .tf extension. Create an file, the contents of this file let Terraform know that the Azure Resource Manager provider is required.

cat << EOF >
provider "azurerm" { }

Initialize the Terraform environment with the terraform init command. This will setup the Terraform environment and download any required plugins. In our case the azurerm plugin will be installed.

terraform init

# output
Initializing provider plugins...
- Checking for available provider plugins on
- Downloading plugin for provider "azurerm" (1.17.0)...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.azurerm: version = "~> 1.17"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.


Annnndddd ..... That's it. Terraform is now able to manage your Azure environment. Look out for future posts where I will show you how to build some Azure infrastructure using Terraform.


Published: 2018-11-01