Bootstrap FreeKB - Amazon Web Services (AWS) - List Availability Zones using Terraform
Amazon Web Services (AWS) - List Availability Zones using Terraform

Updated:   |  Amazon Web Services (AWS) articles

A region in a separate geographical location. For example, in the USA, there are 4 regions:

  • us-east-1
  • us-east-2
  • us-west-1
  • us-west-2

Within each AWS region, there are Availabiltiy Zones which are isolated from each other. For example, the us-east-1 region has the following Availability Zones.

  • us-east-1a
  • us-east-1b
  • us-east-1c
  • us-east-1d
  • us-east-1e
  • us-east-1f

Let's say you have the following files on your Terraform server.

├── required_providers.tf
├── availability_zones (directory)
│   ├── data.tf
│   ├── outputs.tf
│   ├── provider.tf

 

required_providers.tf will almost always have this.

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
    }
  }
}

 

Let's say provider.tf has the following. In this example, the "default" profile in /home/username/.aws/config and /home/username/.aws/credentials is being used. This assumes you have setup Terraform as described in Amazon Web Services (AWS) - Getting Started with Terraform.

provider "aws" {
  alias   = "default"
  profile = "default"
  region  = "default"
}

 

data.tf could have something like this.

data "aws_availability_zones" "my-availability-zones" {}

 

outputs.tf could then have the following.

output "my_availability_zones" {
  value = data.aws_availability_zones.my-availability-zones
}

 

The terraform refresh command should then output something like this.

my-availability-zones = {
  "all_availability_zones" = tobool(null)
  "exclude_names" = toset(null) /* of string */
  "exclude_zone_ids" = toset(null) /* of string */
  "filter" = toset(null) /* of object */
  "group_names" = toset([
    "us-east-1",
  ])
  "id" = "us-east-1"
  "names" = tolist([
    "us-east-1a",
    "us-east-1b",
    "us-east-1c",
    "us-east-1d",
    "us-east-1e",
    "us-east-1f",
  ])
  "state" = tostring(null)
  "timeouts" = null /* object */
  "zone_ids" = tolist([
    "use1-az1",
    "use1-az2",
    "use1-az4",
    "use1-az6",
    "use1-az3",
    "use1-az5",
  ])
}

 

Oten, you'll want just the name of your Availability Zone. outputs.tf could have something like this.

output "my-availability-zone-id" {
  value = data.aws_availability_zones.my-availability-zones.id
}

 

And to output each Availablity Zone.

output "my-availability-zone-a" {
  value = data.aws_availability_zones.my-availability-zones.names[0]
}
output "my-availability-zone-b" {
  value = data.aws_availability_zones.my-availability-zones.names[1]
}
output "my-availability-zone-c" {
  value = data.aws_availability_zones.my-availability-zones.names[2]
}
output "my-availability-zone-d" {
  value = data.aws_availability_zones.my-availability-zones.names[3]
}
output "my-availability-zone-e" {
  value = data.aws_availability_zones.my-availability-zones.names[4]
}
output "my-availability-zone-f" {
  value = data.aws_availability_zones.my-availability-zones.names[5]
}

 

The terraform refresh command should then output something like this.

my-availability-zone-name = "us-east-1"
my-availability-zone-a = "us-east-1a"
my-availability-zone-b = "us-east-1b"
my-availability-zone-c = "us-east-1c"
my-availability-zone-d = "us-east-1d"
my-availability-zone-e = "us-east-1e"
my-availability-zone-f = "us-east-1f"

 

 

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 8743e7 in the box below so that we can be sure you are a human.