Write your first code with Terraform on Azure

Morsi Masmoudi
5 min readJul 21, 2023

--

Terraform is a powerful infrastructure as code (IaC) tool that allows you to define and manage your cloud infrastructure in a declarative manner.

With Terraform, you can create and manage infrastructure resources on various cloud platforms, including Microsoft Azure.

Writing your first code with Terraform for Azure can be an exciting and rewarding experience, as it enables you to automate the deployment of your infrastructure and streamline your operations.

In this article, we’ll explore the basics of writing Terraform code for Azure, including how to set up your development environment, create your first Terraform configuration file, and deploy your resources on Azure.

Whether you’re a seasoned developer or just getting started with Terraform, this article will provide you with the knowledge and tools to start writing infrastructure code with Terraform for Azure.

Prerequisites

Install Azure CLI for Windows

Install Terraform for Windows

Connect to your azure account

Of course, to be able to use Terraform with Azure, you need to sign in to your Azure account. Since you have already installed Azure CLI, launch the terminal and type az login.

The sign-in window for your account will appear, where you can select the correct account and sign in.

Terraform Lifecycle

The Terraform lifecycle consists of four stages: init, plan, apply, and destroy.

Initialisation -> Planning -> Applying -> Destroying

  • Init : Terraform init initializes the working directory that includes all configuration files.
  • Plan: The Terraform plan is used to create an execution plan to reach the desired state of the infrastructure. Changes in the configuration files are made to achieve the desired state.
  • Apply: Terraform apply then applies the modifications to the infrastructure as defined in the plan, and the infrastructure reaches the desired state.
  • Destroy: Terraform destroy is used to delete all old infrastructure resources, which are marked as corrupted after the apply phase.

Terraform Directory Setup

For the purpose of our tutorial, we will create a Terraform directory and place our files in it. This is a very simple organization for the moment, but of course, you will create your own organization.

terraform_folder
└──main.tf
  • main.tf — The main Azure resources to deploy.

It is not necessary to name the initial Terraform file main.tf. It is simply a common naming convention for the main file that contains the Terraform configuration.

You can give any other name to this file if you want, but it is important to ensure that the file name is referenced correctly in the Terraform command.

Create your first code

  • Create a folder with hthe name of your project.
  • Create the main file for terraform.

Edit your main.tf file with your code editor or just with a node pad.

Copy paste the code below :

terraform {

required_version = ">=0.12"

required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "3.0.0"
}
}
}

provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "rg" {
name = "RG-TEST"
location = "france"
}

Run your first code

In Azure Cli, navigate to the Terraform code directory to initialize the directory, then run the command:

terraform init

In our exemple of code, naviguate to C:\terraform\project_02

Run the init terraform command

Various files & folders relating to Terraform initialising are created as part of this

plan the execution with:

terraform plan

An error was generated. There is no region “France” in Azure.

We need to choose the correct Azure region authorized, and for that, we can refer to this page to know exactly how to write the Azure region. You can also refer to the error message to choose the exact name of region.

We change “france” with “francecentral” and we run terraform plan

It’s now ok to run the apply command.

terraform apply

You must approve the action with “yes”.

And the result:

We verify the creation on Azure via the portal:

Although this example is straightforward as it only involves creating an empty resource group in a single file without discussing variables, dynamic programming, creating virtual machines or networking…

The aim is to familiarize yourself with Terraform.

To learn more, stay tuned to my account.

Destroy the ressource

If you create resources for training purposes, don’t forget to delete them. It’s always good to keep your Azure account clean (to distinguish what’s for testing and what’s for production) and also to avoid incurring unnecessary charges, even if it’s just a resource group in this example.

Run the destroy command:

terraform destroy

you must approve the destruction.

--

--

Morsi Masmoudi

IT engineer, azure cloud trainer and administrator, Azure AZ-104 certified and PMP certified