AWS Managed AD - for loop with conditional

Hi All,

Some time ago we deployed a Managed AD instance using Directory Service resource. At the time the Directory Service Shared Directory resource wasn’t available, so we were manually sharing out directories whenever required.

Just updated the hashicorp/AWS provider and am now attempting to do the directory sharing via code. Following passes the plan phase, but fails on apply. My guess is because the AWS Account ID’s assigned to the aws_ad_consumer_acc variable already exist.

Still very green to TF. Just wondering how I might go about checking for a directory existence in the for loop before trying to create it?

resource "aws_directory_service_shared_directory" "aws_managed_ad" {
  directory_id = var.aws_ad_id
  for_each = toset(var.aws_ad_consumer_acc)

  target {
    id = each.value
  }
}

That isn’t possible. If you are now wanting Terraform to manage these directories you need to use terraform import to add each directory into the state file & take over management.