How to Create an Azure Resource Group Using Terraform — A Beginner’s Guide

If you’re new to Terraform and Azure, getting started can feel a little overwhelming. But don’t worry — creating your first Azure Resource Group with Terraform is simpler than you think! In this guide, I’ll walk you through the exact steps to set up your environment and deploy a resource group on Azure using Terraform.

Let’s jump right in! 🚀


What Is Terraform & Why Use It?

Terraform is an open-source Infrastructure as Code (IaC) tool. It lets you write code to create, manage, and update cloud resources — like virtual machines, storage accounts, and resource groups — instead of clicking through a web portal.

Using Terraform means your infrastructure is version-controlled, repeatable, and easy to share or modify. It’s a game-changer for beginners and pros alike.


Prerequisites Before We Begin

To follow along, make sure you have:

  • An Azure account (a free tier works just fine)
  • Terraform installed on your machine (download from terraform.io)
  • Azure CLI installed (optional but helpful for authentication)
  • A Service Principal in Azure with appropriate permissions — this acts like a user Terraform uses to authenticate with Azure securely

Step 1: Set Up Your Azure Service Principal

You can create a Service Principal — which acts like a “robot user” for Terraform — using the Azure Portal:

  1. Go to Azure Active DirectoryApp registrationsNew registration
  2. Give it a name (like terraform) and click Register
  3. After registration, copy the Application (client) ID and Directory (tenant) ID
  4. Go to Certificates & secrets → Create a new client secret and copy the value
  5. Go to Subscriptions → Your subscription → Access control (IAM) → Add role assignment
  6. Assign the Contributor role to your app

That’s it! Now you have the details needed to authenticate Terraform with Azure.


Step 2: Write Your Terraform Configuration

Create a new folder on your computer for your Terraform project. Inside it, create a file named main.tf and add this code:

provider "azurerm" {
  features {}

  subscription_id = "<your-subscription-id>"
  client_id       = "<your-appId>"
  client_secret   = "<your-password>"
  tenant_id       = "<your-tenant>"
}

resource "azurerm_resource_group" "example" {
  name     = "example-resource-group"
  location = "West Europe"
}

Make sure you replace the placeholders (<your-subscription-id>, etc.) with the values from your service principal.


Step 3: Initialize Terraform

Open your terminal (or PowerShell) and navigate to your Terraform project folder. Run:

terraform init

This downloads the Azure provider and prepares Terraform to work with your configuration.


Step 4: Apply Your Configuration

Now you’re ready to create the resource group! Run:

terraform apply

Terraform will show you a preview of what it plans to create. Review it, and if it looks good, type yes and press Enter.


Step 5: Verify Your Resource Group in Azure

Once Terraform finishes, head over to the Azure Portal and check under Resource Groups. You should see your new resource group created exactly where you specified it — “West Europe” in this example!


Bonus: Using Environment Variables for Security

Hardcoding sensitive information like your client secret in code can be risky. Instead, you can set environment variables on your machine like this:

On PowerShell:

$env:ARM_SUBSCRIPTION_ID = "<your-subscription-id>"
$env:ARM_CLIENT_ID       = "<your-appId>"
$env:ARM_CLIENT_SECRET   = "<your-password>"
$env:ARM_TENANT_ID       = "<your-tenant>"

Then update your main.tf to:

provider "azurerm" {
  features {}
}

Terraform will automatically pick up the values from your environment variables. This keeps secrets out of your code — a best practice for every Terraform project!


Wrapping Up

And there you have it — your very first Azure Resource Group created using Terraform! 🎉

Terraform lets you manage your cloud infrastructure with code, which means no more manual clicks, fewer mistakes, and a path to automation and scalability.

Ready to take it further? Next up, try creating virtual machines, storage accounts, or even entire networks with Terraform!


If you enjoyed this beginner-friendly guide, share it with a friend or drop a comment below. I love hearing your feedback and questions!

Go back

Your message has been sent

Warning
Warning
Warning
Warning.

Leave a comment