Wellborn Tellis
Wellborn Tellis

Reputation: 61

How to add multiple instances under an elb

I am trying to add 4 ec2 instances under my elb using the count function. However, when i execute the template i am getting below error:

Error: Incorrect attribute value type on main.tf line 19, in resource "aws_elb" "web": 19: instances = ["${aws_instance.web.*.id}"] Inappropriate value for attribute "instances": element 0: string required.

provider "aws" {
  region = "${var.aws_region}"
}

resource "aws_elb" "web" {
  name = "terraform-example-elb"

  # The same availability zone as our instances
  availability_zones = ["${aws_instance.web.*.availability_zone}"]

  listener {
    instance_port     = 80
    instance_protocol = "http"
    lb_port           = 80
    lb_protocol       = "http"
  }

  # The instances are registered automatically
  instances = ["${aws_instance.web.*.id}"]
}

resource "aws_instance" "web" {
  instance_type = "m1.small"
  ami           = "${lookup(var.aws_amis, var.aws_region)}"

  # This will create 4 instances
  count = 4
}

How do i resolve this?

Upvotes: 1

Views: 1217

Answers (2)

A. Lartey
A. Lartey

Reputation: 57

I totally agree with user11391264 that your ELB won't work because it's missing health checks....Please add the code below to your script.

health_check {
    healthy_threshold   = 3
    unhealthy_threshold = 5
    timeout             = 5
    target              = "${var.elb_healthcheck_target}"
    interval            = 30
}
cross_zone_load_balancing   = true
connection_draining         = true
connection_draining_timeout = 400

variable "elb_healthcheck_target" {
  default = "HTTP:80/"
}

Upvotes: 0

user11391264
user11391264

Reputation:

The availability_zones and instances arguments expects a list, but the returned data using the splat operator also returns a list. Looks like you're passing in a list of lists using the square brackets which is why it's failing.

For example using v0.12 notation:

availability_zones = aws_instance.web[*].availability_zone

Using v0.11 notation:

availability_zones = aws_instance.web.*.availability_zone

Couple of notes:

  • The ELB won't "work" because its failing health checks until the instances start broadcasting on 80
  • m1.small is an older generation

Upvotes: 1

Related Questions