Use AWS 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.auth.credentials.ProfileCredentialsProvider; 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
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.auth.credentials.ProfileCredentialsProvider; 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.
software.amazon.awssdk.regions.Region region = software.amazon.awssdk.regions.Region.US_EAST_1; Ec2Client ec2 = Ec2Client.builder() .region(region) .credentialsProvider(ProfileCredentialsProvider.create()) .build();
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
Describe accounts
To list EC2-related information about 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.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception;
Code
public static void describeEC2Account(Ec2Client ec2) { try{ DescribeAccountAttributesResponse accountResults = ec2.describeAccountAttributes(); accountResults.accountAttributes().forEach(attribute -> { System.out.print("\n The name of the attribute is "+attribute.attributeName()); attribute.attributeValues().forEach(myValue -> System.out.print("\n The value of the attribute is "+myValue.attributeValue())); } ); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
See the
complete example
More information
-
Regions and Availability Zones in the Amazon EC2 User Guide for Linux Instances
-
DescribeRegions in the Amazon EC2 API Reference
-
DescribeAvailabilityZones in the Amazon EC2 API Reference