getAvailabilityZone

aws.getAvailabilityZone provides details about a specific availability zone (AZ) in the current region. This can be used both to validate an availability zone given in a variable and to split the AZ name into its component parts of an AWS region and an AZ identifier letter. The latter may be useful e.g., for implementing a consistent subnet numbering scheme across several regions by mapping both the region and the subnet letter to network numbers. This is different from the aws.getAvailabilityZones (plural) data source, which provides a list of the available zones.

Example Usage

The following example shows how this data source might be used to derive VPC and subnet CIDR prefixes systematically for an availability zone.

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as std from "@pulumi/std";
const config = new pulumi.Config();
const regionNumber = config.getObject("regionNumber") || {
"ap-northeast-1": 5,
"eu-central-1": 4,
"us-east-1": 1,
"us-west-1": 2,
"us-west-2": 3,
};
const azNumber = config.getObject("azNumber") || {
a: 1,
b: 2,
c: 3,
d: 4,
e: 5,
f: 6,
};
// Retrieve the AZ where we want to create network resources
// This must be in the region selected on the AWS provider.
const example = aws.getAvailabilityZone({
name: "eu-central-1a",
});
// Create a VPC for the region associated with the AZ
const exampleVpc = new aws.ec2.Vpc("example", {cidrBlock: example.then(example => std.cidrsubnet({
input: "10.0.0.0/8",
newbits: 4,
netnum: regionNumber[example.region],
})).then(invoke => invoke.result)});
// Create a subnet for the AZ within the regional VPC
const exampleSubnet = new aws.ec2.Subnet("example", {
vpcId: exampleVpc.id,
cidrBlock: pulumi.all([exampleVpc.cidrBlock, example]).apply(([cidrBlock, example]) => std.cidrsubnetOutput({
input: cidrBlock,
newbits: 4,
netnum: azNumber[example.nameSuffix],
})).apply(invoke => invoke.result),
});
import pulumi
import pulumi_aws as aws
import pulumi_std as std
config = pulumi.Config()
region_number = config.get_object("regionNumber")
if region_number is None:
region_number = {
"ap-northeast-1": 5,
"eu-central-1": 4,
"us-east-1": 1,
"us-west-1": 2,
"us-west-2": 3,
}
az_number = config.get_object("azNumber")
if az_number is None:
az_number = {
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
}
# Retrieve the AZ where we want to create network resources
# This must be in the region selected on the AWS provider.
example = aws.get_availability_zone(name="eu-central-1a")
# Create a VPC for the region associated with the AZ
example_vpc = aws.ec2.Vpc("example", cidr_block=std.cidrsubnet(input="10.0.0.0/8",
newbits=4,
netnum=region_number[example.region]).result)
# Create a subnet for the AZ within the regional VPC
example_subnet = aws.ec2.Subnet("example",
vpc_id=example_vpc.id,
cidr_block=example_vpc.cidr_block.apply(lambda cidr_block: std.cidrsubnet_output(input=cidr_block,
newbits=4,
netnum=az_number[example.name_suffix])).apply(lambda invoke: invoke.result))
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
using Std = Pulumi.Std;
return await Deployment.RunAsync(() =>
{
var config = new Config();
var regionNumber = config.GetObject<dynamic>("regionNumber") ??
{
{ "ap-northeast-1", 5 },
{ "eu-central-1", 4 },
{ "us-east-1", 1 },
{ "us-west-1", 2 },
{ "us-west-2", 3 },
};
var azNumber = config.GetObject<dynamic>("azNumber") ??
{
{ "a", 1 },
{ "b", 2 },
{ "c", 3 },
{ "d", 4 },
{ "e", 5 },
{ "f", 6 },
};
// Retrieve the AZ where we want to create network resources
// This must be in the region selected on the AWS provider.
var example = Aws.GetAvailabilityZone.Invoke(new()
{
Name = "eu-central-1a",
});
// Create a VPC for the region associated with the AZ
var exampleVpc = new Aws.Ec2.Vpc("example", new()
{
CidrBlock = Std.Cidrsubnet.Invoke(new()
{
Input = "10.0.0.0/8",
Newbits = 4,
Netnum = regionNumber[example&#46;Apply(getAvailabilityZoneResult => getAvailabilityZoneResult&#46;Region)],
}).Apply(invoke => invoke.Result),
});
// Create a subnet for the AZ within the regional VPC
var exampleSubnet = new Aws.Ec2.Subnet("example", new()
{
VpcId = exampleVpc.Id,
CidrBlock = Output.Tuple(exampleVpc.CidrBlock, example).Apply(values =>
{
var cidrBlock = values.Item1;
var example = values.Item2;
return Std.Cidrsubnet.Invoke(new()
{
Input = cidrBlock,
Newbits = 4,
Netnum = azNumber[example&#46;Apply(getAvailabilityZoneResult => getAvailabilityZoneResult&#46;NameSuffix)],
});
}).Apply(invoke => invoke.Result),
});
});
package main
import (
"github.com/pulumi/pulumi-aws/sdk/v6/go/aws"
"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
"github.com/pulumi/pulumi-std/sdk/go/std"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi/config"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
cfg := config.New(ctx, "")
regionNumber := map[string]interface{}{
"ap-northeast-1": 5,
"eu-central-1": 4,
"us-east-1": 1,
"us-west-1": 2,
"us-west-2": 3,
}
if param := cfg.GetObject("regionNumber"); param != nil {
regionNumber = param
}
azNumber := map[string]interface{}{
"a": 1,
"b": 2,
"c": 3,
"d": 4,
"e": 5,
"f": 6,
}
if param := cfg.GetObject("azNumber"); param != nil {
azNumber = param
}
// Retrieve the AZ where we want to create network resources
// This must be in the region selected on the AWS provider.
example, err := aws.GetAvailabilityZone(ctx, &aws.GetAvailabilityZoneArgs{
Name: pulumi.StringRef("eu-central-1a"),
}, nil)
if err != nil {
return err
}
invokeCidrsubnet, err := std.Cidrsubnet(ctx, &std.CidrsubnetArgs{
Input: "10.0.0.0/8",
Newbits: 4,
Netnum: regionNumber[example&#46;Region],
}, nil)
if err != nil {
return err
}
// Create a VPC for the region associated with the AZ
exampleVpc, err := ec2.NewVpc(ctx, "example", &ec2.VpcArgs{
CidrBlock: pulumi.String(invokeCidrsubnet.Result),
})
if err != nil {
return err
}
// Create a subnet for the AZ within the regional VPC
_, err = ec2.NewSubnet(ctx, "example", &ec2.SubnetArgs{
VpcId: exampleVpc.ID(),
CidrBlock: pulumi.String(exampleVpc.CidrBlock.ApplyT(func(cidrBlock string) (std.CidrsubnetResult, error) {
return std.CidrsubnetResult(interface{}(std.CidrsubnetOutput(ctx, std.CidrsubnetOutputArgs{
Input: cidrBlock,
Newbits: 4,
Netnum: pulumi.Int(azNumber[example&#46;NameSuffix]),
}, nil))), nil
}).(std.CidrsubnetResultOutput).ApplyT(func(invoke std.CidrsubnetResult) (*string, error) {
return invoke.Result, nil
}).(pulumi.StringPtrOutput)),
})
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.aws.AwsFunctions;
import com.pulumi.aws.inputs.GetAvailabilityZoneArgs;
import com.pulumi.aws.ec2.Vpc;
import com.pulumi.aws.ec2.VpcArgs;
import com.pulumi.aws.ec2.Subnet;
import com.pulumi.aws.ec2.SubnetArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
final var config = ctx.config();
final var regionNumber = config.get("regionNumber").orElse(%!v(PANIC=Format method: runtime error: invalid memory address or nil pointer dereference));
final var azNumber = config.get("azNumber").orElse(%!v(PANIC=Format method: runtime error: invalid memory address or nil pointer dereference));
// Retrieve the AZ where we want to create network resources
// This must be in the region selected on the AWS provider.
final var example = AwsFunctions.getAvailabilityZone(GetAvailabilityZoneArgs.builder()
.name("eu-central-1a")
.build());
// Create a VPC for the region associated with the AZ
var exampleVpc = new Vpc("exampleVpc", VpcArgs.builder()
.cidrBlock(StdFunctions.cidrsubnet(CidrsubnetArgs.builder()
.input("10.0.0.0/8")
.newbits(4)
.netnum(regionNumber[example&#46;applyValue(getAvailabilityZoneResult -> getAvailabilityZoneResult&#46;region())])
.build()).result())
.build());
// Create a subnet for the AZ within the regional VPC
var exampleSubnet = new Subnet("exampleSubnet", SubnetArgs.builder()
.vpcId(exampleVpc.id())
.cidrBlock(exampleVpc.cidrBlock().applyValue(cidrBlock -> StdFunctions.cidrsubnet()).applyValue(invoke -> invoke.result()))
.build());
}
}

Return

A collection of values returned by getAvailabilityZone.

Parameters

argument

A collection of arguments for invoking getAvailabilityZone.


suspend fun getAvailabilityZone(allAvailabilityZones: Boolean? = null, filters: List<GetAvailabilityZoneFilter>? = null, name: String? = null, state: String? = null, zoneId: String? = null): GetAvailabilityZoneResult

Return

A collection of values returned by getAvailabilityZone.

Parameters

allAvailabilityZones

Set to true to include all Availability Zones and Local Zones regardless of your opt in status.

filters

Configuration block(s) for filtering. Detailed below.

name

Full name of the availability zone to select.

state

Specific availability zone state to require. May be any of "available", "information" or "impaired".

zoneId

Zone ID of the availability zone to select.

See also


Return

A collection of values returned by getAvailabilityZone.

Parameters

argument

Builder for com.pulumi.aws.kotlin.inputs.GetAvailabilityZonePlainArgs.

See also