Menu
AWS SDK for C++
Developer Guide

Using Regions and Availability Zones for Amazon EC2

Note

These code snippets assume that you understand the material in Getting Started Using the AWS SDK for C++ and have configured default AWS credentials using the information in Providing AWS Credentials.

Describing Regions

To list the regions available to your account, call the EC2Client's DescribeRegions function with a DescribeRegionsRequest.

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

Includes

Copy
#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeRegionsRequest.h> #include <aws/ec2/model/DescribeRegionsResponse.h> #include <iostream> #include <iomanip>

Code

Copy
Aws::EC2::EC2Client ec2; Aws::EC2::Model::DescribeRegionsRequest request; auto outcome = ec2.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::cout << "Failed to describe regions:" << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

Describing 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

Copy
#include <aws/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeAvailabilityZonesRequest.h> #include <aws/ec2/model/DescribeAvailabilityZonesResponse.h> #include <iostream> #include <iomanip>

Code

Copy
Aws::EC2::EC2Client ec2; Aws::EC2::Model::DescribeAvailabilityZonesRequest describe_request; auto describe_outcome = ec2.DescribeAvailabilityZones(describe_request); if (describe_outcome.IsSuccess()) { std::cout << std::left << std::setw(32) << "ZoneName" << std::setw(20) << "State" << std::setw(32) << "Region" << std::endl; const auto &zones = describe_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::cout << "Failed to describe availability zones:" << describe_outcome.GetError().GetMessage() << std::endl; }

See the complete example.

More Information