Terraform basics ~ Creating our first resource in Azure using terraform

Here in this article we'll write a very simple configuration using Terraform and deploy a resource using that configuration. We'll use variables to make life a bit easier.

Downloading requirements to start with Terraform

We'll download the below items -

  1. Terraform binary file - Download from here
  2. A code editor - Download VS Code, IntelliJ Idea Community (Recommended), or any of your favorite.
  3. Install Terraform extension for your code editor. 
      For VS Code - Go to Extensions → search for HashiCorp Terraform
      For IntelliJ Idea - Go to settings (Ctrl+ alt + S) → Plugins → search for HashiCorp Terraform
  4. Install Azure CLI (For Linux) or PowerShell Azure module (For Windows)
    1. For Debian based OS run curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
    2. For other Linux based OS, please visit the Azure installation guide
    3. For Windows, open PowerShell as Administrator and run  Install-Module -Name Az


Setting Alias to run Terraform binary

Setting alias of Terraform binary, so that we can run it by typing terraform in terminal/PowerShell console


For Windows - 

  1. Extract downloaded Terraform binary ZIP file to C:\Windows\terraform.exe

Or another way is to add an alias in PowerShell $Profile

  1. Extract downloaded Terraform binary ZIP file to C:\Users\<Username>\Downloads\Terraform.exe
  2. Go to PowerShell and run the below script (I'm assuming terraform.exe is in your Downloads folder)


$terraform_location = "C:\$env:USERNAME\Downloads\Terraform.exe"  ## Change this location to wherever your terraform.exe file exists.
if (Test-Path $PROFILE) {
    if ( (get-content $PROFILE) -match "Set-Alias -Name 'Terraform'" ) {
        Write-Host 'Error: Alias Terraform already exists, please remove manually.'
        Write-Host 'Opening profile...'
        notepad.exe $PROFILE
    else {
        "`nSet-Alias -Name 'Terraform' -Value '$terraform_location'" | Out-File -FilePath $PROFILE -Append
        Write-Host "Alias Added"
else {
    try { New-Item -Path (Split-Path -Path $PROFILE) -ItemType Directory -ErrorAction Stop | Out-Null } catch {} 
    New-Item -Path $PROFILE -ItemType File | Out-Null
    "Set-Alias -Name 'Terraform' -Value '$terraform_location'" | Out-File -FilePath $PROFILE -Append
    Write-Host "Created Profile and Alias Added"


For Linux - 

Please change the path to your terraform location in the below line and restart the terminal-

echo "alias terraform='/home/kamal/Downloads/terraform'" >> ~/.bashrc


Writing our first configuration to create Azure Resource Group

Now we're done setting up terraform. Let's write down some configuration in any of your favorite code editors. Here I'm using the Intellij Idea community version on my Debian Linux PC.


provider "azurerm" {
    features {}
resource "azurerm_resource_group" "my-rg" {
  location = "eastus"
  name = "TECHAWARE-RG01"

Save the above syntax as any-filename.tf and open PowerShell/Terminal on the same location where this file saved. In my case, I have saved it in /home/kamal/myFirstProject path.


Login to Azure CLI/ PowerShell

  • For Linux type this in terminal: az login
  • For Windows type this in PowerShell: Login-AzAccount


Running Terraform commands (common for both Windows/Linux)


COMMAND 1.  The below command will initialize terraform and download the required provider plugins -

$  terraform init


myFirstProject $ terraform init

Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/azurerm...
- Installing hashicorp/azurerm v2.26.0...
- Installed hashicorp/azurerm v2.26.0 (signed by HashiCorp)

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, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/azurerm: version = "~> 2.26.0"

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.
myFirstProject $


COMMAND 2.  The below command will deploy the resources as per written on the Terraform file

terraform apply


myFirstProject $ terraform apply

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # azurerm_resource_group.my-rg will be created
  + resource "azurerm_resource_group" "my-rg" {
      + id       = (known after apply)
      + location = "eastus"
      + name     = "TECHAWARE-RG01"

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

azurerm_resource_group.my-rg: Creating...
azurerm_resource_group.my-rg: Creation complete after 8s [id=/subscriptions/4545225-sdmf333-sdflk-4aknmv-35lkj/resourceGroups/TECHAWARE-RG01]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.


Confirming resource creation

This is it. You have successfully created a resource group on Azure using Terraform. You can confirm this by running below command -


On Linux -

az group list -o table


myFirstProject $ az group list -o table | sort 
----------------  ------------  ---------
Name              Location      Status
PROD-RG-US932     eastus        Succeeded
TECHAWARE-RG01    eastus        Succeeded


On Windows - 

Get-AzResourceGroup | select ResourceGroupName


$ Get-AzResourceGroup | select ResourceGroupName