Creating dynamic scope using data source with filter for azure rm provider

Varma 1,190 Reputation points
2024-05-01T06:42:40.89+00:00

I would like to use dynamic scope for azure rm provider , for that I am trying to use data source as below. but it is filter with tag.

  1. I want to it through based on existing resource group,
  2. Also both based on resourece group and tag as well can we achive that if so can you shared the code for both above cases for that considering only azure rm provider
Fetch information about virtual machines
data "azurerm_virtual_machines" "vms" {
  resource_group_name = azurerm_resource_group.rg.name
}
# Filter virtual machines based on specific criteria (e.g., tags)
locals {
  filtered_vms = [for vm in data.azurerm_virtual_machines.vms : vm if contains(keys(vm.tags), "environment") && vm.tags.environment == "production"]
}
Assign maintenance configuration dynamically to filtered virtual machines
resource "azurerm_maintenance_configuration_assignment" "vm_maintenance_assignment3" {
  for_each               = { for vm in local.filtered_vms : vm.id => vm }
  name                   = "vmmcassigment-${each.value.name}"
  configuration_name     = azurerm_maintenance_configuration.vm_maintenance.name
  resource_group_name    = azurerm_resource_group.rg.name
  target_resource_id     = each.value.id
  target_resource_region = each.value.location
}
Azure Update Manager
Azure Update Manager
An Azure service to centrally manages updates and compliance at scale.
251 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sina Salam 4,056 Reputation points
    2024-05-01T12:16:56.0333333+00:00

    Hello @Varma

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    Problem

    Sequel to your questions, I understand that you would like to dynamically scope the Azure RM provider in Terraform based on existing resource groups and potentially filter virtual machines based on tags. You also want to achieve this based on existing resource groups alone or a combination of resource groups and tags. You provided a code snippet and asked for assistance in achieving this functionality based on the code.

    Scenario

    As a DevOps engineer at a tech company, you are responsible for managing infrastructure deployments on Azure using Terraform. Recently, you encountered a requirement where there is a need to dynamically scope the Azure RM provider based on existing resource groups and filter virtual machines based on specific criteria, such as tags. To accomplish this, you have plans to modify your Terraform configuration.

    Solution

    This prescribed solution was based on the scenario given and your questions, while focusing on the problem statement. There are three sections in your code based on my review as follows:

    1. To dynamically fetching information about virtual machines across all existing resource groups, not just a single one.
    2. To filtering virtual machines within those resource groups based on the "environment" tag with a value of "production".
    3. To assigning a maintenance configuration to the filtered set of virtual machines.

    In the below code snippet, I provide a revised version of your Terraform configuration with some modifications for improvements:

    # Fetch information about existing resource groups
    data "azurerm_resource_groups" "existing" {}
    # Fetch information about virtual machines within the existing resource groups
    data "azurerm_virtual_machine" "vms" {
      for_each = toset(data.azurerm_resource_groups.existing.names)
      resource_group_name = each.value
    }
    # Define a map to filter virtual machines based on specific criteria (e.g., tags)
    locals {
      filtered_vms = {
        for rg_name, vms in data.azurerm_virtual_machine.vms : rg_name => [
          for vm in vms : vm if contains(keys(vm.tags), "environment") && vm.tags["environment"] == "production"
        ]
      }
    }
    # Assign maintenance configuration dynamically to filtered virtual machines
    resource "azurerm_maintenance_configuration_assignment" "vm_maintenance_assignment" {
      for_each = local.filtered_vms
      name                = "vmmcassigment-${each.key}"
      configuration_name  = azurerm_maintenance_configuration.vm_maintenance.name
      resource_group_name = each.key
      # Iterate over the VMs in the filtered_vms map
      dynamic "target_resource_id" {
        for_each = each.value
        content {
          target_resource_id = target_resource_id.value.id
          target_resource_region = target_resource_id.value.location
        }
      }
    }
    

    Finally

    The code I provided should work for the cases you mentioned. It is filtering based on existing resource groups alone, and filtering based on both resource groups and tags. If you need to adjust the filtering criteria further, you can modify the conditions within the locals block.

    Please ensure that you have the necessary permissions and that the Azure RM provider is correctly configured in your Terraform setup. Also, verify that the azurerm_maintenance_configuration.vm_maintenance.name` corresponds to an existing maintenance configuration in your Azure environment.

    References

    Source: https://github.com/hashicorp/terraform-provider-azurerm/issues/23336. accessed. 5/1/2024.

    Source 2: Terraform Registry. Accessed. 5/1/2024.

    Source 3: Hashi Corp Tags Management accessed. 5/1/2024.

    Terraform Registry

    Accept Answer

    I hope this is helpful! Do not hesitate to let me know if you have any other questions.

    ** Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful ** so that others in the community facing similar issues can easily find the solution.

    Best Regards,

    Sina Salam


1 additional answer

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more