Creating Azure Resource Group using Terraform: A Step-by-Step Practical Guide
Step 1: Install Terraform
First, you need to have Terraform installed on your local machine or the environment where you’ll be working. You can download the appropriate package for your operating system from the official Terraform website: terraform install
Step 2: Set up Azure Credentials
Terraform needs to authenticate with Azure to manage resources. You can set up authentication in one of the following ways:
- Azure CLI: Install the Azure CLI and run az login to authenticate with your Azure account. Install Az Cli
- Service Principal: Create an Azure Service Principal and set the required environment variables (ARM_SUBSCRIPTION_ID, ARM_CLIENT_ID, ARM_CLIENT_SECRET, and ARM_TENANT_ID).
az ad sp create-for-rbac --name "ServicePrincipalName" --role contributor --scopes /subscriptions/<SUBSCRIPTION_ID>
$env:ARM_SUBSCRIPTION_ID="<SUBSCRIPTION_ID>"
$env:ARM_CLIENT_ID="<APP_ID>"
$env:ARM_CLIENT_SECRET="<PASSWORD>"
$env:ARM_TENANT_ID="<TENANT_ID>"
- Managed Identity: If you’re running Terraform from an Azure resource (e.g., a Virtual Machine), you can use a Managed Identity.
Step 3: Configure Terraform
In your Terraform configuration file (provider.tf), add the following provider block:
provider "azurerm" {
subscription_id = "<SUBSCRIPTION_ID>"
client_id = "<APP_ID>"
client_secret = "<PASSWORD>"
tenant_id = "<TENANT_ID>"
features {}
}
Step 4: Create Terraform Configuration File
Create a new file (e.g., main.tf) in your directory and define the Azure Resource Group using the azurerm_resource_group resource:
resource "azurerm_resource_group" "example" {
name = "my-resource-group"
location = "West Europe"
}
In this example, we’re defining an Azure Resource Group named my-resource-group in the West Europe location. You can customize the name and location as needed.
Step 5: Initialize Terraform
initialize Terraform by running the following command:
terraform init
Step 6: Plan Terraform Changes
Before applying the changes, you can preview the planned actions by running:
terraform plan
This command shows you the resources that will be created, updated, or deleted based on your configuration.
Step 7: Apply Terraform Changes
If you’re satisfied with the planned changes, you can apply them by running:
terraform apply
Terraform will prompt you to confirm the changes before proceeding. Type yes to confirm and create the Azure Resource Group.
Step 7: Verifying the changes
Open the azure portal and look for the newly made resource group.
Step 8: Deleting the resource group
It is very important to delete the resource group or anything made via terraform when they are not in use. It can be deleted using the following command:
terraform destroy
How To Create Azure Resource Group Using Terraform ?
As more organizations adopt multi-cloud strategies and deploy applications in diverse regions and instances, managing this stack has grown much more intricate. By way of solving problems manually, the provisioning process might take a lot of time, may be incorrect sometimes, or pave the way to inconsistency across different infrastructures. To offer an alternative to manual configurations or the need for different dominant tools, there is Terraform, an open-source infrastructure as code (IaC) tool that permits you to establish and arrange resources over several cloud providers like Microsoft Azure through a declarative configuration language.
The foundational concept in Azure is resource groups, which represent a logical container where resources serve a given region of the planet. Resource groups function as a means to regulate, supervise, and define access patterns for different logically related resources simply and intuitively. By defining resource groups using Terraform, you could depict consistent and repeatable deployment characteristics. Also, when doing this, thoughts on collaboration tools for team members could be enhanced, and the audit record of infrastructure could be maintained.