AWS SDK for Java version 2
Developer Guide

The AWS Documentation website is getting a new look!
Try it now and let us know what you think. Switch to the new look >>

You can return to the original look by selecting English in the language selector above.

Using Regions and Availability Zones

Describing Regions

To list the Regions available to your account, call the Ec2Client's describeRegions method. It returns a DescribeRegionsResponse. Call the returned object's regions method to get a list of Region objects that represent each Region.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.Region; import software.amazon.awssdk.services.ec2.model.AvailabilityZone; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;

Code

DescribeRegionsResponse regions_response = ec2.describeRegions(); for(Region region : regions_response.regions()) { System.out.printf( "Found region %s " + "with endpoint %s", region.regionName(), region.endpoint()); System.out.println();

See the complete example on GitHub.

Describing Availability Zones

To list each Availability Zone available to your account, call the Ec2Client's describeAvailabilityZones method. It returns a DescribeAvailabilityZonesResponse. Call its availabilityZones method to get a list of AvailabilityZone objects that represent each Availability Zone.

Imports

import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.Region; import software.amazon.awssdk.services.ec2.model.AvailabilityZone; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;

Code

Create the Ec2Client.

Ec2Client ec2 = Ec2Client.create();

Then call describeAvailabilityZones() and retrieve results.

DescribeAvailabilityZonesResponse zones_response = ec2.describeAvailabilityZones(); for(AvailabilityZone zone : zones_response.availabilityZones()) { System.out.printf( "Found availability zone %s " + "with status %s " + "in region %s", zone.zoneName(), zone.state(), zone.regionName()); System.out.println();

See the complete example on GitHub.

More Information