For_each resource referring

Hi, How do I reference a resource created with a for_each loop. I just need a simple example, here is what I have

variable “resource_group_name” {
type = list(string)
description = “The name of the Resource Group which will contain Management Resources for the Enterprise”
default = [“AAATestOne”, “AAATestTwo”]
}

variable “subnets” {
type = map(string)
default = {
“subnet1” = “10.1.1.0/24”
“subnet2” = “10.1.2.0/24”
“external” = “10.1.0.0/24”
}
}

resource “azurerm_resource_group” “main” {
for_each = toset(var.resource_group_name)
name = each.value
location = var.location
}

resource “azurerm_virtual_network” “main” {

name = “test-network”

resource_group_name = I WANT TO REF THE FIRST RESOURCE GROUP
address_space = [“10.1.0.0/16”]
location = “uk south”

dynamic “subnet” {
for_each = var.subnets
content {
name = subnet.key
address_prefix = subnet.value
}
}

As you can see from the code I want to reference the first resource group.

Thanks
R

Found out how to do - so for anyone else here is the answer.

resource_group_name = azurerm.resource_group.main[“AAATestOne”].name