I would like to spin an AWS ALB - For that I need at least two subnets in two different Availability Zones.

(Terraform shows a very explicit error in case we forget - see Error #1 Below).

I would like to check the current number of public subnets that I have in the relevant VPC, and make sure its a least 2.

My Attempt:

For that I'll calculate:

number_of_public_subnets_to_create  = "${2 - length(data.aws_subnet_ids.customer_a_public_subnets.ids)}"

For the calculation above - Terraform have 2 data sources types: aws_subnet and aws_subnet_ids.

In case the subnets are tagged - We can use the aws_subnet_ids data source and add a simple filter like this:

data "aws_subnet_ids" "customer_a_public_subnets" {
  vpc_id = "${data.aws_vpc.my-customer_a-vpc.id}"
  tags {
      Tier = "Public"

My Question:

But what if the subnets doesn't contain a "Public/Private" tags?

Is there simple solution for that (maybe a common filter on the aws_subnet data source)?

Error #1:

Error creating application Load Balancer: ValidationError: At least two subnets in two different Availability Zones must be specified.

  • Have you figured out a way to solve the Public/Private filtering ? If so, can you share ? – Auyer Feb 20 '20 at 16:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.