Create Multiple Azure Resources With Terraform

In this blog post, I will show you how to create multiple Azure resources with Terraform count option.

Terraform Count

By default, when we create an Azure resource with Terraform, we create a single resource. But what if we need, for example, create two using the same block configuration.

For that reason, Terraform has the count argument. To show you how it works I will create two resource groups in Azure using the same code.

In the configuration below, I’m creating two resource groups with the name RG001 and RG002. To create the index number I’m using the built-in index variable which is part of the count argument.

Configuration

terraform {
  required_providers {
    azurerm = {
      source = "hashicorp/azurerm"
      version = ">= 2.26"
    }
  }
}

provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "rg" {
  count = 2  
  name     = "rg00-${count.index}"
  location = "westus2"
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.