availability zones for example cloudposse terraform module subnets

285 Views Asked by At

I want to try and implement this repo in my terraform plan.

I want to have it operate across all availability zones and am trying to work out how to make sure that is the case (there isn't a list of all of them anywhere I can find?)

https://github.com/cloudposse/terraform-aws-dynamic-subnets

based off of this,

https://radix.ai/blog/2020/12/swiftly-writing-and-deploying-apis-to-stay-agile/

Currently my subnets look like this,

module "subnets" {
  source              = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/1.0.0"
  ....
  availability_zones  = ["us-east-1a", "us-east-1b","us-west-2","us-west-1","eu-west-1","eu-central-1","ap-northeast-1","ap-northeast-2","ap-southeast-1","ap-southeast-2","ap-south-1","sa-east-1","ap-south-1"]
}

Thanks in advance,

1

There are 1 best solutions below

0
On

Looking at the azs in your code, "us-east-1a", "us-east-1b","us-west-2","us-west-1","eu-west-1","eu-central-1","ap-northeast-1","ap-northeast-2","ap-southeast-1","ap-southeast-2","ap-south-1","sa-east-1","ap-south-1", it seems only the first two entries are azs, the rest are regions. Because your aws provider configuration can only point to a single region, your code should not work as is.

You can dynamically pull the "available" azs from data for the region your provider is pointing at and then use them in your module.

data "aws_availability_zones" "available" {
  state = "available"
}

module "subnets" {
  source              = "git::https://github.com/cloudposse/terraform-aws-dynamic-subnets.git?ref=tags/1.0.0"
  ....
  availability_zones  = data.aws_availability_zones.available.names
}

And to emphasize again, the above suggestion includes only the azs in the region your aws provider is pointing at.