Michal Checinski

Getting started with Terraform on Azure

October 1, 2020 , posted under Azure Terraform
Getting started with Terraform on Azure

Today I wanted to show you how to get started with Terraform to manage Aure resources. Let’s talk about what is Terraform.

Terraform is one of the IaC (Infrastructure as a Code) tools. IaC is a concept that every resource, is provisioned using files with definition of that resource. It stands as a opposite of managing resources by hand eg. clicking on the GUI, or making actions on the console. IaC can use both declarative approach (I want my infrastructure that way, I don’t need to know how you’ve done it) or by scripting (do this and that to deliver me what I want). Here is a brief video by Hashicorp describing what Infrastructure as a Code is: https://www.youtube.com/watch?v=RO7VcUAsf-I.

Now let’s dig into the steps that we need to make to work with Terraform.

1. Install Terraform on your machine

Installation steps depend on your system. There are several ways of installing Terraform regarding on your system.

You can use the binary option. Just download correct version of Terraform from official Terraform download site unzip it to location included in your PATH environment variable. There are also more ‘automated’ ways of installing it on your system using package management tools.

Windows

On Windows you can use chocolatey:

choco install terraform

macOS

On macOS you can use homebrew:

brew install hashicorp/tap/terraform

Linux

On (Debian/Ubuntu)-based distros you use those commands:

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get install terraform

For other Linux distributions, you can find instructions here: link

After typing terraform into your console/terminal window, you should get a similar result as I got here:

Terraform command output in console with list of availabe Terraform commands

2. Install Azure CLI

To use Terraform with Azure from your computer you need to install Azure CLI. How to do that is described on the official Microsoft docs page for Azure CLI.

To verify correctness of the installation simply run this command in your terminal:

az --version

If Azure Cli is installed correctly you will see the version number:

Output of the az –version command

3. Login using Azure CLI

After installing all needed tools we need to authenticate to Azure. To do that you will need to type

az login

command into your terminal. After that, you will be redirected to the Microsoft login page in your browser. After successful login, you will see information about redirection to Azure CLI docs after 10 seconds. You can close your browser. In the terminal you could see details about account and Azure environment that you’ve logged in:

Output of the az login command

4. Create your first Terraform file

Now it’s time to create your very first Terraform file. To do that create a new folder where you will place your Terraform playground, and create main.tf file there. After that open it with your favorite editor (eg. VS Code).

In the file paste text from below:

provider "azurerm" {
  version = "~>2.5.0"
}

This is the minimal code needed to use Azure integration with Terraform.

Initialize the Terraform

Now you will learn a few basic commands of the Terraform. Go to the terminal, cd into your directory with Terraform file we just created. Now we need to initialize Terraform context for this project. So type

terraform init

in your terminal. You will see the initialization process of your terraform project. It may take a while, as Terraform is downloading AzureRM module.

Output of the terraform init command

One thing to mention here that you need to run terraform init command everytime you change something in your provider, or add a new provider to your Terraform config files.

First resource provisioning using terraform

Now that we’ve initialized Terraform, we can create our first Azure resource from the Terraform. The simplest Azure resource is the resource group. Let’s do it.

We need to edit out main.tf file and add there a statement at the end of the file to create our new resource group:

resource "azurerm_resource_group" "sample" {
  name     = "rg-terraform"
  location = "West Europe"
}

You need to provide two parameters: name and location.

Now you could type terraform validate to check the correctness of your Terraform config.

The next command we need to type is:

terraform plan

This will check the config, and Terraform state file and give you information what resources needs to be added, destroyed, or changed in your Azure environment. For our config you should see something similar to this: Output of the terraform plan command

You can see that the resource group named rg-terraform in westeurope location will be added.

As it is written on the bottom of the output, let’s run the:

terraform apply

command, and see what is going to happen. Now you can see that Terraform needs to confirm the changes it’s going to make on the Azure.

Output of the Terraform apply command

Let’s cancel this by hitting Ctrl+C. Now we are going to re-enter the same command but with special flag:

terraform apply -auto-approve

As you can predict this makes Terraform approve every change without asking. The output of this command will consist information of the steps taken by the Terraform to provide the environment described in the main.tf file. In this case it’s saying that the resource group was created:

Output of the Terraform apply command

Now let’s check if the resource group was created. You can go to the portal.azure.com, but I’ll use the az command to check it out. In the --name parameter you need to set name of your resource group.

az group show --name rg-terraform

And we can see that the resource group exists in the Azure. Success!

To delete all resources created by Terraform from config files, we can just run

terraform destroy -auto-approve

What you’ve learned today

That was a lot of knowledge for the first time. Let’s just briefly summarize what we’ve done today:

  • installed Terraform
  • installed Azure CLI
  • logged in to Azure az login
  • created minimal Terraform config with AzureRM provider
  • initialized Terraform context terraform init
  • added code to create Azure resource group
  • planned resource deployment terraform plan
  • applied deployment of Azure resources using Terraform terraform apply -auto-approve
  • checked if the resource group exists using az command az group show --name rg-terraform
  • destroyed the terraform managed infrastructure terraform destroy -auto-approve

That’s it for today. I hope you’ve learned something today. If you like this blogpost please share it on your social media, and with your friends. Also observe me on twitter because I definitely will provide you more Terraform on Azure articles in the future.

Featured image by imgix on Unsplash