Chris Snow
Chris Snow

Reputation: 24646

how to lookup availability zone name from availability zone id?

In some places in my tf file, I am providing an availability_zone_id. E.g.

variable "az_id" { }

resource "aws_subnet" "main" {
  vpc_id     = "${aws_vpc.main.id}"
  cidr_block = "${var.subnet_cidr_block}"
  availability_zone_id = "${var.az_id}"
  map_public_ip_on_launch = true
}

... and in the my.tfvars file:

az_id              = "euw2-az1"

In other places, I need to provide an availablity_zone name. E.g.

resource "aws_ebs_volume" "controller-ebs-sdb" { 
  availability_zone = "${var.az}"  
  size              = 1024 
  type              = "gp2"
}         

Is there a way in terraform to lookup the availability zone name from the availability zone id?

Upvotes: 0

Views: 1431

Answers (2)

user11389395
user11389395

Reputation: 345

another way is to get the availability zones that are supported on that region then use an index. Then can this index.

Example:

data "aws_availability_zones" "available_az" {}

Then on your resource use the following with index. If you want to use your 1st AZ then use index 0. If you want to use your 2nd AZ then use index 1.

availability_zone = "${element(data.aws_availability_zones.available_az.names, 0)}"

Upvotes: 1

Chris Snow
Chris Snow

Reputation: 24646

I fixed this by creating a variable az and removing the az_id

az = "eu-west-2a"

... and adding a data element

data "aws_availability_zone" "main" {
  name = "${var.az}"
}

resource "aws_subnet" "main" {                                          
  ...                          
  availability_zone_id = "${data.aws_availability_zone.main.zone_id}"   
  ...                                                                                                       
}  

resource "aws_ebs_volume" "controller-ebs-sdb" {
  availability_zone = "${var.az}"
  ...
}

Upvotes: 1

Related Questions