Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Using Regions and Availability Zones for Amazon EC2

Focus mode
Using Regions and Availability Zones for Amazon EC2 - AWS SDK for C++

Prerequisites

Before you begin, we recommend you read Getting started using the AWS SDK for C++.

Download the example code and build the solution as described in Getting started on code examples.

To run the examples, the user profile your code uses to make the requests must have proper permissions in AWS (for the service and the action). For more information, see Providing AWS credentials.

Describe Regions

To list the AWS Regions available to your AWS account, call the EC2Client’s DescribeRegions function with a DescribeRegionsRequest.

You will receive a DescribeRegionsResponse in the outcome object. Call its GetRegions function to get a list of Region objects that represent each Region.

Includes

#include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeRegionsRequest.h>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeRegionsRequest request; Aws::EC2::Model::DescribeRegionsOutcome outcome = ec2Client.DescribeRegions(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "RegionName" << std::setw(64) << "Endpoint" << std::endl; const auto &regions = outcome.GetResult().GetRegions(); for (const auto &region: regions) { std::cout << std::left << std::setw(32) << region.GetRegionName() << std::setw(64) << region.GetEndpoint() << std::endl; } } else { std::cerr << "Failed to describe regions:" << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

Describe Availability Zones

To list each availability zone available to your account, call the EC2Client’s DescribeAvailabilityZones function with a DescribeAvailabilityZonesRequest.

You will receive a DescribeAvailabilityZonesResponse in the outcome object. Call its GetAvailabilityZones function to get a list of AvailabilityZone objects that represent each availability zone.

Includes

#include <aws/ec2/model/DescribeAvailabilityZonesRequest.h>

Code

Aws::EC2::Model::DescribeAvailabilityZonesRequest request; Aws::EC2::Model::DescribeAvailabilityZonesOutcome outcome = ec2Client.DescribeAvailabilityZones(request); if (outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "ZoneName" << std::setw(20) << "State" << std::setw(32) << "Region" << std::endl; const auto &zones = outcome.GetResult().GetAvailabilityZones(); for (const auto &zone: zones) { Aws::String stateString = Aws::EC2::Model::AvailabilityZoneStateMapper::GetNameForAvailabilityZoneState( zone.GetState()); std::cout << std::left << std::setw(32) << zone.GetZoneName() << std::setw(20) << stateString << std::setw(32) << zone.GetRegionName() << std::endl; } } else { std::cerr << "Failed to describe availability zones:" << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

More Information

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.