以下のようにする事で、Terrafromでインスタンスを2つ作成後に、ELBを作成して紐付けする事ができる。 検証環境を簡単に作れるので便利。
resource "aws_instance" "〇〇" { count = 2 ami = "ami-3bd3c45c" instance_type = "t2.micro" key_name = "〇〇" vpc_security_group_ids = ["〇〇"] subnet_id = "〇〇" tags { Name = "${format("instance-%d", count.index + 1)}" } }
resource "aws_elb" "〇〇" { name = "〇〇" subnets = [ "〇〇", "〇〇" ] security_groups = ["〇〇"] listener { instance_port = 80 instance_protocol = "http" lb_port = 433 lb_protocol = "https" ssl_certificate_id = "〇〇" } health_check { healthy_threshold = 2 unhealthy_threshold = 2 timeout = 5 target = "HTTP:80/" interval = 30 } instances = ["${aws_instance.〇〇.*.id}"] cross_zone_load_balancing = true connection_draining = true connection_draining_timeout = 400 tags { Name = "〇〇" } }