How to use attribute from a single resource created with a for_each

I have a variable: var.MANAGER_NODE_NAME.

I want to create an A-Record for the droplet’s IP address which has the name in this variable. The droplet obviously has to be created first

My main error now is (I guess because it doesn’t exist yet):

digitalocean_droplet.droplets is object with no attributes

I create digitalocean droplets in a loop:

resource "digitalocean_droplet" "droplets" {
  for_each = local.nodes
  name  = each.key
  image = each.value.image
  size = each.value.size
}

I have ommitted SSH keys and other unecessary values


What I have tried:

Some weird sort of filter for a for, which doesn’t work.

resource "digitalocean_record" "a_record" {
  domain = digitalocean_domain.domain.name
  type = "A"
  name = "@"
  value = [ for droplet in digitalocean_droplet.droplets: droplet.id if droplet.name == var.MANAGER_NODE_NAME ]
  ttl = "60"
}

I’ve also tried using [0]:

value = digitalocean_droplet.droplets[0].ipv4_address

and the value as the key:

value = digitalocean_droplet.droplets[var.MANAGER_NODE_NAME].ipv4_address

I’ve also tried a data but I couldn’t get that working either.


How can I achieve what I need here?

Interesting, I just deleted my .terraform directory and state files and it worked with the name.