For_each and toset

Hi Team,

i am creating 3 instances using for_each and toset in my resource code block.

However how can i create only one instance out of the three instances provisioned with ansible/httpd using user_data or remote_exec.

main.tf as below.

resource "aws_instance" "demo-server" {
  ami           = "ami-053b0d53c279acc90"
  instance_type = "t2.micro"
  key_name      = "sshkey"
  vpc_security_group_ids = [aws_security_group.demo-sg.id]
  subnet_id = aws_subnet.dpw-public_subent_01.id
  for_each = toset(["Jenkins-master","Jenkins-slave","Ansible"])
  tags = {
    Name = "${each.key}"
  }
} 

maybe something like this:

locals {
  servers = {
    Jenkins-master = ""
    Jenkins-slave  = "",
    Ansible        = filebase64("${path.module}/user_data.txt")
  }
}

resource "aws_instance" "demo-server" {
  for_each = local.servers

  ami                    = "ami-053b0d53c279acc90"
  instance_type          = "t2.micro"
  key_name               = "sshkey"

  vpc_security_group_ids = [aws_security_group.demo-sg.id]
  subnet_id = aws_subnet.dpw-public_subent_01.id

  tags = {
    Name = "${each.key}"
  }
  user_data_base64 = each.value
}

in this case I used a map, key is the name, and value the string that follow

Note that I only tested with a plan and not applied