skip to Main Content

I want to get all resource groups in the subscription using Terraform.
The goal of it is to go through the storage accounts in these resource groups and create resources based on those storage accounts.

I’ve tried the following things but the list of resource groups is always empty:

data "azurerm_resources" "resource_groups" {
  type = "Microsoft.Resources/resourceGroups"
}
data "azapi_resource_list" "all_resource_groups" {
  // Get version from here: https://learn.microsoft.com/en-us/azure/templates/microsoft.resources/resourcegroups?pivots=deployment-language-bicep#resource-format
  type                   = "Microsoft.Resources/resourceGroups@2022-09-01"
  parent_id              = "/subscriptions/${data.azurerm_client_config.current.subscription_id}"
  response_export_values = ["name"]
}

I also added an output to check the result and this is what it shows:

  + all_resource_groups = {
      + id                     = "/subscriptions/<redacted>/resourceGroups"
      + output                 = jsonencode({})
      + parent_id              = "/subscriptions/<redacted>"
      + response_export_values = [
          + "name",
        ]
      + timeouts               = null
      + type                   = "Microsoft.Resources/resourceGroups@2022-09-01"
    }

EDIT: Note that after getting the resource groups I’m going to filter them in a for loop so that only certain resource groups are included but the resource groups may not exist which is why it is necessary to fetch them first.

2

Answers


  1. I tried to get the list of resource groups in Terraform using cli commands in the null resource. I was able to provision the resource successfully.

    I tried to use the module that you shared unfortunately that doesn’t work. So I tried using the CLI commands which helped list out the resource group and place them in a null resource of terraform where I could list out resource group.

    My terraform configuration:

    provider "azurerm" {
        features {}
    }
    
    
    resource "null_resource" "resource_groups_list" {
      triggers = {
        always_run = "${timestamp()}"
      }
    
      provisioner "local-exec" {
        command = <<-EOT
          az group list --query '[].name'--output json > resource_groups.json
        EOT
      }
    }
    

    Output:

    command Terrafrom Plan

    enter image description here

    Command Terraform Apply

    enter image description here

    This command will create a json with all the info related to resource groups available in the subscription.

    Now run the command cat resource_groups.json

    This will list out the available resource groups in the subscription.

    enter image description here

    Login or Signup to reply.
  2. Have you tried adding the parent (the subscription)? I managed to fetch a list of Azure Container Apps using the resource, then parsing the output and creating new resources based on the exported attributes.

    data "azapi_resource_list" "container_apps" {
      type                   = "Microsoft.App/containerApps@2022-11-01-preview"
      parent_id              = "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/resource-group-name"
      response_export_values = ["*"]
    }
    
    locals {
      # this creates map {<id>: <name>} containing the apps, suitable for for_each
      aca_apps = {for app in jsondecode(data.azapi_resource_list.container_apps.output).value: app.name => app.id }
    }
    
    module "some_module" {
      for_each             = local.aca_apps
      source               = "../_modules/some_module"
      name                 = "app-name-${each.key}"
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search