DEV Community

Cover image for Setup Terraform for Azure
Cheulong Sear
Cheulong Sear

Posted on • Edited on

Setup Terraform for Azure

Before we start, let me talk briefly about each tools and what is it used for:

  • Terraform is Infrastructure as Code(IaC) tool from HashiCorp that automates the provisioning, updating, and destruction of infrastructure resources.
  • Azure is a cloud computing platform. Azure offers a wide range of cloud services, including compute, storage, networking, analytics, and AI.

Let start

Install Terraform

Here we use Ubuntu, Install Terraform - Ubuntu

sudo wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install terraform
Enter fullscreen mode Exit fullscreen mode

Then run terraform -v

Terraform v1.11.4
on linux_amd64

Your version of Terraform is out of date! The latest version
is 1.12.1. You can update by downloading from https://developer.hashicorp.com/terraform/install
Enter fullscreen mode Exit fullscreen mode

Install Azure CLI

There are other ways to connect terraform with azure beside azure cli

We can just follow the official instruction, Install Azure CLI- Ubuntu

curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash
Enter fullscreen mode Exit fullscreen mode

Then run az version

{
  "azure-cli": "2.74.0",
  "azure-cli-core": "2.74.0",
  "azure-cli-telemetry": "1.1.0",
  "extensions": {}
}
Enter fullscreen mode Exit fullscreen mode

Config Azure Credentials

We can connect to Azure by type az login and follow the instructions.
azlogin
type az account show to show the details of a subscription

azaccountshow

Terraform Configuration

Create main.tf and config azure provider as in the document Azure Provider

# main.tf
# We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

# Create a resource group
resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

Enter fullscreen mode Exit fullscreen mode

type terraform init to initializing the backend

tfinit

then, type terraform validate to validate the code

tfvalidate

next, we run terraform plan, this will compare the actual state and desire state and show the plan what is the config will do.

tfplan

Finally, terraform apply to apply to Azure cloud

tfapply

azure

Terraform also allows you to modify the config code.

# Let change location of resource group
resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "Southeast Asia"
}
Enter fullscreen mode Exit fullscreen mode

type terraform validate, then terraform plan, then terraform apply

tfapply1

tfnew

We also can destroy resources via terraform destroy command

tfdestroy

tfdestroyazure

Caution terraform destroy will remove all resources that mention in the config, you should modify the config and use terraform apply instead.

Common commands

  • terraform init: to initializing the backend
  • terraform fmt: to format the terraform code
  • terraform validate: to validate the terraform code
  • terraform plan: to compare the actual state and desire state
  • terraform apply: to apply change to the actual infrastructure
  • terraform destroy: to remove the resource from the cloud infrastructure

Best practice

  • Manipulate state only through TF commands
  • Remote State
  • State Locking
  • Back up State File
  • Use 1 State per Environment
  • Host TF code in Git repository
  • CI for TF Code
  • Execute TF only in an automated build

Repo of this code

(back to top)

Leave a comment if you have any questions.

===========
Please keep in touch
Portfolio
Linkedin
Github
Youtube

Top comments (0)