Using Regions and Availability Zones for Amazon EC2 - Amazon SDK for C++
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Using Regions and Availability Zones for Amazon EC2

Prerequisites

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

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

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

Describe Regions

To list the Amazon Web Services Regions available to your Amazon Web Services 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/core/Aws.h> #include <aws/ec2/EC2Client.h> #include <aws/ec2/model/DescribeRegionsRequest.h> #include <aws/ec2/model/DescribeRegionsResponse.h>

Code

Aws::EC2::EC2Client ec2Client(clientConfiguration); Aws::EC2::Model::DescribeRegionsRequest request; auto outcome = ec2Client.DescribeRegions(request); bool result = true; 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; result = false; }

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> #include <aws/ec2/model/DescribeAvailabilityZonesResponse.h>

Code

Aws::EC2::Model::DescribeAvailabilityZonesRequest describe_request; auto describe_outcome = ec2Client.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::cerr << "Failed to describe availability zones:" << describe_outcome.GetError().GetMessage() << std::endl; result = false; }

See the complete example.

More Information