awsgeek
awsgeek

Reputation: 51

Writing Terraform Code for existing Instances aws_lb_target_group_attachment Errors Unsupported Block type

I am trying to write code for existing EC2 Instances in aws_lb_target_group_attachment and I get unsupported block type. Here is the code:

variable "target_id" {
  default = [
    {
      name = "i-123456789abcdefgh"
    },
    {
      name = "i-24680bcgdhkij1234"
    }
  ]
}
resource "aws_lb_target_group_attachment" "mytestAlb" {
  target_group_arn = aws_lb_target_group.mytestAlb.arn
  dynamic "target_id" {
    for_each = var.target_id
    content {
      name = target_id.value["name"]
    }
  }
}

I get two errors

  1. Error: missing required arguement, the target_id is required but no definition was found
  2. Error: Unsupported block type , Blocks of type target_id are not expected here. I tried different variable names and that did not work either. Sorry spent long hours so need another pair of eyes. These EC2 instances are up and running..

Upvotes: 1

Views: 316

Answers (1)

Marcin
Marcin

Reputation: 238131

This happens, because target_id is not a block. Its requires a single instance id.

Thus, you should be doing:

resource "aws_lb_target_group_attachment" "mytestAlb" {

  for_each = {for idx, v in var.target_id: idx => v}

  target_group_arn = aws_lb_target_group.mytestAlb.arn

  target_id = each.value["name"]

}

Upvotes: 1

Related Questions