AWS SDK for Ruby
Developer Guide

Getting Information about Regions and Availability Zones for Amazon EC2

The following examples show you how to use the AWS SDK for Ruby together with Amazon EC2 to:

  • Get information about available Amazon EC2 regions and their endpoints.

  • Get information about available Amazon EC2 Availability Zones.

For more information about Amazon EC2 regions and Availability Zones, see Regions and Availability Zones in the Amazon EC2 User Guide for Linux Instances.

For additional code that you can use to run these examples, see the Complete Example.

Get Information about Regions and Endpoints

To get information about available regions, call the describe_regions method.

describe_regions_result = ec2.describe_regions()

In this code, ec2 is a variable representing an Aws::EC2::Client object. For more information, see the Complete Example.

To get the region names and endpoints:

  1. Get an Aws::EC2::Types::DescribeRegionsResult object, which is returned by the describe_regions method and represented in this code by the describe_regions_result variable.

  2. Use the DescribeRegionsResult object's regions attribute to get an array of Aws::EC2::Types::Region objects representing the regions.

  3. Get each region's name and endpoint by using the Region object's region_name and endpoint attributes.

describe_regions_result.regions.each do |region| puts "#{region.region_name} (#{region.endpoint})" end

Get Information about Availability Zones

To get information about Availability Zones, call the describe_availability_zones method.

describe_availability_zones_result = ec2.describe_availability_zones()

The Aws::EC2::Types::DescribeAvailabilityZonesResult object contains an array of Aws::EC2::Types::AvailabilityZone objects representing the Availability Zones. The DescribeAvailabilityZonesResult object is returned by the describe_availability_zones method and represented in this code by the describe_availability_zones_result variable.

In this code, ec2 is a variable representing an Aws::EC2::Client object. For more information, see the Complete Example.

To get the name and state of each Availability Zone, use the AvailabilityZone object's zone_name and state attributes.

describe_availability_zones_result.availability_zones.each do |zone| puts "#{zone.zone_name} is #{zone.state}" if zone.messages.count > 0 zone.messages.each do |message| " #{message.message}" end end end

To get any messages about Availability Zones:

  1. Use the AvailabilityZone object's messages attribute, which returns an Aws::EC2::Types::AvailabilityZoneMessage array.

  2. If there is at least one message in the array, use each AvailabilityZoneMessage object's message attribute to get the message.

Complete Example

The following code, which you can adapt and run, combines the preceding examples into a single example.

require 'aws-sdk-ec2' # v2: require 'aws-sdk' ec2 = Aws::EC2::Client.new(region: 'us-east-1') puts "Amazon EC2 region(s) (and their endpoint(s)) that are currently available to you:\n\n" describe_regions_result = ec2.describe_regions() describe_regions_result.regions.each do |region| puts "#{region.region_name} (#{region.endpoint})" end puts "\nAmazon EC2 availability zone(s) that are available to you for your current region:\n\n" describe_availability_zones_result = ec2.describe_availability_zones() describe_availability_zones_result.availability_zones.each do |zone| puts "#{zone.zone_name} is #{zone.state}" if zone.messages.count > 0 zone.messages.each do |message| " #{message.message}" end end end

To run this code:

  1. Install the AWS SDK for Ruby. For more information, see Installing the AWS SDK for Ruby.

  2. Set the AWS access credentials that the AWS SDK for Ruby will use to verify your access to AWS services and resources. For more information, see Configuring the AWS SDK for Ruby. Be sure the AWS credentials map to an AWS Identity and Access Management (IAM) entity with access to the AWS actions and resources described in this example. This example assumes you have set the credentials in the AWS credentials profile file or in the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables on your local system.