Use regions and availability zones - AWS SDK for Java version 2

Use regions and availability zones

Describe 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.Ec2Exception; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;

Code

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

See the complete example on GitHub.

Describe 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.Ec2Exception; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;

Code

Create the Ec2Client.

Ec2Client ec2 = Ec2Client.create();

Then call describeAvailabilityZones() and retrieve results.

DescribeAvailabilityZonesResponse zonesResponse = ec2.describeAvailabilityZones(); for(AvailabilityZone zone : zonesResponse.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.

Describe accounts

To describe your account, call the Ec2Client’s describeAccountAttributes method. This method returns a DescribeAccountAttributesResponse object. Invoke this objects accountAttributes method to get a list of AccountAttribute objects. You can iterate through the list to retrieve an AccountAttribute object.

You can get your account’s attribute values by invoking the AccountAttribute object’s attributeValues method. This method returns a list of AccountAttributeValue objects. You can iterate through this second list to display the value of attributes (see the following code example).

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.Ec2Exception; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse;

Code

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

See the complete example on GitHub.

More information