Zonas de uso Regiones de AWS y disponibilidad - AWS SDK for Java 2.x

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

Zonas de uso Regiones de AWS y disponibilidad

Descripción de regiones

Para mostrar las regiones disponibles para su cuenta, llame al método describeRegions de Ec2Client. Este método devuelve un objeto DescribeRegionsResponse. Llame al método regions del objeto devuelto para obtener una lista de objetos Region que representan cada región.

Importaciones

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture;

Código

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeRegionsAndZones { public static void main(String[] args) { Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<Void> future = describeEC2RegionsAndZonesAsync(ec2AsyncClient); future.join(); // Wait for both async operations to complete. } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Asynchronously describes the EC2 regions and availability zones. * * @param ec2AsyncClient the EC2 async client used to make the API calls * @return a {@link CompletableFuture} that completes when both the region and availability zone descriptions are complete */ public static CompletableFuture<Void> describeEC2RegionsAndZonesAsync(Ec2AsyncClient ec2AsyncClient) { // Initiate the asynchronous request to describe regions CompletableFuture<DescribeRegionsResponse> regionsResponse = ec2AsyncClient.describeRegions(); // Handle the response or exception for regions CompletableFuture<DescribeRegionsResponse> regionsFuture = regionsResponse.whenComplete((regionsResp, ex) -> { if (ex != null) { // Handle the exception by throwing a RuntimeException throw new RuntimeException("Failed to describe EC2 regions.", ex); } else if (regionsResp == null || regionsResp.regions().isEmpty()) { // Throw an exception if the response is null or the result is empty throw new RuntimeException("No EC2 regions found."); } else { // Process the response if no exception occurred and the result is not empty regionsResp.regions().forEach(region -> { System.out.printf( "Found Region %s with endpoint %s%n", region.regionName(), region.endpoint()); }); } }); CompletableFuture<DescribeAvailabilityZonesResponse> zonesResponse = ec2AsyncClient.describeAvailabilityZones(); CompletableFuture<DescribeAvailabilityZonesResponse> zonesFuture = zonesResponse.whenComplete((zonesResp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to describe EC2 availability zones.", ex); } else if (zonesResp == null || zonesResp.availabilityZones().isEmpty()) { throw new RuntimeException("No EC2 availability zones found."); } else { zonesResp.availabilityZones().forEach(zone -> { System.out.printf( "Found Availability Zone %s with status %s in region %s%n", zone.zoneName(), zone.state(), zone.regionName() ); }); } }); return CompletableFuture.allOf(regionsFuture, zonesFuture); } }

Consulte el ejemplo completo en. GitHub

Describir zonas de disponibilidad

Para mostrar las zonas de disponibilidad disponibles para su cuenta, llame al método describeAvailabilityZones de Ec2Client. Este método devuelve un objeto DescribeAvailabilityZonesResponse. Llame a su availabilityZones método para obtener una lista de AvailabilityZoneobjetos que representan cada zona de disponibilidad.

Importaciones

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture;

Código

Crear el Ec2Client.

Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build();

A continuación, llame a describeAvailabilityZones () y recupere los resultados.

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeRegionsResponse; import software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesResponse; import java.util.concurrent.CompletableFuture; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeRegionsAndZones { public static void main(String[] args) { Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<Void> future = describeEC2RegionsAndZonesAsync(ec2AsyncClient); future.join(); // Wait for both async operations to complete. } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Asynchronously describes the EC2 regions and availability zones. * * @param ec2AsyncClient the EC2 async client used to make the API calls * @return a {@link CompletableFuture} that completes when both the region and availability zone descriptions are complete */ public static CompletableFuture<Void> describeEC2RegionsAndZonesAsync(Ec2AsyncClient ec2AsyncClient) { // Initiate the asynchronous request to describe regions CompletableFuture<DescribeRegionsResponse> regionsResponse = ec2AsyncClient.describeRegions(); // Handle the response or exception for regions CompletableFuture<DescribeRegionsResponse> regionsFuture = regionsResponse.whenComplete((regionsResp, ex) -> { if (ex != null) { // Handle the exception by throwing a RuntimeException throw new RuntimeException("Failed to describe EC2 regions.", ex); } else if (regionsResp == null || regionsResp.regions().isEmpty()) { // Throw an exception if the response is null or the result is empty throw new RuntimeException("No EC2 regions found."); } else { // Process the response if no exception occurred and the result is not empty regionsResp.regions().forEach(region -> { System.out.printf( "Found Region %s with endpoint %s%n", region.regionName(), region.endpoint()); }); } }); CompletableFuture<DescribeAvailabilityZonesResponse> zonesResponse = ec2AsyncClient.describeAvailabilityZones(); CompletableFuture<DescribeAvailabilityZonesResponse> zonesFuture = zonesResponse.whenComplete((zonesResp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to describe EC2 availability zones.", ex); } else if (zonesResp == null || zonesResp.availabilityZones().isEmpty()) { throw new RuntimeException("No EC2 availability zones found."); } else { zonesResp.availabilityZones().forEach(zone -> { System.out.printf( "Found Availability Zone %s with status %s in region %s%n", zone.zoneName(), zone.state(), zone.regionName() ); }); } }); return CompletableFuture.allOf(regionsFuture, zonesFuture); } }

Consulte el ejemplo completo en GitHub.

Describir cuentas

Para ver información EC2 relacionada con su cuenta, llame al método Ec2Client. describeAccountAttributes Este método devuelve un DescribeAccountAttributesResponseobjeto. Invoca este accountAttributes método de objetos para obtener una lista de AccountAttributeobjetos. Puede recorrer la lista en iteraciones para recuperar un AccountAttribute objeto.

Puede obtener los valores de los atributos de su cuenta invocando el método del AccountAttribute attributeValues objeto. Este método devuelve una lista de AccountAttributeValueobjetos. Puede recorrer en iteración esta segunda lista para mostrar el valor de los atributos (consulte el siguiente ejemplo de código).

Importaciones

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import java.util.concurrent.CompletableFuture;

Código

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.DescribeAccountAttributesResponse; import java.util.concurrent.CompletableFuture; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DescribeAccount { public static void main(String[] args) { Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<DescribeAccountAttributesResponse> future = describeEC2AccountAsync(ec2AsyncClient); future.join(); System.out.println("EC2 Account attributes described successfully."); } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Describes the EC2 account attributes asynchronously. * * @param ec2AsyncClient the EC2 asynchronous client to use for the operation * @return a {@link CompletableFuture} containing the {@link DescribeAccountAttributesResponse} with the account attributes */ public static CompletableFuture<DescribeAccountAttributesResponse> describeEC2AccountAsync(Ec2AsyncClient ec2AsyncClient) { CompletableFuture<DescribeAccountAttributesResponse> response = ec2AsyncClient.describeAccountAttributes(); return response.whenComplete((accountResults, ex) -> { if (ex != null) { // Handle the exception by throwing a RuntimeException. throw new RuntimeException("Failed to describe EC2 account attributes.", ex); } else if (accountResults == null || accountResults.accountAttributes().isEmpty()) { // Throw an exception if the response is null or no account attributes are found. throw new RuntimeException("No account attributes found."); } else { // Process the response if no exception occurred. accountResults.accountAttributes().forEach(attribute -> { System.out.println("\nThe name of the attribute is " + attribute.attributeName()); attribute.attributeValues().forEach( myValue -> System.out.println("The value of the attribute is " + myValue.attributeValue())); }); } }); } }

Consulte el ejemplo completo en GitHub.

Más información