EC2. Load balancer. At least two subnets must be specified
Asked Answered
Z

3

29

I'm trying to create and configure Load Balancer. The Availability Zones section has only one subnet and one zone for me.

I faced error:

At least two subnets must be specified.

Simple question - what should I do? thanks.

Zendejas answered 13/9, 2018 at 20:36 Comment(3)
Sounds like you need to create at least one more subnet in a different AZ for your region. Once created, it should be selectable when configuring your ELB.Harvell
@Harvell yeap. I got it already. Add your answer.Zendejas
@Harvell thanks, that worked. Key point was a different AZ for region.Adcock
H
18

You'll first need to create two or more subnets in your VPC. It's typically good practice to create at least one subnet for each availability zone (AZ) in your region. Be sure to select public/private as is appropriate for your architecture.

There isn't a specific example in the AWS documentation, but be sure to first understand VPC concepts and the creation of subnets. See Scenarios and Examples for the closest example walkthroughs for creating subnets.

Selecting subnets when configuring an ELB should be straightforward now.

Harvell answered 13/9, 2018 at 21:1 Comment(2)
Can I use the same subnets for two ELBs when each of them is pointing to different instance?Colchis
@Colchis - yes you can.Gunmaker
R
4

You need two subnets in your VPC: good practice is to have one in two different availability zones, for instance a and b. The relevant section of TERRAFORM_CONFIG_NAME.tf would look something like this:

setting {
    namespace = "aws:ec2:vpc"
    name      = "Subnets"
    value     = "${lookup(var.vpc_subnets, format("%s_%s", var.location, var.availability_zone))}, ${lookup(var.vpc_subnets, format("%s_%s", var.location, var.secondary_availability_zone))}"
}

, variables.tf like this:

variable "vpc_subnets" {
  type = "map"
  default = {
    "frankfurt_a" = "subnet-12345671"
    "frankfurt_b" = "subnet-12345672"
    "frankfurt_c" = "subnet-12345673"
    "ireland_a" = "subnet-12345674"
    "ireland_b" = "subnet-12345675"
    "ireland_c" = "subnet-12345676"
  }
}

variable "availability_zone" {
  default = "a"
}

variable "secondary_availability_zone" {
  default = "b"
}


variable "regions" {
  type = "map"
  default = {
    "frankfurt" = "eu-central-1"
    "ireland" = "eu-west-1"
    "london" = "eu-west-2"
  }
}

variable "location" {
  default = "ireland"
}
Renowned answered 11/9, 2019 at 8:13 Comment(1)
Alternatively, it is also possible to simply provide a list of subnets in the value field of the TERRAFORM_CONFIG_NAME.tf like: value = "subnet-1, subnet-2, subnet-3".Renowned
H
1

There is an easier way to solve this problem. See the docs code below as an example.

resource "aws_lb" "test" {
  name               = "test-lb-tf"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.lb_sg.id]
  subnets            = [for subnet in aws_subnet.public : subnet.id]

  enable_deletion_protection = true

  access_logs {
    bucket  = aws_s3_bucket.lb_logs.id
    prefix  = "test-lb"
    enabled = true
  }

  tags = {
    Environment = "production"
  }
}

Hopefully this will help

Hardship answered 13/3, 2023 at 23:54 Comment(2)
Can you please clarify what makes it "easier" in the code provided?Abubekr
In the past, you needed to use for_each perhaps I meant you can still do it. however one can only use one line: subnets = [for subnet in aws_subnet.public : subnet.id] To avoid the complexity. Hopefully this answered your questionHardship

© 2022 - 2024 — McMap. All rights reserved.