'how to create single dlm policy with multiple tags

Trying to create a DLM (data lifecycle management) policy to take ebs snapshots every 12 hours. AWS UI allows to specify multiple target tags to combine multiple instance's into one policy.

Trying to create the same using terraform, https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/dlm_lifecycle_policy#target_tags

target_tags doesn't seem to take multiple map values -

    target_tags = {
      Name = "g1n1",
      Name = "g1n2"
    }

Terraform plan is taking the last map value from the list -

  # aws_dlm_lifecycle_policy.example has changed
  ~ resource "aws_dlm_lifecycle_policy" "example" {
        id                 = "policy-0719fc4b498671592"
        tags               = {}
        # (5 unchanged attributes hidden)

      ~ policy_details {
          ~ target_tags    = {
              ~ "Name" = "g1n1" -> "g1n2"
            }
            # (1 unchanged attribute hidden)

            # (1 unchanged block hidden)
        }
    }

To overcome this I am creating 2 policies for each instance, but is there any way to create single policy for multiple instance?

resource "aws_dlm_lifecycle_policy" "DLM_g1n1" {
    description = "DLM_g1n1"
    execution_role_arn = "AWSDataLifecycleManagerDefaultRole"
    policy_details {
      resource_types = ["INSTANCE"]
      schedule {
        name = "snapshot-every12hours"
        create_rule {
          interval      = 12
          interval_unit = "HOURS"
          times         = ["04:30"]
        }         
      retain_rule { count = 28 }
      copy_tags = false
    }
    target_tags = {
      Name = "g1n1"
    }
   }
}
resource "aws_dlm_lifecycle_policy" "DLM_g1n2" {
    description = "DLM_g1n2"
    execution_role_arn = "AWSDataLifecycleManagerDefaultRole"
    policy_details {
      resource_types = ["INSTANCE"]
      schedule {
        name = "cedcas-snapshot-every12hours"
        create_rule {
          interval      = 12
          interval_unit = "HOURS"
          times         = ["04:30"]
        }         
      retain_rule { count = 28 }
      copy_tags = false
    }
    target_tags = {
      Name = "g1n2"
    }
   }
}


Solution 1:[1]

There does not seem to be possible in TF. But instead of creating fully separate aws_dlm_lifecycle_policy, you can use count or for_each:

variable "tags" {
    default = [{Name = "g1n1"}, {Name = "g1n2"}]
}

resource "aws_dlm_lifecycle_policy" "DLM_g1" {

    count = length(var.tags)

    description = "DLM_g1n${count.index}"
    execution_role_arn = "AWSDataLifecycleManagerDefaultRole"
    policy_details {
      resource_types = ["INSTANCE"]
      schedule {
        name = "snapshot-every12hours"
        create_rule {
          interval      = 12
          interval_unit = "HOURS"
          times         = ["04:30"]
        }         
      retain_rule { count = 28 }
      copy_tags = false
    }
    target_tags = var.tags[count.index]
   }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Marcin