1. Answers
  2. Finding the Availability Zone of a Public Subnet on AWS

How Do I Find the Availability Zone of a Public Subnet on AWS?

In this example, we will use Pulumi to create a VPC with a public subnet and then retrieve the availability zone of that subnet. This will help you understand how to manage and extract information about your AWS infrastructure using Pulumi.

The program will:

  1. Create a new VPC.
  2. Create a public subnet within the VPC.
  3. Output the availability zone of the created subnet.
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";

// Create a new VPC
const vpc = new aws.ec2.Vpc("myVpc", {
    cidrBlock: "10.0.0.0/16",
    tags: {
        Name: "myVpc",
    },
});

// Create a public subnet within the VPC
const publicSubnet = new aws.ec2.Subnet("myPublicSubnet", {
    vpcId: vpc.id,
    cidrBlock: "10.0.1.0/24",
    mapPublicIpOnLaunch: true,
    tags: {
        Name: "myPublicSubnet",
    },
});

// Export the availability zone of the public subnet
export const publicSubnetAz = publicSubnet.availabilityZone;

Key Points

  • VPC Creation: We create a VPC with a specified CIDR block.
  • Subnet Creation: We create a public subnet within the VPC and enable public IP assignment on launch.
  • Availability Zone Output: We export the availability zone of the created public subnet.

Summary

This Pulumi program demonstrates how to create a VPC and a public subnet in AWS, and then retrieve the availability zone of the subnet. This is useful for understanding the distribution of your resources across different availability zones in AWS.

Deploy this code

Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.

Sign up

New to Pulumi?

Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.

Sign up