本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用SDK适用于 Java 2.x 的亚马逊EC2示例
以下代码示例向您展示如何在 Amazon 中使用来执行操作和实现常见场景EC2。 AWS SDK for Java 2.x
基础知识是向您展示如何在服务中执行基本操作的代码示例。
操作是大型程序的代码摘录,必须在上下文中运行。您可以通过操作了解如何调用单个服务函数,还可以通过函数相关场景的上下文查看操作。
场景是向您展示如何通过在一个服务中调用多个函数或与其他 AWS 服务结合来完成特定任务的代码示例。
每个示例都包含一个指向完整源代码的链接,您可以在其中找到有关如何在上下文中设置和运行代码的说明。
开始使用
以下代码示例展示了如何开始使用 Amazon EC2。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Asynchronously describes the security groups for the specified group ID. * * @param groupName the name of the security group to describe * @return a {@link CompletableFuture} that represents the asynchronous operation * of describing the security groups. The future will complete with a * {@link DescribeSecurityGroupsResponse} object that contains the * security group information. */ public CompletableFuture<String> describeSecurityGroupArnByNameAsync(String groupName) { DescribeSecurityGroupsRequest request = DescribeSecurityGroupsRequest.builder() .groupNames(groupName) .build(); DescribeSecurityGroupsPublisher paginator = getAsyncClient().describeSecurityGroupsPaginator(request); AtomicReference<String> groupIdRef = new AtomicReference<>(); return paginator.subscribe(response -> { response.securityGroups().stream() .filter(securityGroup -> securityGroup.groupName().equals(groupName)) .findFirst() .ifPresent(securityGroup -> groupIdRef.set(securityGroup.groupId())); }).thenApply(v -> { String groupId = groupIdRef.get(); if (groupId == null) { throw new RuntimeException("No security group found with the name: " + groupName); } return groupId; }).exceptionally(ex -> { logger.info("Failed to describe security group: " + ex.getMessage()); throw new RuntimeException("Failed to describe security group", ex); }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeSecurityGroups” 中的。
-
基础知识
以下代码示例展示了如何:
创建密钥对和安全组。
选择 Amazon 系统映像 (AMI) 和兼容的实例类型,然后创建实例。
停止实例,然后再重启。
将弹性 IP 地址与您的实例相关联。
使用连接您的实例SSH,然后清理资源。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 在命令提示符中运行场景。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.services.ec2.model.CreateKeyPairResponse; import software.amazon.awssdk.services.ec2.model.DeleteKeyPairResponse; import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsResponse; import software.amazon.awssdk.services.ec2.model.DisassociateAddressResponse; import software.amazon.awssdk.services.ec2.model.Ec2Exception; import software.amazon.awssdk.services.ec2.model.ReleaseAddressResponse; import software.amazon.awssdk.services.ssm.model.GetParametersByPathResponse; import software.amazon.awssdk.services.ssm.model.Parameter; import java.net.InetAddress; import java.net.UnknownHostException; import java.util.List; import java.util.Scanner; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; /** * 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 * * This Java example performs the following tasks: * * 1. Creates an RSA key pair and saves the private key data as a .pem file. * 2. Lists key pairs. * 3. Creates a security group for the default VPC. * 4. Displays security group information. * 5. Gets a list of Amazon Linux 2 AMIs and selects one. * 6. Gets additional information about the image. * 7. Gets a list of instance types that are compatible with the selected AMI’s * architecture. * 8. Creates an instance with the key pair, security group, AMI, and an * instance type. * 9. Displays information about the instance. * 10. Stops the instance and waits for it to stop. * 11. Starts the instance and waits for it to start. * 12. Allocates an Elastic IP address and associates it with the instance. * 13. Displays SSH connection info for the instance. * 14. Disassociates and deletes the Elastic IP address. * 15. Terminates the instance and waits for it to terminate. * 16. Deletes the security group. * 17. Deletes the key pair. */ public class EC2Scenario { public static final String DASHES = new String(new char[80]).replace("\0", "-"); private static final Logger logger = LoggerFactory.getLogger(EC2Scenario.class); public static void main(String[] args) throws InterruptedException, UnknownHostException { logger.info(""" Usage: <keyName> <fileName> <groupName> <groupDesc> Where: keyName - A key pair name (for example, TestKeyPair).\s fileName - A file name where the key information is written to.\s groupName - The name of the security group.\s groupDesc - The description of the security group.\s """); Scanner scanner = new Scanner(System.in); EC2Actions ec2Actions = new EC2Actions(); String keyName = "TestKeyPair7" ; String fileName = "ec2Key.pem"; String groupName = "TestSecGroup7" ; String groupDesc = "Test Group" ; String vpcId = ec2Actions.describeFirstEC2VpcAsync().join().vpcId(); InetAddress localAddress = InetAddress.getLocalHost(); String myIpAddress = localAddress.getHostAddress(); logger.info(""" Amazon Elastic Compute Cloud (EC2) is a web service that provides secure, resizable compute capacity in the cloud. It allows developers and organizations to easily launch and manage virtual server instances, known as EC2 instances, to run their applications. EC2 provides a wide range of instance types, each with different compute, memory, and storage capabilities, to meet the diverse needs of various workloads. Developers can choose the appropriate instance type based on their application's requirements, such as high-performance computing, memory-intensive tasks, or GPU-accelerated workloads. The `Ec2AsyncClient` interface in the AWS SDK for Java 2.x provides a set of methods to programmatically interact with the Amazon EC2 service. This allows developers to automate the provisioning, management, and monitoring of EC2 instances as part of their application deployment pipelines. With EC2, teams can focus on building and deploying their applications without having to worry about the underlying infrastructure required to host and manage physical servers. This scenario walks you through how to perform key operations for this service. Let's get started... """); waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("1. Create an RSA key pair and save the private key material as a .pem file."); logger.info(""" An RSA key pair for Amazon EC2 is a security mechanism used to authenticate and secure access to your EC2 instances. It consists of a public key and a private key, which are generated as a pair. """); waitForInputToContinue(scanner); try { CompletableFuture<CreateKeyPairResponse> future = ec2Actions.createKeyPairAsync(keyName, fileName); CreateKeyPairResponse response = future.join(); logger.info("Key Pair successfully created. Key Fingerprint: " + response.keyFingerprint()); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { if (ec2Ex.getMessage().contains("already exists")) { // Key pair already exists. logger.info("The key pair '" + keyName + "' already exists. Moving on..."); } else { logger.info("EC2 error occurred: Error message: {}, Error code {}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } } else { logger.info("An unexpected error occurred: " + (rt.getMessage())); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("2. List key pairs."); waitForInputToContinue(scanner); try { CompletableFuture<DescribeKeyPairsResponse> future = ec2Actions.describeKeysAsync(); DescribeKeyPairsResponse keyPairsResponse = future.join(); keyPairsResponse.keyPairs().forEach(keyPair -> logger.info( "Found key pair with name {} and fingerprint {}", keyPair.keyName(), keyPair.keyFingerprint())); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Error message: {}, Error code {}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", (cause != null ? cause.getMessage() : rt.getMessage())); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("3. Create a security group."); logger.info(""" An AWS EC2 Security Group is a virtual firewall that controls the inbound and outbound traffic to an EC2 instance. It acts as a first line of defense for your EC2 instances, allowing you to specify the rules that govern the network traffic entering and leaving your instances. """); waitForInputToContinue(scanner); String groupId = ""; try { CompletableFuture<String> future = ec2Actions.createSecurityGroupAsync(groupName, groupDesc, vpcId, myIpAddress); future.join(); logger.info("Created security group") ; } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { if (ec2Ex.awsErrorDetails().errorMessage().contains("already exists")) { logger.info("The Security Group already exists. Moving on..."); } else { logger.error("An unexpected error occurred: {}", ec2Ex.awsErrorDetails().errorMessage()); return; } } else { logger.error("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("4. Display security group information for the new security group."); waitForInputToContinue(scanner); try { CompletableFuture<String> future = ec2Actions.describeSecurityGroupArnByNameAsync(groupName); groupId = future.join(); logger.info("The security group Id is "+groupId); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { String errorCode = ec2Ex.awsErrorDetails().errorCode(); if ("InvalidGroup.NotFound".equals(errorCode)) { logger.info("Security group '{}' does not exist. Error Code: {}", groupName, errorCode); } else { logger.info("EC2 error occurred: Message {}, Error Code: {}", ec2Ex.getMessage(), errorCode); } } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("5. Get a list of Amazon Linux 2 AMIs and select one with amzn2 in the name."); logger.info(""" An Amazon EC2 AMI (Amazon Machine Image) is a pre-configured virtual machine image that serves as a template for launching EC2 instances. It contains all the necessary software and configurations required to run an application or operating system on an EC2 instance. """); waitForInputToContinue(scanner); String instanceAMI=""; try { CompletableFuture<GetParametersByPathResponse> future = ec2Actions.getParaValuesAsync(); GetParametersByPathResponse pathResponse = future.join(); List<Parameter> parameterList = pathResponse.parameters(); for (Parameter para : parameterList) { if (filterName(para.name())) { instanceAMI = para.value(); break; } } } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } logger.info("The AMI value with amzn2 is: {}", instanceAMI); waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("6. Get the (Amazon Machine Image) AMI value from the amzn2 image."); logger.info(""" An AMI value represents a specific version of a virtual machine (VM) or server image. It uniquely identifies a particular version of an EC2 instance, including its operating system, pre-installed software, and any custom configurations. This allows you to consistently deploy the same VM image across your infrastructure. """); waitForInputToContinue(scanner); String amiValue; try { CompletableFuture<String> future = ec2Actions.describeImageAsync(instanceAMI); amiValue = future.join(); } catch (CompletionException ce) { Throwable cause = ce.getCause(); if (cause instanceof Ec2Exception) { Ec2Exception ec2Ex = (Ec2Exception) cause; logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("7. Retrieves an instance type available in the current AWS region."); waitForInputToContinue(scanner); String instanceType; try { CompletableFuture<String> future = ec2Actions.getInstanceTypesAsync(); instanceType = future.join(); if (!instanceType.isEmpty()) { logger.info("Found instance type: " + instanceType); } else { logger.info("Desired instance type not found."); } } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("8. Create an Amazon EC2 instance using the key pair, the instance type, the security group, and the EC2 AMI value."); logger.info("Once the EC2 instance is created, it is placed into a running state."); waitForInputToContinue(scanner); String newInstanceId; try { CompletableFuture<String> future = ec2Actions.runInstanceAsync(instanceType, keyName, groupName, amiValue); newInstanceId = future.join(); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception) { Ec2Exception ec2Ex = (Ec2Exception) cause; switch (ec2Ex.awsErrorDetails().errorCode()) { case "InvalidParameterValue": logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); break; case "InsufficientInstanceCapacity": // Handle insufficient instance capacity. logger.info("Insufficient instance capacity: {}, {}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); break; case "InvalidGroup.NotFound": // Handle security group not found. logger.info("Security group not found: {},{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); break; default: logger.info("EC2 error occurred: {} (Code: {}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); break; } return; } else { logger.info("An unexpected error occurred: {}", (cause != null ? cause.getMessage() : rt.getMessage())); return; } } logger.info("The instance Id is " + newInstanceId); waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("9. Display information about the running instance. "); waitForInputToContinue(scanner); String publicIp; try { CompletableFuture<String> future = ec2Actions.describeEC2InstancesAsync(newInstanceId); publicIp = future.join(); logger.info("EC2 instance public IP {}", publicIp); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } logger.info("You can SSH to the instance using this command:"); logger.info("ssh -i " + fileName + " ec2-user@" + publicIp); waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("10. Stop the instance using a waiter (this may take a few mins)."); // Remove the 2nd one waitForInputToContinue(scanner); try { CompletableFuture<Void> future = ec2Actions.stopInstanceAsync(newInstanceId); future.join(); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("11. Start the instance using a waiter (this may take a few mins)."); try { CompletableFuture<Void> future = ec2Actions.startInstanceAsync(newInstanceId); future.join(); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { // Handle EC2 exceptions. logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("12. Allocate an Elastic IP address and associate it with the instance."); logger.info(""" An Elastic IP address is a static public IP address that you can associate with your EC2 instance. This allows you to have a fixed, predictable IP address that remains the same even if your instance is stopped, terminated, or replaced. This is particularly useful for applications or services that need to be accessed consistently from a known IP address. An EC2 Allocation ID (also known as a Reserved Instance Allocation ID) is a unique identifier associated with a Reserved Instance (RI) that you have purchased in AWS. When you purchase a Reserved Instance, AWS assigns a unique Allocation ID to it. This Allocation ID is used to track and identify the specific RI you have purchased, and it is important for managing and monitoring your Reserved Instances. """); waitForInputToContinue(scanner); String allocationId; try { CompletableFuture<String> future = ec2Actions.allocateAddressAsync(); allocationId = future.join(); logger.info("Successfully allocated address with ID: " +allocationId); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } logger.info("The allocation Id value is " + allocationId); waitForInputToContinue(scanner); String associationId; try { CompletableFuture<String> future = ec2Actions.associateAddressAsync(newInstanceId, allocationId); associationId = future.join(); logger.info("Successfully associated address with ID: " +associationId); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("13. Describe the instance again. Note that the public IP address has changed"); waitForInputToContinue(scanner); try { CompletableFuture<String> future = ec2Actions.describeEC2InstancesAsync(newInstanceId); publicIp = future.join(); logger.info("EC2 instance public IP: " + publicIp); logger.info("You can SSH to the instance using this command:"); logger.info("ssh -i " + fileName + " ec2-user@" + publicIp); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("14. Disassociate and release the Elastic IP address."); waitForInputToContinue(scanner); try { CompletableFuture<DisassociateAddressResponse> future = ec2Actions.disassociateAddressAsync(associationId); future.join(); logger.info("Address successfully disassociated."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { // Handle EC2 exceptions. logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); try { CompletableFuture<ReleaseAddressResponse> future = ec2Actions.releaseEC2AddressAsync(allocationId); future.join(); // Wait for the operation to complete logger.info("Elastic IP address successfully released."); } catch (RuntimeException rte) { logger.info("An unexpected error occurred: {}", rte.getMessage()); return; } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("15. Terminate the instance and use a waiter (this may take a few mins)."); waitForInputToContinue(scanner); try { CompletableFuture<Object> future = ec2Actions.terminateEC2Async(newInstanceId); future.join(); logger.info("EC2 instance successfully terminated."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { // Handle EC2 exceptions. logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } logger.info(DASHES); logger.info(DASHES); logger.info("16. Delete the security group."); waitForInputToContinue(scanner); try { CompletableFuture<Void> future = ec2Actions.deleteEC2SecGroupAsync(groupId); future.join(); logger.info("Security group successfully deleted."); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("17. Delete the key."); waitForInputToContinue(scanner); try { CompletableFuture<DeleteKeyPairResponse> future = ec2Actions.deleteKeysAsync(keyName); future.join(); logger.info("Successfully deleted key pair named " + keyName); } catch (RuntimeException rt) { Throwable cause = rt.getCause(); if (cause instanceof Ec2Exception ec2Ex) { logger.info("EC2 error occurred: Message {}, Error Code:{}", ec2Ex.getMessage(), ec2Ex.awsErrorDetails().errorCode()); return; } else { logger.info("An unexpected error occurred: {}", cause.getMessage()); return; } } waitForInputToContinue(scanner); logger.info(DASHES); logger.info(DASHES); logger.info("You successfully completed the Amazon EC2 scenario."); logger.info(DASHES); } public static boolean filterName(String name) { String[] parts = name.split("/"); String myValue = parts[4]; return myValue.contains("amzn2"); } private static void waitForInputToContinue(Scanner scanner) { while (true) { logger.info(""); logger.info("Enter 'c' followed by <ENTER> to continue:"); String input = scanner.nextLine(); if (input.trim().equalsIgnoreCase("c")) { logger.info("Continuing with the program..."); logger.info(""); break; } else { // Handle invalid input. logger.info("Invalid input. Please try again."); } } } }
定义一个封装EC2动作的类。
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; import software.amazon.awssdk.http.async.SdkAsyncHttpClient; import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient; import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.AllocateAddressRequest; import software.amazon.awssdk.services.ec2.model.AllocateAddressResponse; import software.amazon.awssdk.services.ec2.model.AssociateAddressRequest; import software.amazon.awssdk.services.ec2.model.AssociateAddressResponse; import software.amazon.awssdk.services.ec2.model.AuthorizeSecurityGroupIngressRequest; import software.amazon.awssdk.services.ec2.model.CreateKeyPairRequest; import software.amazon.awssdk.services.ec2.model.CreateKeyPairResponse; import software.amazon.awssdk.services.ec2.model.CreateSecurityGroupRequest; import software.amazon.awssdk.services.ec2.model.DeleteKeyPairRequest; import software.amazon.awssdk.services.ec2.model.DeleteKeyPairResponse; import software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupRequest; import software.amazon.awssdk.services.ec2.model.DeleteSecurityGroupResponse; import software.amazon.awssdk.services.ec2.model.DescribeImagesRequest; import software.amazon.awssdk.services.ec2.model.DescribeInstanceTypesRequest; import software.amazon.awssdk.services.ec2.model.DescribeInstanceTypesResponse; import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest; import software.amazon.awssdk.services.ec2.model.DescribeKeyPairsResponse; import software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsRequest; import software.amazon.awssdk.services.ec2.model.DescribeSecurityGroupsResponse; import software.amazon.awssdk.services.ec2.model.DescribeVpcsRequest; import software.amazon.awssdk.services.ec2.model.DisassociateAddressRequest; import software.amazon.awssdk.services.ec2.model.DisassociateAddressResponse; import software.amazon.awssdk.services.ec2.model.DomainType; import software.amazon.awssdk.services.ec2.model.Ec2Exception; import software.amazon.awssdk.services.ec2.model.Filter; import software.amazon.awssdk.services.ec2.model.InstanceTypeInfo; import software.amazon.awssdk.services.ec2.model.IpPermission; import software.amazon.awssdk.services.ec2.model.IpRange; import software.amazon.awssdk.services.ec2.model.ReleaseAddressRequest; import software.amazon.awssdk.services.ec2.model.ReleaseAddressResponse; import software.amazon.awssdk.services.ec2.model.RunInstancesRequest; import software.amazon.awssdk.services.ec2.model.RunInstancesResponse; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.TerminateInstancesRequest; import software.amazon.awssdk.services.ec2.model.Vpc; import software.amazon.awssdk.services.ec2.paginators.DescribeImagesPublisher; import software.amazon.awssdk.services.ec2.paginators.DescribeInstancesPublisher; import software.amazon.awssdk.services.ec2.paginators.DescribeSecurityGroupsPublisher; import software.amazon.awssdk.services.ec2.paginators.DescribeVpcsPublisher; import software.amazon.awssdk.services.ec2.waiters.Ec2AsyncWaiter; import software.amazon.awssdk.services.ssm.SsmAsyncClient; import software.amazon.awssdk.services.ssm.model.GetParametersByPathRequest; import software.amazon.awssdk.services.ssm.model.GetParametersByPathResponse; import software.amazon.awssdk.services.ec2.model.TerminateInstancesResponse; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.time.Duration; import java.util.List; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.atomic.AtomicReference; public class EC2Actions { private static final Logger logger = LoggerFactory.getLogger(EC2Actions.class); private static Ec2AsyncClient ec2AsyncClient; /** * Retrieves an asynchronous Amazon Elastic Container Registry (ECR) client. * * @return the configured ECR asynchronous client. */ private static Ec2AsyncClient getAsyncClient() { if (ec2AsyncClient == null) { /* The `NettyNioAsyncHttpClient` class is part of the AWS SDK for Java, version 2, and it is designed to provide a high-performance, asynchronous HTTP client for interacting with AWS services. It uses the Netty framework to handle the underlying network communication and the Java NIO API to provide a non-blocking, event-driven approach to HTTP requests and responses. */ SdkAsyncHttpClient httpClient = NettyNioAsyncHttpClient.builder() .maxConcurrency(50) // Adjust as needed. .connectionTimeout(Duration.ofSeconds(60)) // Set the connection timeout. .readTimeout(Duration.ofSeconds(60)) // Set the read timeout. .writeTimeout(Duration.ofSeconds(60)) // Set the write timeout. .build(); ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder() .apiCallTimeout(Duration.ofMinutes(2)) // Set the overall API call timeout. .apiCallAttemptTimeout(Duration.ofSeconds(90)) // Set the individual call attempt timeout. .build(); ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .httpClient(httpClient) .overrideConfiguration(overrideConfig) .build(); } return ec2AsyncClient; } /** * Deletes a key pair asynchronously. * * @param keyPair the name of the key pair to delete * @return a {@link CompletableFuture} that represents the result of the asynchronous operation. * The {@link CompletableFuture} will complete with a {@link DeleteKeyPairResponse} object * that provides the result of the key pair deletion operation. */ public CompletableFuture<DeleteKeyPairResponse> deleteKeysAsync(String keyPair) { DeleteKeyPairRequest request = DeleteKeyPairRequest.builder() .keyName(keyPair) .build(); // Initiate the asynchronous request to delete the key pair. CompletableFuture<DeleteKeyPairResponse> response = getAsyncClient().deleteKeyPair(request); return response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to delete key pair: " + keyPair, ex); } else if (resp == null) { throw new RuntimeException("No response received for deleting key pair: " + keyPair); } }); } /** * Deletes an EC2 security group asynchronously. * * @param groupId the ID of the security group to delete * @return a CompletableFuture that completes when the security group is deleted */ public CompletableFuture<Void> deleteEC2SecGroupAsync(String groupId) { DeleteSecurityGroupRequest request = DeleteSecurityGroupRequest.builder() .groupId(groupId) .build(); CompletableFuture<DeleteSecurityGroupResponse> response = getAsyncClient().deleteSecurityGroup(request); return response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to delete security group with Id " + groupId, ex); } else if (resp == null) { throw new RuntimeException("No response received for deleting security group with Id " + groupId); } }).thenApply(resp -> null); } /** * Terminates an EC2 instance asynchronously and waits for it to reach the terminated state. * * @param instanceId the ID of the EC2 instance to terminate * @return a {@link CompletableFuture} that completes when the instance has been terminated * @throws RuntimeException if there is no response from the AWS SDK or if there is a failure during the termination process */ public CompletableFuture<Object> terminateEC2Async(String instanceId) { TerminateInstancesRequest terminateRequest = TerminateInstancesRequest.builder() .instanceIds(instanceId) .build(); CompletableFuture<TerminateInstancesResponse> responseFuture = getAsyncClient().terminateInstances(terminateRequest); return responseFuture.thenCompose(terminateResponse -> { if (terminateResponse == null) { throw new RuntimeException("No response received for terminating instance " + instanceId); } System.out.println("Going to terminate an EC2 instance and use a waiter to wait for it to be in terminated state"); return getAsyncClient().waiter() .waitUntilInstanceTerminated(r -> r.instanceIds(instanceId)) .thenApply(waiterResponse -> null); }).exceptionally(throwable -> { // Handle any exceptions that occurred during the async call throw new RuntimeException("Failed to terminate EC2 instance: " + throwable.getMessage(), throwable); }); } /** * Releases an Elastic IP address asynchronously. * * @param allocId the allocation ID of the Elastic IP address to be released * @return a {@link CompletableFuture} representing the asynchronous operation of releasing the Elastic IP address */ public CompletableFuture<ReleaseAddressResponse> releaseEC2AddressAsync(String allocId) { ReleaseAddressRequest request = ReleaseAddressRequest.builder() .allocationId(allocId) .build(); CompletableFuture<ReleaseAddressResponse> response = getAsyncClient().releaseAddress(request); response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to release Elastic IP address", ex); } }); return response; } /** * Disassociates an Elastic IP address from an instance asynchronously. * * @param associationId The ID of the association you want to disassociate. * @return a {@link CompletableFuture} representing the asynchronous operation of disassociating the address. The * {@link CompletableFuture} will complete with a {@link DisassociateAddressResponse} when the operation is * finished. * @throws RuntimeException if the disassociation of the address fails. */ public CompletableFuture<DisassociateAddressResponse> disassociateAddressAsync(String associationId) { Ec2AsyncClient ec2 = getAsyncClient(); DisassociateAddressRequest addressRequest = DisassociateAddressRequest.builder() .associationId(associationId) .build(); // Disassociate the address asynchronously. CompletableFuture<DisassociateAddressResponse> response = ec2.disassociateAddress(addressRequest); response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to disassociate address", ex); } }); return response; } /** * Associates an Elastic IP address with an EC2 instance asynchronously. * * @param instanceId the ID of the EC2 instance to associate the Elastic IP address with * @param allocationId the allocation ID of the Elastic IP address to associate * @return a {@link CompletableFuture} that completes with the association ID when the operation is successful, * or throws a {@link RuntimeException} if the operation fails */ public CompletableFuture<String> associateAddressAsync(String instanceId, String allocationId) { AssociateAddressRequest associateRequest = AssociateAddressRequest.builder() .instanceId(instanceId) .allocationId(allocationId) .build(); CompletableFuture<AssociateAddressResponse> responseFuture = getAsyncClient().associateAddress(associateRequest); return responseFuture.thenApply(response -> { if (response.associationId() != null) { return response.associationId(); } else { throw new RuntimeException("Association ID is null after associating address."); } }).whenComplete((result, ex) -> { if (ex != null) { throw new RuntimeException("Failed to associate address", ex); } }); } /** * Allocates an Elastic IP address asynchronously in the VPC domain. * * @return a {@link CompletableFuture} containing the allocation ID of the allocated Elastic IP address */ public CompletableFuture<String> allocateAddressAsync() { AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder() .domain(DomainType.VPC) .build(); CompletableFuture<AllocateAddressResponse> responseFuture = getAsyncClient().allocateAddress(allocateRequest); return responseFuture.thenApply(AllocateAddressResponse::allocationId).whenComplete((result, ex) -> { if (ex != null) { throw new RuntimeException("Failed to allocate address", ex); } }); } /** * Asynchronously describes the state of an EC2 instance. * The paginator helps you iterate over multiple pages of results. * * @param newInstanceId the ID of the EC2 instance to describe * @return a {@link CompletableFuture} that, when completed, contains a string describing the state of the EC2 instance */ public CompletableFuture<String> describeEC2InstancesAsync(String newInstanceId) { DescribeInstancesRequest request = DescribeInstancesRequest.builder() .instanceIds(newInstanceId) .build(); DescribeInstancesPublisher paginator = getAsyncClient().describeInstancesPaginator(request); AtomicReference<String> publicIpAddressRef = new AtomicReference<>(); return paginator.subscribe(response -> { response.reservations().stream() .flatMap(reservation -> reservation.instances().stream()) .filter(instance -> instance.instanceId().equals(newInstanceId)) .findFirst() .ifPresent(instance -> publicIpAddressRef.set(instance.publicIpAddress())); }).thenApply(v -> { String publicIpAddress = publicIpAddressRef.get(); if (publicIpAddress == null) { throw new RuntimeException("Instance with ID " + newInstanceId + " not found."); } return publicIpAddress; }).exceptionally(ex -> { logger.info("Failed to describe instances: " + ex.getMessage()); throw new RuntimeException("Failed to describe instances", ex); }); } /** * Runs an EC2 instance asynchronously. * * @param instanceType The instance type to use for the EC2 instance. * @param keyName The name of the key pair to associate with the EC2 instance. * @param groupName The name of the security group to associate with the EC2 instance. * @param amiId The ID of the Amazon Machine Image (AMI) to use for the EC2 instance. * @return A {@link CompletableFuture} that completes with the ID of the started EC2 instance. * @throws RuntimeException If there is an error running the EC2 instance. */ public CompletableFuture<String> runInstanceAsync(String instanceType, String keyName, String groupName, String amiId) { RunInstancesRequest runRequest = RunInstancesRequest.builder() .instanceType(instanceType) .keyName(keyName) .securityGroups(groupName) .maxCount(1) .minCount(1) .imageId(amiId) .build(); CompletableFuture<RunInstancesResponse> responseFuture = getAsyncClient().runInstances(runRequest); return responseFuture.thenCompose(response -> { String instanceIdVal = response.instances().get(0).instanceId(); System.out.println("Going to start an EC2 instance and use a waiter to wait for it to be in running state"); return getAsyncClient().waiter() .waitUntilInstanceExists(r -> r.instanceIds(instanceIdVal)) .thenCompose(waitResponse -> getAsyncClient().waiter() .waitUntilInstanceRunning(r -> r.instanceIds(instanceIdVal)) .thenApply(runningResponse -> instanceIdVal)); }).exceptionally(throwable -> { // Handle any exceptions that occurred during the async call throw new RuntimeException("Failed to run EC2 instance: " + throwable.getMessage(), throwable); }); } /** * Asynchronously retrieves the instance types available in the current AWS region. * <p> * This method uses the AWS SDK's asynchronous API to fetch the available instance types * and then processes the response. It logs the memory information, network information, * and instance type for each instance type returned. Additionally, it returns a * {@link CompletableFuture} that resolves to the instance type string for the "t2.2xlarge" * instance type, if it is found in the response. If the "t2.2xlarge" instance type is not * found, an empty string is returned. * </p> * * @return a {@link CompletableFuture} that resolves to the instance type string for the * "t2.2xlarge" instance type, or an empty string if the instance type is not found */ public CompletableFuture<String> getInstanceTypesAsync() { DescribeInstanceTypesRequest typesRequest = DescribeInstanceTypesRequest.builder() .maxResults(10) .build(); CompletableFuture<DescribeInstanceTypesResponse> response = getAsyncClient().describeInstanceTypes(typesRequest); response.whenComplete((resp, ex) -> { if (resp != null) { List<InstanceTypeInfo> instanceTypes = resp.instanceTypes(); for (InstanceTypeInfo type : instanceTypes) { logger.info("The memory information of this type is " + type.memoryInfo().sizeInMiB()); logger.info("Network information is " + type.networkInfo().toString()); logger.info("Instance type is " + type.instanceType().toString()); } } else { throw (RuntimeException) ex; } }); return response.thenApply(resp -> { for (InstanceTypeInfo type : resp.instanceTypes()) { String instanceType = type.instanceType().toString(); if (instanceType.equals("t2.2xlarge")) { return instanceType; } } return ""; }); } /** * Asynchronously describes an AWS EC2 image with the specified image ID. * * @param imageId the ID of the image to be described * @return a {@link CompletableFuture} that, when completed, contains the ID of the described image * @throws RuntimeException if no images are found with the provided image ID, or if an error occurs during the AWS API call */ public CompletableFuture<String> describeImageAsync(String imageId) { DescribeImagesRequest imagesRequest = DescribeImagesRequest.builder() .imageIds(imageId) .build(); AtomicReference<String> imageIdRef = new AtomicReference<>(); DescribeImagesPublisher paginator = getAsyncClient().describeImagesPaginator(imagesRequest); return paginator.subscribe(response -> { response.images().stream() .filter(image -> image.imageId().equals(imageId)) .findFirst() .ifPresent(image -> { logger.info("The description of the image is " + image.description()); logger.info("The name of the image is " + image.name()); imageIdRef.set(image.imageId()); }); }).thenApply(v -> { String id = imageIdRef.get(); if (id == null) { throw new RuntimeException("No images found with the provided image ID."); } return id; }).exceptionally(ex -> { logger.info("Failed to describe image: " + ex.getMessage()); throw new RuntimeException("Failed to describe image", ex); }); } /** * Retrieves the parameter values asynchronously using the AWS Systems Manager (SSM) API. * * @return a {@link CompletableFuture} that holds the response from the SSM API call to get parameters by path */ public CompletableFuture<GetParametersByPathResponse> getParaValuesAsync() { SsmAsyncClient ssmClient = SsmAsyncClient.builder() .region(Region.US_EAST_1) .build(); GetParametersByPathRequest parameterRequest = GetParametersByPathRequest.builder() .path("/aws/service/ami-amazon-linux-latest") .build(); // Create a CompletableFuture to hold the final result. CompletableFuture<GetParametersByPathResponse> responseFuture = new CompletableFuture<>(); ssmClient.getParametersByPath(parameterRequest) .whenComplete((response, exception) -> { if (exception != null) { responseFuture.completeExceptionally(new RuntimeException("Failed to get parameters by path", exception)); } else { responseFuture.complete(response); } }); return responseFuture; } /** * Asynchronously describes the security groups for the specified group ID. * * @param groupName the name of the security group to describe * @return a {@link CompletableFuture} that represents the asynchronous operation * of describing the security groups. The future will complete with a * {@link DescribeSecurityGroupsResponse} object that contains the * security group information. */ public CompletableFuture<String> describeSecurityGroupArnByNameAsync(String groupName) { DescribeSecurityGroupsRequest request = DescribeSecurityGroupsRequest.builder() .groupNames(groupName) .build(); DescribeSecurityGroupsPublisher paginator = getAsyncClient().describeSecurityGroupsPaginator(request); AtomicReference<String> groupIdRef = new AtomicReference<>(); return paginator.subscribe(response -> { response.securityGroups().stream() .filter(securityGroup -> securityGroup.groupName().equals(groupName)) .findFirst() .ifPresent(securityGroup -> groupIdRef.set(securityGroup.groupId())); }).thenApply(v -> { String groupId = groupIdRef.get(); if (groupId == null) { throw new RuntimeException("No security group found with the name: " + groupName); } return groupId; }).exceptionally(ex -> { logger.info("Failed to describe security group: " + ex.getMessage()); throw new RuntimeException("Failed to describe security group", ex); }); } /** * Creates a new security group asynchronously with the specified group name, description, and VPC ID. It also * authorizes inbound traffic on ports 80 and 22 from the specified IP address. * * @param groupName the name of the security group to create * @param groupDesc the description of the security group * @param vpcId the ID of the VPC in which to create the security group * @param myIpAddress the IP address from which to allow inbound traffic (e.g., "192.168.1.1/0" to allow traffic from * any IP address in the 192.168.1.0/24 subnet) * @return a CompletableFuture that, when completed, returns the ID of the created security group * @throws RuntimeException if there was a failure creating the security group or authorizing the inbound traffic */ public CompletableFuture<String> createSecurityGroupAsync(String groupName, String groupDesc, String vpcId, String myIpAddress) { CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder() .groupName(groupName) .description(groupDesc) .vpcId(vpcId) .build(); return getAsyncClient().createSecurityGroup(createRequest) .thenCompose(createResponse -> { String groupId = createResponse.groupId(); IpRange ipRange = IpRange.builder() .cidrIp(myIpAddress + "/32") .build(); IpPermission ipPerm = IpPermission.builder() .ipProtocol("tcp") .toPort(80) .fromPort(80) .ipRanges(ipRange) .build(); IpPermission ipPerm2 = IpPermission.builder() .ipProtocol("tcp") .toPort(22) .fromPort(22) .ipRanges(ipRange) .build(); AuthorizeSecurityGroupIngressRequest authRequest = AuthorizeSecurityGroupIngressRequest.builder() .groupName(groupName) .ipPermissions(ipPerm, ipPerm2) .build(); return getAsyncClient().authorizeSecurityGroupIngress(authRequest) .thenApply(authResponse -> groupId); }) .whenComplete((result, exception) -> { if (exception != null) { if (exception instanceof CompletionException && exception.getCause() instanceof Ec2Exception) { throw (Ec2Exception) exception.getCause(); } else { throw new RuntimeException("Failed to create security group: " + exception.getMessage(), exception); } } }); } /** * Asynchronously describes the key pairs associated with the current AWS account. * * @return a {@link CompletableFuture} containing the {@link DescribeKeyPairsResponse} object, which provides * information about the key pairs. */ public CompletableFuture<DescribeKeyPairsResponse> describeKeysAsync() { CompletableFuture<DescribeKeyPairsResponse> responseFuture = getAsyncClient().describeKeyPairs(); responseFuture.whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to describe key pairs: " + exception.getMessage(), exception); } }); return responseFuture; } /** * Creates a new key pair asynchronously. * * @param keyName the name of the key pair to create * @param fileName the name of the file to write the key material to * @return a {@link CompletableFuture} that represents the asynchronous operation * of creating the key pair and writing the key material to a file */ public CompletableFuture<CreateKeyPairResponse> createKeyPairAsync(String keyName, String fileName) { CreateKeyPairRequest request = CreateKeyPairRequest.builder() .keyName(keyName) .build(); CompletableFuture<CreateKeyPairResponse> responseFuture = getAsyncClient().createKeyPair(request); responseFuture.whenComplete((response, exception) -> { if (response != null) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write(response.keyMaterial()); writer.close(); } catch (IOException e) { throw new RuntimeException("Failed to write key material to file: " + e.getMessage(), e); } } else { throw new RuntimeException("Failed to create key pair: " + exception.getMessage(), exception); } }); return responseFuture; } /** * Describes the first default VPC asynchronously and using a paginator. * * @return a {@link CompletableFuture} that, when completed, contains the first default VPC found.\ */ public CompletableFuture<Vpc> describeFirstEC2VpcAsync() { Filter myFilter = Filter.builder() .name("is-default") .values("true") .build(); DescribeVpcsRequest request = DescribeVpcsRequest.builder() .filters(myFilter) .build(); DescribeVpcsPublisher paginator = getAsyncClient().describeVpcsPaginator(request); AtomicReference<Vpc> vpcRef = new AtomicReference<>(); return paginator.subscribe(response -> { response.vpcs().stream() .findFirst() .ifPresent(vpcRef::set); }).thenApply(v -> { Vpc vpc = vpcRef.get(); if (vpc == null) { throw new RuntimeException("Default VPC not found"); } return vpc; }).exceptionally(ex -> { logger.info("Failed to describe VPCs: " + ex.getMessage()); throw new RuntimeException("Failed to describe VPCs", ex); }); } /** * Stops the EC2 instance with the specified ID asynchronously and waits for the instance to stop. * * @param instanceId the ID of the EC2 instance to stop * @return a {@link CompletableFuture} that completes when the instance has been stopped, or exceptionally if an error occurs */ public CompletableFuture<Void> stopInstanceAsync(String instanceId) { StopInstancesRequest stopRequest = StopInstancesRequest.builder() .instanceIds(instanceId) .build(); DescribeInstancesRequest describeRequest = DescribeInstancesRequest.builder() .instanceIds(instanceId) .build(); Ec2AsyncWaiter ec2Waiter = Ec2AsyncWaiter.builder() .client(getAsyncClient()) .build(); CompletableFuture<Void> resultFuture = new CompletableFuture<>(); logger.info("Stopping instance " + instanceId + " and waiting for it to stop."); getAsyncClient().stopInstances(stopRequest) .thenCompose(response -> { if (response.stoppingInstances().isEmpty()) { return CompletableFuture.failedFuture(new RuntimeException("No instances were stopped. Please check the instance ID: " + instanceId)); } return ec2Waiter.waitUntilInstanceStopped(describeRequest); }) .thenAccept(waiterResponse -> { logger.info("Successfully stopped instance " + instanceId); resultFuture.complete(null); }) .exceptionally(throwable -> { logger.error("Failed to stop instance " + instanceId + ": " + throwable.getMessage(), throwable); resultFuture.completeExceptionally(new RuntimeException("Failed to stop instance: " + throwable.getMessage(), throwable)); return null; }); return resultFuture; } /** * Starts an Amazon EC2 instance asynchronously and waits until it is in the "running" state. * * @param instanceId the ID of the instance to start * @return a {@link CompletableFuture} that completes when the instance has been started and is in the "running" state, or exceptionally if an error occurs */ public CompletableFuture<Void> startInstanceAsync(String instanceId) { StartInstancesRequest startRequest = StartInstancesRequest.builder() .instanceIds(instanceId) .build(); Ec2AsyncWaiter ec2Waiter = Ec2AsyncWaiter.builder() .client(getAsyncClient()) .build(); DescribeInstancesRequest describeRequest = DescribeInstancesRequest.builder() .instanceIds(instanceId) .build(); logger.info("Starting instance " + instanceId + " and waiting for it to run."); CompletableFuture<Void> resultFuture = new CompletableFuture<>(); return getAsyncClient().startInstances(startRequest) .thenCompose(response -> ec2Waiter.waitUntilInstanceRunning(describeRequest) ) .thenAccept(waiterResponse -> { logger.info("Successfully started instance " + instanceId); resultFuture.complete(null); }) .exceptionally(throwable -> { resultFuture.completeExceptionally(new RuntimeException("Failed to start instance: " + throwable.getMessage(), throwable)); return null; }); } }
-
有关API详细信息,请参阅 “参AWS SDK for Java 2.x API考” 中的以下主题。
-
操作
以下代码示例显示了如何使用AllocateAddress
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Allocates an Elastic IP address asynchronously in the VPC domain. * * @return a {@link CompletableFuture} containing the allocation ID of the allocated Elastic IP address */ public CompletableFuture<String> allocateAddressAsync() { AllocateAddressRequest allocateRequest = AllocateAddressRequest.builder() .domain(DomainType.VPC) .build(); CompletableFuture<AllocateAddressResponse> responseFuture = getAsyncClient().allocateAddress(allocateRequest); return responseFuture.thenApply(AllocateAddressResponse::allocationId).whenComplete((result, ex) -> { if (ex != null) { throw new RuntimeException("Failed to allocate address", ex); } }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 AllocateAddress” 中的。
-
以下代码示例显示了如何使用AssociateAddress
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Associates an Elastic IP address with an EC2 instance asynchronously. * * @param instanceId the ID of the EC2 instance to associate the Elastic IP address with * @param allocationId the allocation ID of the Elastic IP address to associate * @return a {@link CompletableFuture} that completes with the association ID when the operation is successful, * or throws a {@link RuntimeException} if the operation fails */ public CompletableFuture<String> associateAddressAsync(String instanceId, String allocationId) { AssociateAddressRequest associateRequest = AssociateAddressRequest.builder() .instanceId(instanceId) .allocationId(allocationId) .build(); CompletableFuture<AssociateAddressResponse> responseFuture = getAsyncClient().associateAddress(associateRequest); return responseFuture.thenApply(response -> { if (response.associationId() != null) { return response.associationId(); } else { throw new RuntimeException("Association ID is null after associating address."); } }).whenComplete((result, ex) -> { if (ex != null) { throw new RuntimeException("Failed to associate address", ex); } }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 AssociateAddress” 中的。
-
以下代码示例显示了如何使用AuthorizeSecurityGroupIngress
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Creates a new security group asynchronously with the specified group name, description, and VPC ID. It also * authorizes inbound traffic on ports 80 and 22 from the specified IP address. * * @param groupName the name of the security group to create * @param groupDesc the description of the security group * @param vpcId the ID of the VPC in which to create the security group * @param myIpAddress the IP address from which to allow inbound traffic (e.g., "192.168.1.1/0" to allow traffic from * any IP address in the 192.168.1.0/24 subnet) * @return a CompletableFuture that, when completed, returns the ID of the created security group * @throws RuntimeException if there was a failure creating the security group or authorizing the inbound traffic */ public CompletableFuture<String> createSecurityGroupAsync(String groupName, String groupDesc, String vpcId, String myIpAddress) { CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder() .groupName(groupName) .description(groupDesc) .vpcId(vpcId) .build(); return getAsyncClient().createSecurityGroup(createRequest) .thenCompose(createResponse -> { String groupId = createResponse.groupId(); IpRange ipRange = IpRange.builder() .cidrIp(myIpAddress + "/32") .build(); IpPermission ipPerm = IpPermission.builder() .ipProtocol("tcp") .toPort(80) .fromPort(80) .ipRanges(ipRange) .build(); IpPermission ipPerm2 = IpPermission.builder() .ipProtocol("tcp") .toPort(22) .fromPort(22) .ipRanges(ipRange) .build(); AuthorizeSecurityGroupIngressRequest authRequest = AuthorizeSecurityGroupIngressRequest.builder() .groupName(groupName) .ipPermissions(ipPerm, ipPerm2) .build(); return getAsyncClient().authorizeSecurityGroupIngress(authRequest) .thenApply(authResponse -> groupId); }) .whenComplete((result, exception) -> { if (exception != null) { if (exception instanceof CompletionException && exception.getCause() instanceof Ec2Exception) { throw (Ec2Exception) exception.getCause(); } else { throw new RuntimeException("Failed to create security group: " + exception.getMessage(), exception); } } }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 AuthorizeSecurityGroupIngress” 中的。
-
以下代码示例显示了如何使用CreateKeyPair
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Creates a new key pair asynchronously. * * @param keyName the name of the key pair to create * @param fileName the name of the file to write the key material to * @return a {@link CompletableFuture} that represents the asynchronous operation * of creating the key pair and writing the key material to a file */ public CompletableFuture<CreateKeyPairResponse> createKeyPairAsync(String keyName, String fileName) { CreateKeyPairRequest request = CreateKeyPairRequest.builder() .keyName(keyName) .build(); CompletableFuture<CreateKeyPairResponse> responseFuture = getAsyncClient().createKeyPair(request); responseFuture.whenComplete((response, exception) -> { if (response != null) { try { BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); writer.write(response.keyMaterial()); writer.close(); } catch (IOException e) { throw new RuntimeException("Failed to write key material to file: " + e.getMessage(), e); } } else { throw new RuntimeException("Failed to create key pair: " + exception.getMessage(), exception); } }); return responseFuture; }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateKeyPair” 中的。
-
以下代码示例显示了如何使用CreateSecurityGroup
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Creates a new security group asynchronously with the specified group name, description, and VPC ID. It also * authorizes inbound traffic on ports 80 and 22 from the specified IP address. * * @param groupName the name of the security group to create * @param groupDesc the description of the security group * @param vpcId the ID of the VPC in which to create the security group * @param myIpAddress the IP address from which to allow inbound traffic (e.g., "192.168.1.1/0" to allow traffic from * any IP address in the 192.168.1.0/24 subnet) * @return a CompletableFuture that, when completed, returns the ID of the created security group * @throws RuntimeException if there was a failure creating the security group or authorizing the inbound traffic */ public CompletableFuture<String> createSecurityGroupAsync(String groupName, String groupDesc, String vpcId, String myIpAddress) { CreateSecurityGroupRequest createRequest = CreateSecurityGroupRequest.builder() .groupName(groupName) .description(groupDesc) .vpcId(vpcId) .build(); return getAsyncClient().createSecurityGroup(createRequest) .thenCompose(createResponse -> { String groupId = createResponse.groupId(); IpRange ipRange = IpRange.builder() .cidrIp(myIpAddress + "/32") .build(); IpPermission ipPerm = IpPermission.builder() .ipProtocol("tcp") .toPort(80) .fromPort(80) .ipRanges(ipRange) .build(); IpPermission ipPerm2 = IpPermission.builder() .ipProtocol("tcp") .toPort(22) .fromPort(22) .ipRanges(ipRange) .build(); AuthorizeSecurityGroupIngressRequest authRequest = AuthorizeSecurityGroupIngressRequest.builder() .groupName(groupName) .ipPermissions(ipPerm, ipPerm2) .build(); return getAsyncClient().authorizeSecurityGroupIngress(authRequest) .thenApply(authResponse -> groupId); }) .whenComplete((result, exception) -> { if (exception != null) { if (exception instanceof CompletionException && exception.getCause() instanceof Ec2Exception) { throw (Ec2Exception) exception.getCause(); } else { throw new RuntimeException("Failed to create security group: " + exception.getMessage(), exception); } } }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 CreateSecurityGroup” 中的。
-
以下代码示例显示了如何使用DeleteKeyPair
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Deletes a key pair asynchronously. * * @param keyPair the name of the key pair to delete * @return a {@link CompletableFuture} that represents the result of the asynchronous operation. * The {@link CompletableFuture} will complete with a {@link DeleteKeyPairResponse} object * that provides the result of the key pair deletion operation. */ public CompletableFuture<DeleteKeyPairResponse> deleteKeysAsync(String keyPair) { DeleteKeyPairRequest request = DeleteKeyPairRequest.builder() .keyName(keyPair) .build(); // Initiate the asynchronous request to delete the key pair. CompletableFuture<DeleteKeyPairResponse> response = getAsyncClient().deleteKeyPair(request); return response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to delete key pair: " + keyPair, ex); } else if (resp == null) { throw new RuntimeException("No response received for deleting key pair: " + keyPair); } }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteKeyPair” 中的。
-
以下代码示例显示了如何使用DeleteSecurityGroup
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Deletes an EC2 security group asynchronously. * * @param groupId the ID of the security group to delete * @return a CompletableFuture that completes when the security group is deleted */ public CompletableFuture<Void> deleteEC2SecGroupAsync(String groupId) { DeleteSecurityGroupRequest request = DeleteSecurityGroupRequest.builder() .groupId(groupId) .build(); CompletableFuture<DeleteSecurityGroupResponse> response = getAsyncClient().deleteSecurityGroup(request); return response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to delete security group with Id " + groupId, ex); } else if (resp == null) { throw new RuntimeException("No response received for deleting security group with Id " + groupId); } }).thenApply(resp -> null); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DeleteSecurityGroup” 中的。
-
以下代码示例显示了如何使用DescribeInstanceTypes
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Asynchronously retrieves the instance types available in the current AWS region. * <p> * This method uses the AWS SDK's asynchronous API to fetch the available instance types * and then processes the response. It logs the memory information, network information, * and instance type for each instance type returned. Additionally, it returns a * {@link CompletableFuture} that resolves to the instance type string for the "t2.2xlarge" * instance type, if it is found in the response. If the "t2.2xlarge" instance type is not * found, an empty string is returned. * </p> * * @return a {@link CompletableFuture} that resolves to the instance type string for the * "t2.2xlarge" instance type, or an empty string if the instance type is not found */ public CompletableFuture<String> getInstanceTypesAsync() { DescribeInstanceTypesRequest typesRequest = DescribeInstanceTypesRequest.builder() .maxResults(10) .build(); CompletableFuture<DescribeInstanceTypesResponse> response = getAsyncClient().describeInstanceTypes(typesRequest); response.whenComplete((resp, ex) -> { if (resp != null) { List<InstanceTypeInfo> instanceTypes = resp.instanceTypes(); for (InstanceTypeInfo type : instanceTypes) { logger.info("The memory information of this type is " + type.memoryInfo().sizeInMiB()); logger.info("Network information is " + type.networkInfo().toString()); logger.info("Instance type is " + type.instanceType().toString()); } } else { throw (RuntimeException) ex; } }); return response.thenApply(resp -> { for (InstanceTypeInfo type : resp.instanceTypes()) { String instanceType = type.instanceType().toString(); if (instanceType.equals("t2.2xlarge")) { return instanceType; } } return ""; }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeInstanceTypes” 中的。
-
以下代码示例显示了如何使用DescribeInstances
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Asynchronously describes an AWS EC2 image with the specified image ID. * * @param imageId the ID of the image to be described * @return a {@link CompletableFuture} that, when completed, contains the ID of the described image * @throws RuntimeException if no images are found with the provided image ID, or if an error occurs during the AWS API call */ public CompletableFuture<String> describeImageAsync(String imageId) { DescribeImagesRequest imagesRequest = DescribeImagesRequest.builder() .imageIds(imageId) .build(); AtomicReference<String> imageIdRef = new AtomicReference<>(); DescribeImagesPublisher paginator = getAsyncClient().describeImagesPaginator(imagesRequest); return paginator.subscribe(response -> { response.images().stream() .filter(image -> image.imageId().equals(imageId)) .findFirst() .ifPresent(image -> { logger.info("The description of the image is " + image.description()); logger.info("The name of the image is " + image.name()); imageIdRef.set(image.imageId()); }); }).thenApply(v -> { String id = imageIdRef.get(); if (id == null) { throw new RuntimeException("No images found with the provided image ID."); } return id; }).exceptionally(ex -> { logger.info("Failed to describe image: " + ex.getMessage()); throw new RuntimeException("Failed to describe image", ex); }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeInstances” 中的。
-
以下代码示例显示了如何使用DescribeKeyPairs
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Asynchronously describes the key pairs associated with the current AWS account. * * @return a {@link CompletableFuture} containing the {@link DescribeKeyPairsResponse} object, which provides * information about the key pairs. */ public CompletableFuture<DescribeKeyPairsResponse> describeKeysAsync() { CompletableFuture<DescribeKeyPairsResponse> responseFuture = getAsyncClient().describeKeyPairs(); responseFuture.whenComplete((response, exception) -> { if (exception != null) { throw new RuntimeException("Failed to describe key pairs: " + exception.getMessage(), exception); } }); return responseFuture; }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeKeyPairs” 中的。
-
以下代码示例显示了如何使用DescribeSecurityGroups
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Asynchronously describes the security groups for the specified group ID. * * @param groupName the name of the security group to describe * @return a {@link CompletableFuture} that represents the asynchronous operation * of describing the security groups. The future will complete with a * {@link DescribeSecurityGroupsResponse} object that contains the * security group information. */ public CompletableFuture<String> describeSecurityGroupArnByNameAsync(String groupName) { DescribeSecurityGroupsRequest request = DescribeSecurityGroupsRequest.builder() .groupNames(groupName) .build(); DescribeSecurityGroupsPublisher paginator = getAsyncClient().describeSecurityGroupsPaginator(request); AtomicReference<String> groupIdRef = new AtomicReference<>(); return paginator.subscribe(response -> { response.securityGroups().stream() .filter(securityGroup -> securityGroup.groupName().equals(groupName)) .findFirst() .ifPresent(securityGroup -> groupIdRef.set(securityGroup.groupId())); }).thenApply(v -> { String groupId = groupIdRef.get(); if (groupId == null) { throw new RuntimeException("No security group found with the name: " + groupName); } return groupId; }).exceptionally(ex -> { logger.info("Failed to describe security group: " + ex.getMessage()); throw new RuntimeException("Failed to describe security group", ex); }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DescribeSecurityGroups” 中的。
-
以下代码示例显示了如何使用DisassociateAddress
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Disassociates an Elastic IP address from an instance asynchronously. * * @param associationId The ID of the association you want to disassociate. * @return a {@link CompletableFuture} representing the asynchronous operation of disassociating the address. The * {@link CompletableFuture} will complete with a {@link DisassociateAddressResponse} when the operation is * finished. * @throws RuntimeException if the disassociation of the address fails. */ public CompletableFuture<DisassociateAddressResponse> disassociateAddressAsync(String associationId) { Ec2AsyncClient ec2 = getAsyncClient(); DisassociateAddressRequest addressRequest = DisassociateAddressRequest.builder() .associationId(associationId) .build(); // Disassociate the address asynchronously. CompletableFuture<DisassociateAddressResponse> response = ec2.disassociateAddress(addressRequest); response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to disassociate address", ex); } }); return response; }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 DisassociateAddress” 中的。
-
以下代码示例显示了如何使用GetPasswordData
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2AsyncClient; import software.amazon.awssdk.services.ec2.model.*; 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 GetPasswordData { public static void main(String[] args) { final String usage = """ Usage: <instanceId> Where: instanceId - An instance id value that you can obtain from the AWS Management Console.\s """; if (args.length != 1) { System.out.println(usage); System.exit(1); } String instanceId = args[0]; Ec2AsyncClient ec2AsyncClient = Ec2AsyncClient.builder() .region(Region.US_EAST_1) .build(); try { CompletableFuture<Void> future = getPasswordDataAsync(ec2AsyncClient, instanceId); future.join(); } catch (RuntimeException rte) { System.err.println("An exception occurred: " + (rte.getCause() != null ? rte.getCause().getMessage() : rte.getMessage())); } } /** * Fetches the password data for the specified EC2 instance asynchronously. * * @param ec2AsyncClient the EC2 asynchronous client to use for the request * @param instanceId instanceId the ID of the EC2 instance for which you want to fetch the password data * @return a {@link CompletableFuture} that completes when the password data has been fetched * @throws RuntimeException if there was a failure in fetching the password data */ public static CompletableFuture<Void> getPasswordDataAsync(Ec2AsyncClient ec2AsyncClient, String instanceId) { GetPasswordDataRequest getPasswordDataRequest = GetPasswordDataRequest.builder() .instanceId(instanceId) .build(); CompletableFuture<GetPasswordDataResponse> response = ec2AsyncClient.getPasswordData(getPasswordDataRequest); response.whenComplete((getPasswordDataResponse, ex) -> { if (ex != null) { throw new RuntimeException("Failed to get password data for instance: " + instanceId, ex); } else if (getPasswordDataResponse == null || getPasswordDataResponse.passwordData().isEmpty()) { throw new RuntimeException("No password data found for instance: " + instanceId); } else { String encryptedPasswordData = getPasswordDataResponse.passwordData(); System.out.println("Encrypted Password Data: " + encryptedPasswordData); } }); return response.thenApply(resp -> null); } }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 GetPasswordData” 中的。
-
以下代码示例显示了如何使用ReleaseAddress
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Releases an Elastic IP address asynchronously. * * @param allocId the allocation ID of the Elastic IP address to be released * @return a {@link CompletableFuture} representing the asynchronous operation of releasing the Elastic IP address */ public CompletableFuture<ReleaseAddressResponse> releaseEC2AddressAsync(String allocId) { ReleaseAddressRequest request = ReleaseAddressRequest.builder() .allocationId(allocId) .build(); CompletableFuture<ReleaseAddressResponse> response = getAsyncClient().releaseAddress(request); response.whenComplete((resp, ex) -> { if (ex != null) { throw new RuntimeException("Failed to release Elastic IP address", ex); } }); return response; }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 ReleaseAddress” 中的。
-
以下代码示例显示了如何使用RunInstances
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Runs an EC2 instance asynchronously. * * @param instanceType The instance type to use for the EC2 instance. * @param keyName The name of the key pair to associate with the EC2 instance. * @param groupName The name of the security group to associate with the EC2 instance. * @param amiId The ID of the Amazon Machine Image (AMI) to use for the EC2 instance. * @return A {@link CompletableFuture} that completes with the ID of the started EC2 instance. * @throws RuntimeException If there is an error running the EC2 instance. */ public CompletableFuture<String> runInstanceAsync(String instanceType, String keyName, String groupName, String amiId) { RunInstancesRequest runRequest = RunInstancesRequest.builder() .instanceType(instanceType) .keyName(keyName) .securityGroups(groupName) .maxCount(1) .minCount(1) .imageId(amiId) .build(); CompletableFuture<RunInstancesResponse> responseFuture = getAsyncClient().runInstances(runRequest); return responseFuture.thenCompose(response -> { String instanceIdVal = response.instances().get(0).instanceId(); System.out.println("Going to start an EC2 instance and use a waiter to wait for it to be in running state"); return getAsyncClient().waiter() .waitUntilInstanceExists(r -> r.instanceIds(instanceIdVal)) .thenCompose(waitResponse -> getAsyncClient().waiter() .waitUntilInstanceRunning(r -> r.instanceIds(instanceIdVal)) .thenApply(runningResponse -> instanceIdVal)); }).exceptionally(throwable -> { // Handle any exceptions that occurred during the async call throw new RuntimeException("Failed to run EC2 instance: " + throwable.getMessage(), throwable); }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 RunInstances” 中的。
-
以下代码示例显示了如何使用StartInstances
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Starts an Amazon EC2 instance asynchronously and waits until it is in the "running" state. * * @param instanceId the ID of the instance to start * @return a {@link CompletableFuture} that completes when the instance has been started and is in the "running" state, or exceptionally if an error occurs */ public CompletableFuture<Void> startInstanceAsync(String instanceId) { StartInstancesRequest startRequest = StartInstancesRequest.builder() .instanceIds(instanceId) .build(); Ec2AsyncWaiter ec2Waiter = Ec2AsyncWaiter.builder() .client(getAsyncClient()) .build(); DescribeInstancesRequest describeRequest = DescribeInstancesRequest.builder() .instanceIds(instanceId) .build(); logger.info("Starting instance " + instanceId + " and waiting for it to run."); CompletableFuture<Void> resultFuture = new CompletableFuture<>(); return getAsyncClient().startInstances(startRequest) .thenCompose(response -> ec2Waiter.waitUntilInstanceRunning(describeRequest) ) .thenAccept(waiterResponse -> { logger.info("Successfully started instance " + instanceId); resultFuture.complete(null); }) .exceptionally(throwable -> { resultFuture.completeExceptionally(new RuntimeException("Failed to start instance: " + throwable.getMessage(), throwable)); return null; }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 StartInstances” 中的。
-
以下代码示例显示了如何使用StopInstances
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Stops the EC2 instance with the specified ID asynchronously and waits for the instance to stop. * * @param instanceId the ID of the EC2 instance to stop * @return a {@link CompletableFuture} that completes when the instance has been stopped, or exceptionally if an error occurs */ public CompletableFuture<Void> stopInstanceAsync(String instanceId) { StopInstancesRequest stopRequest = StopInstancesRequest.builder() .instanceIds(instanceId) .build(); DescribeInstancesRequest describeRequest = DescribeInstancesRequest.builder() .instanceIds(instanceId) .build(); Ec2AsyncWaiter ec2Waiter = Ec2AsyncWaiter.builder() .client(getAsyncClient()) .build(); CompletableFuture<Void> resultFuture = new CompletableFuture<>(); logger.info("Stopping instance " + instanceId + " and waiting for it to stop."); getAsyncClient().stopInstances(stopRequest) .thenCompose(response -> { if (response.stoppingInstances().isEmpty()) { return CompletableFuture.failedFuture(new RuntimeException("No instances were stopped. Please check the instance ID: " + instanceId)); } return ec2Waiter.waitUntilInstanceStopped(describeRequest); }) .thenAccept(waiterResponse -> { logger.info("Successfully stopped instance " + instanceId); resultFuture.complete(null); }) .exceptionally(throwable -> { logger.error("Failed to stop instance " + instanceId + ": " + throwable.getMessage(), throwable); resultFuture.completeExceptionally(new RuntimeException("Failed to stop instance: " + throwable.getMessage(), throwable)); return null; }); return resultFuture; }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 StopInstances” 中的。
-
以下代码示例显示了如何使用TerminateInstances
。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 /** * Terminates an EC2 instance asynchronously and waits for it to reach the terminated state. * * @param instanceId the ID of the EC2 instance to terminate * @return a {@link CompletableFuture} that completes when the instance has been terminated * @throws RuntimeException if there is no response from the AWS SDK or if there is a failure during the termination process */ public CompletableFuture<Object> terminateEC2Async(String instanceId) { TerminateInstancesRequest terminateRequest = TerminateInstancesRequest.builder() .instanceIds(instanceId) .build(); CompletableFuture<TerminateInstancesResponse> responseFuture = getAsyncClient().terminateInstances(terminateRequest); return responseFuture.thenCompose(terminateResponse -> { if (terminateResponse == null) { throw new RuntimeException("No response received for terminating instance " + instanceId); } System.out.println("Going to terminate an EC2 instance and use a waiter to wait for it to be in terminated state"); return getAsyncClient().waiter() .waitUntilInstanceTerminated(r -> r.instanceIds(instanceId)) .thenApply(waiterResponse -> null); }).exceptionally(throwable -> { // Handle any exceptions that occurred during the async call throw new RuntimeException("Failed to terminate EC2 instance: " + throwable.getMessage(), throwable); }); }
-
有关API详细信息,请参阅 “AWS SDK for Java 2.x API参考 TerminateInstances” 中的。
-
场景
以下代码示例演示了如何创建可返回书籍、电影和歌曲推荐的负载均衡的 Web 服务。该示例演示服务如何响应故障,以及如何重组服务以提高故障发生时的弹性。
使用 Amazon A EC2 uto Scaling 组根据启动模板创建亚马逊弹性计算云 (AmazonEC2) 实例,并将实例数量保持在指定范围内。
使用 Elastic Load Balancing 处理和分发HTTP请求。
监控自动扩缩组中实例的运行状况,并仅将请求转发到运行状况良好的实例。
在每个EC2实例上运行 Python 网络服务器来处理HTTP请求。Web 服务器以建议和运行状况检查作为响应。
使用 Amazon DynamoDB 表模拟推荐服务。
通过更新 AWS Systems Manager 参数来控制 Web 服务器对请求和运行状况检查的响应。
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整示例,学习如何在 AWS 代码示例存储库
中进行设置和运行。 在命令提示符中运行交互式场景。
public class Main { public static final String fileName = "C:\\AWS\\resworkflow\\recommendations.json"; // Modify file location. public static final String tableName = "doc-example-recommendation-service"; public static final String startScript = "C:\\AWS\\resworkflow\\server_startup_script.sh"; // Modify file location. public static final String policyFile = "C:\\AWS\\resworkflow\\instance_policy.json"; // Modify file location. public static final String ssmJSON = "C:\\AWS\\resworkflow\\ssm_only_policy.json"; // Modify file location. public static final String failureResponse = "doc-example-resilient-architecture-failure-response"; public static final String healthCheck = "doc-example-resilient-architecture-health-check"; public static final String templateName = "doc-example-resilience-template"; public static final String roleName = "doc-example-resilience-role"; public static final String policyName = "doc-example-resilience-pol"; public static final String profileName = "doc-example-resilience-prof"; public static final String badCredsProfileName = "doc-example-resilience-prof-bc"; public static final String targetGroupName = "doc-example-resilience-tg"; public static final String autoScalingGroupName = "doc-example-resilience-group"; public static final String lbName = "doc-example-resilience-lb"; public static final String protocol = "HTTP"; public static final int port = 80; public static final String DASHES = new String(new char[80]).replace("\0", "-"); public static void main(String[] args) throws IOException, InterruptedException { Scanner in = new Scanner(System.in); Database database = new Database(); AutoScaler autoScaler = new AutoScaler(); LoadBalancer loadBalancer = new LoadBalancer(); System.out.println(DASHES); System.out.println("Welcome to the demonstration of How to Build and Manage a Resilient Service!"); System.out.println(DASHES); System.out.println(DASHES); System.out.println("A - SETUP THE RESOURCES"); System.out.println("Press Enter when you're ready to start deploying resources."); in.nextLine(); deploy(loadBalancer); System.out.println(DASHES); System.out.println(DASHES); System.out.println("B - DEMO THE RESILIENCE FUNCTIONALITY"); System.out.println("Press Enter when you're ready."); in.nextLine(); demo(loadBalancer); System.out.println(DASHES); System.out.println(DASHES); System.out.println("C - DELETE THE RESOURCES"); System.out.println(""" This concludes the demo of how to build and manage a resilient service. To keep things tidy and to avoid unwanted charges on your account, we can clean up all AWS resources that were created for this demo. """); System.out.println("\n Do you want to delete the resources (y/n)? "); String userInput = in.nextLine().trim().toLowerCase(); // Capture user input if (userInput.equals("y")) { // Delete resources here deleteResources(loadBalancer, autoScaler, database); System.out.println("Resources deleted."); } else { System.out.println(""" Okay, we'll leave the resources intact. Don't forget to delete them when you're done with them or you might incur unexpected charges. """); } System.out.println(DASHES); System.out.println(DASHES); System.out.println("The example has completed. "); System.out.println("\n Thanks for watching!"); System.out.println(DASHES); } // Deletes the AWS resources used in this example. private static void deleteResources(LoadBalancer loadBalancer, AutoScaler autoScaler, Database database) throws IOException, InterruptedException { loadBalancer.deleteLoadBalancer(lbName); System.out.println("*** Wait 30 secs for resource to be deleted"); TimeUnit.SECONDS.sleep(30); loadBalancer.deleteTargetGroup(targetGroupName); autoScaler.deleteAutoScaleGroup(autoScalingGroupName); autoScaler.deleteRolesPolicies(policyName, roleName, profileName); autoScaler.deleteTemplate(templateName); database.deleteTable(tableName); } private static void deploy(LoadBalancer loadBalancer) throws InterruptedException, IOException { Scanner in = new Scanner(System.in); System.out.println( """ For this demo, we'll use the AWS SDK for Java (v2) to create several AWS resources to set up a load-balanced web service endpoint and explore some ways to make it resilient against various kinds of failures. Some of the resources create by this demo are: \t* A DynamoDB table that the web service depends on to provide book, movie, and song recommendations. \t* An EC2 launch template that defines EC2 instances that each contain a Python web server. \t* An EC2 Auto Scaling group that manages EC2 instances across several Availability Zones. \t* An Elastic Load Balancing (ELB) load balancer that targets the Auto Scaling group to distribute requests. """); System.out.println("Press Enter when you're ready."); in.nextLine(); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Creating and populating a DynamoDB table named " + tableName); Database database = new Database(); database.createTable(tableName, fileName); System.out.println(DASHES); System.out.println(DASHES); System.out.println(""" Creating an EC2 launch template that runs '{startup_script}' when an instance starts. This script starts a Python web server defined in the `server.py` script. The web server listens to HTTP requests on port 80 and responds to requests to '/' and to '/healthcheck'. For demo purposes, this server is run as the root user. In production, the best practice is to run a web server, such as Apache, with least-privileged credentials. The template also defines an IAM policy that each instance uses to assume a role that grants permissions to access the DynamoDB recommendation table and Systems Manager parameters that control the flow of the demo. """); LaunchTemplateCreator templateCreator = new LaunchTemplateCreator(); templateCreator.createTemplate(policyFile, policyName, profileName, startScript, templateName, roleName); System.out.println(DASHES); System.out.println(DASHES); System.out.println( "Creating an EC2 Auto Scaling group that maintains three EC2 instances, each in a different Availability Zone."); System.out.println("*** Wait 30 secs for the VPC to be created"); TimeUnit.SECONDS.sleep(30); AutoScaler autoScaler = new AutoScaler(); String[] zones = autoScaler.createGroup(3, templateName, autoScalingGroupName); System.out.println(""" At this point, you have EC2 instances created. Once each instance starts, it listens for HTTP requests. You can see these instances in the console or continue with the demo. Press Enter when you're ready to continue. """); in.nextLine(); System.out.println(DASHES); System.out.println(DASHES); System.out.println("Creating variables that control the flow of the demo."); ParameterHelper paramHelper = new ParameterHelper(); paramHelper.reset(); System.out.println(DASHES); System.out.println(DASHES); System.out.println(""" Creating an Elastic Load Balancing target group and load balancer. The target group defines how the load balancer connects to instances. The load balancer provides a single endpoint where clients connect and dispatches requests to instances in the group. """); String vpcId = autoScaler.getDefaultVPC(); List<Subnet> subnets = autoScaler.getSubnets(vpcId, zones); System.out.println("You have retrieved a list with " + subnets.size() + " subnets"); String targetGroupArn = loadBalancer.createTargetGroup(protocol, port, vpcId, targetGroupName); String elbDnsName = loadBalancer.createLoadBalancer(subnets, targetGroupArn, lbName, port, protocol); autoScaler.attachLoadBalancerTargetGroup(autoScalingGroupName, targetGroupArn); System.out.println("Verifying access to the load balancer endpoint..."); boolean wasSuccessul = loadBalancer.verifyLoadBalancerEndpoint(elbDnsName); if (!wasSuccessul) { System.out.println("Couldn't connect to the load balancer, verifying that the port is open..."); CloseableHttpClient httpClient = HttpClients.createDefault(); // Create an HTTP GET request to "http://checkip.amazonaws.com" HttpGet httpGet = new HttpGet("http://checkip.amazonaws.com"); try { // Execute the request and get the response HttpResponse response = httpClient.execute(httpGet); // Read the response content. String ipAddress = IOUtils.toString(response.getEntity().getContent(), StandardCharsets.UTF_8).trim(); // Print the public IP address. System.out.println("Public IP Address: " + ipAddress); GroupInfo groupInfo = autoScaler.verifyInboundPort(vpcId, port, ipAddress); if (!groupInfo.isPortOpen()) { System.out.println(""" For this example to work, the default security group for your default VPC must allow access from this computer. You can either add it automatically from this example or add it yourself using the AWS Management Console. """); System.out.println( "Do you want to add a rule to security group " + groupInfo.getGroupName() + " to allow"); System.out.println("inbound traffic on port " + port + " from your computer's IP address (y/n) "); String ans = in.nextLine(); if ("y".equalsIgnoreCase(ans)) { autoScaler.openInboundPort(groupInfo.getGroupName(), String.valueOf(port), ipAddress); System.out.println("Security group rule added."); } else { System.out.println("No security group rule added."); } } } catch (AutoScalingException e) { e.printStackTrace(); } } else if (wasSuccessul) { System.out.println("Your load balancer is ready. You can access it by browsing to:"); System.out.println("\t http://" + elbDnsName); } else { System.out.println("Couldn't get a successful response from the load balancer endpoint. Troubleshoot by"); System.out.println("manually verifying that your VPC and security group are configured correctly and that"); System.out.println("you can successfully make a GET request to the load balancer."); } System.out.println("Press Enter when you're ready to continue with the demo."); in.nextLine(); } // A method that controls the demo part of the Java program. public static void demo(LoadBalancer loadBalancer) throws IOException, InterruptedException { ParameterHelper paramHelper = new ParameterHelper(); System.out.println("Read the ssm_only_policy.json file"); String ssmOnlyPolicy = readFileAsString(ssmJSON); System.out.println("Resetting parameters to starting values for demo."); paramHelper.reset(); System.out.println( """ This part of the demonstration shows how to toggle different parts of the system to create situations where the web service fails, and shows how using a resilient architecture can keep the web service running in spite of these failures. At the start, the load balancer endpoint returns recommendations and reports that all targets are healthy. """); demoChoices(loadBalancer); System.out.println( """ The web service running on the EC2 instances gets recommendations by querying a DynamoDB table. The table name is contained in a Systems Manager parameter named self.param_helper.table. To simulate a failure of the recommendation service, let's set this parameter to name a non-existent table. """); paramHelper.put(paramHelper.tableName, "this-is-not-a-table"); System.out.println( """ \nNow, sending a GET request to the load balancer endpoint returns a failure code. But, the service reports as healthy to the load balancer because shallow health checks don't check for failure of the recommendation service. """); demoChoices(loadBalancer); System.out.println( """ Instead of failing when the recommendation service fails, the web service can return a static response. While this is not a perfect solution, it presents the customer with a somewhat better experience than failure. """); paramHelper.put(paramHelper.failureResponse, "static"); System.out.println(""" Now, sending a GET request to the load balancer endpoint returns a static response. The service still reports as healthy because health checks are still shallow. """); demoChoices(loadBalancer); System.out.println("Let's reinstate the recommendation service."); paramHelper.put(paramHelper.tableName, paramHelper.dyntable); System.out.println(""" Let's also substitute bad credentials for one of the instances in the target group so that it can't access the DynamoDB recommendation table. We will get an instance id value. """); LaunchTemplateCreator templateCreator = new LaunchTemplateCreator(); AutoScaler autoScaler = new AutoScaler(); // Create a new instance profile based on badCredsProfileName. templateCreator.createInstanceProfile(policyFile, policyName, badCredsProfileName, roleName); String badInstanceId = autoScaler.getBadInstance(autoScalingGroupName); System.out.println("The bad instance id values used for this demo is " + badInstanceId); String profileAssociationId = autoScaler.getInstanceProfile(badInstanceId); System.out.println("The association Id value is " + profileAssociationId); System.out.println("Replacing the profile for instance " + badInstanceId + " with a profile that contains bad credentials"); autoScaler.replaceInstanceProfile(badInstanceId, badCredsProfileName, profileAssociationId); System.out.println( """ Now, sending a GET request to the load balancer endpoint returns either a recommendation or a static response, depending on which instance is selected by the load balancer. """); demoChoices(loadBalancer); System.out.println(""" Let's implement a deep health check. For this demo, a deep health check tests whether the web service can access the DynamoDB table that it depends on for recommendations. Note that the deep health check is only for ELB routing and not for Auto Scaling instance health. This kind of deep health check is not recommended for Auto Scaling instance health, because it risks accidental termination of all instances in the Auto Scaling group when a dependent service fails. """); System.out.println(""" By implementing deep health checks, the load balancer can detect when one of the instances is failing and take that instance out of rotation. """); paramHelper.put(paramHelper.healthCheck, "deep"); System.out.println(""" Now, checking target health indicates that the instance with bad credentials is unhealthy. Note that it might take a minute or two for the load balancer to detect the unhealthy instance. Sending a GET request to the load balancer endpoint always returns a recommendation, because the load balancer takes unhealthy instances out of its rotation. """); demoChoices(loadBalancer); System.out.println( """ Because the instances in this demo are controlled by an auto scaler, the simplest way to fix an unhealthy instance is to terminate it and let the auto scaler start a new instance to replace it. """); autoScaler.terminateInstance(badInstanceId); System.out.println(""" Even while the instance is terminating and the new instance is starting, sending a GET request to the web service continues to get a successful recommendation response because the load balancer routes requests to the healthy instances. After the replacement instance starts and reports as healthy, it is included in the load balancing rotation. Note that terminating and replacing an instance typically takes several minutes, during which time you can see the changing health check status until the new instance is running and healthy. """); demoChoices(loadBalancer); System.out.println( "If the recommendation service fails now, deep health checks mean all instances report as unhealthy."); paramHelper.put(paramHelper.tableName, "this-is-not-a-table"); demoChoices(loadBalancer); paramHelper.reset(); } public static void demoChoices(LoadBalancer loadBalancer) throws IOException, InterruptedException { String[] actions = { "Send a GET request to the load balancer endpoint.", "Check the health of load balancer targets.", "Go to the next part of the demo." }; Scanner scanner = new Scanner(System.in); while (true) { System.out.println("-".repeat(88)); System.out.println("See the current state of the service by selecting one of the following choices:"); for (int i = 0; i < actions.length; i++) { System.out.println(i + ": " + actions[i]); } try { System.out.print("\nWhich action would you like to take? "); int choice = scanner.nextInt(); System.out.println("-".repeat(88)); switch (choice) { case 0 -> { System.out.println("Request:\n"); System.out.println("GET http://" + loadBalancer.getEndpoint(lbName)); CloseableHttpClient httpClient = HttpClients.createDefault(); // Create an HTTP GET request to the ELB. HttpGet httpGet = new HttpGet("http://" + loadBalancer.getEndpoint(lbName)); // Execute the request and get the response. HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("HTTP Status Code: " + statusCode); // Display the JSON response BufferedReader reader = new BufferedReader( new InputStreamReader(response.getEntity().getContent())); StringBuilder jsonResponse = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { jsonResponse.append(line); } reader.close(); // Print the formatted JSON response. System.out.println("Full Response:\n"); System.out.println(jsonResponse.toString()); // Close the HTTP client. httpClient.close(); } case 1 -> { System.out.println("\nChecking the health of load balancer targets:\n"); List<TargetHealthDescription> health = loadBalancer.checkTargetHealth(targetGroupName); for (TargetHealthDescription target : health) { System.out.printf("\tTarget %s on port %d is %s%n", target.target().id(), target.target().port(), target.targetHealth().stateAsString()); } System.out.println(""" Note that it can take a minute or two for the health check to update after changes are made. """); } case 2 -> { System.out.println("\nOkay, let's move on."); System.out.println("-".repeat(88)); return; // Exit the method when choice is 2 } default -> System.out.println("You must choose a value between 0-2. Please select again."); } } catch (java.util.InputMismatchException e) { System.out.println("Invalid input. Please select again."); scanner.nextLine(); // Clear the input buffer. } } } public static String readFileAsString(String filePath) throws IOException { byte[] bytes = Files.readAllBytes(Paths.get(filePath)); return new String(bytes); } }
创建一个包含 Auto Scaling 和 Amazon EC2 操作的类。
public class AutoScaler { private static Ec2Client ec2Client; private static AutoScalingClient autoScalingClient; private static IamClient iamClient; private static SsmClient ssmClient; private IamClient getIAMClient() { if (iamClient == null) { iamClient = IamClient.builder() .region(Region.US_EAST_1) .build(); } return iamClient; } private SsmClient getSSMClient() { if (ssmClient == null) { ssmClient = SsmClient.builder() .region(Region.US_EAST_1) .build(); } return ssmClient; } private Ec2Client getEc2Client() { if (ec2Client == null) { ec2Client = Ec2Client.builder() .region(Region.US_EAST_1) .build(); } return ec2Client; } private AutoScalingClient getAutoScalingClient() { if (autoScalingClient == null) { autoScalingClient = AutoScalingClient.builder() .region(Region.US_EAST_1) .build(); } return autoScalingClient; } /** * Terminates and instances in an EC2 Auto Scaling group. After an instance is * terminated, it can no longer be accessed. */ public void terminateInstance(String instanceId) { TerminateInstanceInAutoScalingGroupRequest terminateInstanceIRequest = TerminateInstanceInAutoScalingGroupRequest .builder() .instanceId(instanceId) .shouldDecrementDesiredCapacity(false) .build(); getAutoScalingClient().terminateInstanceInAutoScalingGroup(terminateInstanceIRequest); System.out.format("Terminated instance %s.", instanceId); } /** * Replaces the profile associated with a running instance. After the profile is * replaced, the instance is rebooted to ensure that it uses the new profile. * When * the instance is ready, Systems Manager is used to restart the Python web * server. */ public void replaceInstanceProfile(String instanceId, String newInstanceProfileName, String profileAssociationId) throws InterruptedException { // Create an IAM instance profile specification. software.amazon.awssdk.services.ec2.model.IamInstanceProfileSpecification iamInstanceProfile = software.amazon.awssdk.services.ec2.model.IamInstanceProfileSpecification .builder() .name(newInstanceProfileName) // Make sure 'newInstanceProfileName' is a valid IAM Instance Profile // name. .build(); // Replace the IAM instance profile association for the EC2 instance. ReplaceIamInstanceProfileAssociationRequest replaceRequest = ReplaceIamInstanceProfileAssociationRequest .builder() .iamInstanceProfile(iamInstanceProfile) .associationId(profileAssociationId) // Make sure 'profileAssociationId' is a valid association ID. .build(); try { getEc2Client().replaceIamInstanceProfileAssociation(replaceRequest); // Handle the response as needed. } catch (Ec2Exception e) { // Handle exceptions, log, or report the error. System.err.println("Error: " + e.getMessage()); } System.out.format("Replaced instance profile for association %s with profile %s.", profileAssociationId, newInstanceProfileName); TimeUnit.SECONDS.sleep(15); boolean instReady = false; int tries = 0; // Reboot after 60 seconds while (!instReady) { if (tries % 6 == 0) { getEc2Client().rebootInstances(RebootInstancesRequest.builder() .instanceIds(instanceId) .build()); System.out.println("Rebooting instance " + instanceId + " and waiting for it to be ready."); } tries++; try { TimeUnit.SECONDS.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } DescribeInstanceInformationResponse informationResponse = getSSMClient().describeInstanceInformation(); List<InstanceInformation> instanceInformationList = informationResponse.instanceInformationList(); for (InstanceInformation info : instanceInformationList) { if (info.instanceId().equals(instanceId)) { instReady = true; break; } } } SendCommandRequest sendCommandRequest = SendCommandRequest.builder() .instanceIds(instanceId) .documentName("AWS-RunShellScript") .parameters(Collections.singletonMap("commands", Collections.singletonList("cd / && sudo python3 server.py 80"))) .build(); getSSMClient().sendCommand(sendCommandRequest); System.out.println("Restarted the Python web server on instance " + instanceId + "."); } public void openInboundPort(String secGroupId, String port, String ipAddress) { AuthorizeSecurityGroupIngressRequest ingressRequest = AuthorizeSecurityGroupIngressRequest.builder() .groupName(secGroupId) .cidrIp(ipAddress) .fromPort(Integer.parseInt(port)) .build(); getEc2Client().authorizeSecurityGroupIngress(ingressRequest); System.out.format("Authorized ingress to %s on port %s from %s.", secGroupId, port, ipAddress); } /** * Detaches a role from an instance profile, detaches policies from the role, * and deletes all the resources. */ public void deleteInstanceProfile(String roleName, String profileName) { try { software.amazon.awssdk.services.iam.model.GetInstanceProfileRequest getInstanceProfileRequest = software.amazon.awssdk.services.iam.model.GetInstanceProfileRequest .builder() .instanceProfileName(profileName) .build(); GetInstanceProfileResponse response = getIAMClient().getInstanceProfile(getInstanceProfileRequest); String name = response.instanceProfile().instanceProfileName(); System.out.println(name); RemoveRoleFromInstanceProfileRequest profileRequest = RemoveRoleFromInstanceProfileRequest.builder() .instanceProfileName(profileName) .roleName(roleName) .build(); getIAMClient().removeRoleFromInstanceProfile(profileRequest); DeleteInstanceProfileRequest deleteInstanceProfileRequest = DeleteInstanceProfileRequest.builder() .instanceProfileName(profileName) .build(); getIAMClient().deleteInstanceProfile(deleteInstanceProfileRequest); System.out.println("Deleted instance profile " + profileName); DeleteRoleRequest deleteRoleRequest = DeleteRoleRequest.builder() .roleName(roleName) .build(); // List attached role policies. ListAttachedRolePoliciesResponse rolesResponse = getIAMClient() .listAttachedRolePolicies(role -> role.roleName(roleName)); List<AttachedPolicy> attachedPolicies = rolesResponse.attachedPolicies(); for (AttachedPolicy attachedPolicy : attachedPolicies) { DetachRolePolicyRequest request = DetachRolePolicyRequest.builder() .roleName(roleName) .policyArn(attachedPolicy.policyArn()) .build(); getIAMClient().detachRolePolicy(request); System.out.println("Detached and deleted policy " + attachedPolicy.policyName()); } getIAMClient().deleteRole(deleteRoleRequest); System.out.println("Instance profile and role deleted."); } catch (IamException e) { System.err.println(e.getMessage()); System.exit(1); } } public void deleteTemplate(String templateName) { getEc2Client().deleteLaunchTemplate(name -> name.launchTemplateName(templateName)); System.out.format(templateName + " was deleted."); } public void deleteAutoScaleGroup(String groupName) { DeleteAutoScalingGroupRequest deleteAutoScalingGroupRequest = DeleteAutoScalingGroupRequest.builder() .autoScalingGroupName(groupName) .forceDelete(true) .build(); getAutoScalingClient().deleteAutoScalingGroup(deleteAutoScalingGroupRequest); System.out.println(groupName + " was deleted."); } /* * Verify the default security group of the specified VPC allows ingress from * this * computer. This can be done by allowing ingress from this computer's IP * address. In some situations, such as connecting from a corporate network, you * must instead specify a prefix list ID. You can also temporarily open the port * to * any IP address while running this example. If you do, be sure to remove * public * access when you're done. * */ public GroupInfo verifyInboundPort(String VPC, int port, String ipAddress) { boolean portIsOpen = false; GroupInfo groupInfo = new GroupInfo(); try { Filter filter = Filter.builder() .name("group-name") .values("default") .build(); Filter filter1 = Filter.builder() .name("vpc-id") .values(VPC) .build(); DescribeSecurityGroupsRequest securityGroupsRequest = DescribeSecurityGroupsRequest.builder() .filters(filter, filter1) .build(); DescribeSecurityGroupsResponse securityGroupsResponse = getEc2Client() .describeSecurityGroups(securityGroupsRequest); String securityGroup = securityGroupsResponse.securityGroups().get(0).groupName(); groupInfo.setGroupName(securityGroup); for (SecurityGroup secGroup : securityGroupsResponse.securityGroups()) { System.out.println("Found security group: " + secGroup.groupId()); for (IpPermission ipPermission : secGroup.ipPermissions()) { if (ipPermission.fromPort() == port) { System.out.println("Found inbound rule: " + ipPermission); for (IpRange ipRange : ipPermission.ipRanges()) { String cidrIp = ipRange.cidrIp(); if (cidrIp.startsWith(ipAddress) || cidrIp.equals("0.0.0.0/0")) { System.out.println(cidrIp + " is applicable"); portIsOpen = true; } } if (!ipPermission.prefixListIds().isEmpty()) { System.out.println("Prefix lList is applicable"); portIsOpen = true; } if (!portIsOpen) { System.out .println("The inbound rule does not appear to be open to either this computer's IP," + " all IP addresses (0.0.0.0/0), or to a prefix list ID."); } else { break; } } } } } catch (AutoScalingException e) { System.err.println(e.awsErrorDetails().errorMessage()); } groupInfo.setPortOpen(portIsOpen); return groupInfo; } /* * Attaches an Elastic Load Balancing (ELB) target group to this EC2 Auto * Scaling group. * The target group specifies how the load balancer forward requests to the * instances * in the group. */ public void attachLoadBalancerTargetGroup(String asGroupName, String targetGroupARN) { try { AttachLoadBalancerTargetGroupsRequest targetGroupsRequest = AttachLoadBalancerTargetGroupsRequest.builder() .autoScalingGroupName(asGroupName) .targetGroupARNs(targetGroupARN) .build(); getAutoScalingClient().attachLoadBalancerTargetGroups(targetGroupsRequest); System.out.println("Attached load balancer to " + asGroupName); } catch (AutoScalingException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } // Creates an EC2 Auto Scaling group with the specified size. public String[] createGroup(int groupSize, String templateName, String autoScalingGroupName) { // Get availability zones. software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesRequest zonesRequest = software.amazon.awssdk.services.ec2.model.DescribeAvailabilityZonesRequest .builder() .build(); DescribeAvailabilityZonesResponse zonesResponse = getEc2Client().describeAvailabilityZones(zonesRequest); List<String> availabilityZoneNames = zonesResponse.availabilityZones().stream() .map(software.amazon.awssdk.services.ec2.model.AvailabilityZone::zoneName) .collect(Collectors.toList()); String availabilityZones = String.join(",", availabilityZoneNames); LaunchTemplateSpecification specification = LaunchTemplateSpecification.builder() .launchTemplateName(templateName) .version("$Default") .build(); String[] zones = availabilityZones.split(","); CreateAutoScalingGroupRequest groupRequest = CreateAutoScalingGroupRequest.builder() .launchTemplate(specification) .availabilityZones(zones) .maxSize(groupSize) .minSize(groupSize) .autoScalingGroupName(autoScalingGroupName) .build(); try { getAutoScalingClient().createAutoScalingGroup(groupRequest); } catch (AutoScalingException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } System.out.println("Created an EC2 Auto Scaling group named " + autoScalingGroupName); return zones; } public String getDefaultVPC() { // Define the filter. Filter defaultFilter = Filter.builder() .name("is-default") .values("true") .build(); software.amazon.awssdk.services.ec2.model.DescribeVpcsRequest request = software.amazon.awssdk.services.ec2.model.DescribeVpcsRequest .builder() .filters(defaultFilter) .build(); DescribeVpcsResponse response = getEc2Client().describeVpcs(request); return response.vpcs().get(0).vpcId(); } // Gets the default subnets in a VPC for a specified list of Availability Zones. public List<Subnet> getSubnets(String vpcId, String[] availabilityZones) { List<Subnet> subnets = null; Filter vpcFilter = Filter.builder() .name("vpc-id") .values(vpcId) .build(); Filter azFilter = Filter.builder() .name("availability-zone") .values(availabilityZones) .build(); Filter defaultForAZ = Filter.builder() .name("default-for-az") .values("true") .build(); DescribeSubnetsRequest request = DescribeSubnetsRequest.builder() .filters(vpcFilter, azFilter, defaultForAZ) .build(); DescribeSubnetsResponse response = getEc2Client().describeSubnets(request); subnets = response.subnets(); return subnets; } // Gets data about the instances in the EC2 Auto Scaling group. public String getBadInstance(String groupName) { DescribeAutoScalingGroupsRequest request = DescribeAutoScalingGroupsRequest.builder() .autoScalingGroupNames(groupName) .build(); DescribeAutoScalingGroupsResponse response = getAutoScalingClient().describeAutoScalingGroups(request); AutoScalingGroup autoScalingGroup = response.autoScalingGroups().get(0); List<String> instanceIds = autoScalingGroup.instances().stream() .map(instance -> instance.instanceId()) .collect(Collectors.toList()); String[] instanceIdArray = instanceIds.toArray(new String[0]); for (String instanceId : instanceIdArray) { System.out.println("Instance ID: " + instanceId); return instanceId; } return ""; } // Gets data about the profile associated with an instance. public String getInstanceProfile(String instanceId) { Filter filter = Filter.builder() .name("instance-id") .values(instanceId) .build(); DescribeIamInstanceProfileAssociationsRequest associationsRequest = DescribeIamInstanceProfileAssociationsRequest .builder() .filters(filter) .build(); DescribeIamInstanceProfileAssociationsResponse response = getEc2Client() .describeIamInstanceProfileAssociations(associationsRequest); return response.iamInstanceProfileAssociations().get(0).associationId(); } public void deleteRolesPolicies(String policyName, String roleName, String InstanceProfile) { ListPoliciesRequest listPoliciesRequest = ListPoliciesRequest.builder().build(); ListPoliciesResponse listPoliciesResponse = getIAMClient().listPolicies(listPoliciesRequest); for (Policy policy : listPoliciesResponse.policies()) { if (policy.policyName().equals(policyName)) { // List the entities (users, groups, roles) that are attached to the policy. software.amazon.awssdk.services.iam.model.ListEntitiesForPolicyRequest listEntitiesRequest = software.amazon.awssdk.services.iam.model.ListEntitiesForPolicyRequest .builder() .policyArn(policy.arn()) .build(); ListEntitiesForPolicyResponse listEntitiesResponse = iamClient .listEntitiesForPolicy(listEntitiesRequest); if (!listEntitiesResponse.policyGroups().isEmpty() || !listEntitiesResponse.policyUsers().isEmpty() || !listEntitiesResponse.policyRoles().isEmpty()) { // Detach the policy from any entities it is attached to. DetachRolePolicyRequest detachPolicyRequest = DetachRolePolicyRequest.builder() .policyArn(policy.arn()) .roleName(roleName) // Specify the name of the IAM role .build(); getIAMClient().detachRolePolicy(detachPolicyRequest); System.out.println("Policy detached from entities."); } // Now, you can delete the policy. DeletePolicyRequest deletePolicyRequest = DeletePolicyRequest.builder() .policyArn(policy.arn()) .build(); getIAMClient().deletePolicy(deletePolicyRequest); System.out.println("Policy deleted successfully."); break; } } // List the roles associated with the instance profile ListInstanceProfilesForRoleRequest listRolesRequest = ListInstanceProfilesForRoleRequest.builder() .roleName(roleName) .build(); // Detach the roles from the instance profile ListInstanceProfilesForRoleResponse listRolesResponse = iamClient.listInstanceProfilesForRole(listRolesRequest); for (software.amazon.awssdk.services.iam.model.InstanceProfile profile : listRolesResponse.instanceProfiles()) { RemoveRoleFromInstanceProfileRequest removeRoleRequest = RemoveRoleFromInstanceProfileRequest.builder() .instanceProfileName(InstanceProfile) .roleName(roleName) // Remove the extra dot here .build(); getIAMClient().removeRoleFromInstanceProfile(removeRoleRequest); System.out.println("Role " + roleName + " removed from instance profile " + InstanceProfile); } // Delete the instance profile after removing all roles DeleteInstanceProfileRequest deleteInstanceProfileRequest = DeleteInstanceProfileRequest.builder() .instanceProfileName(InstanceProfile) .build(); getIAMClient().deleteInstanceProfile(r -> r.instanceProfileName(InstanceProfile)); System.out.println(InstanceProfile + " Deleted"); System.out.println("All roles and policies are deleted."); } }
创建一个包含弹性负载均衡操作的类。
public class LoadBalancer { public ElasticLoadBalancingV2Client elasticLoadBalancingV2Client; public ElasticLoadBalancingV2Client getLoadBalancerClient() { if (elasticLoadBalancingV2Client == null) { elasticLoadBalancingV2Client = ElasticLoadBalancingV2Client.builder() .region(Region.US_EAST_1) .build(); } return elasticLoadBalancingV2Client; } // Checks the health of the instances in the target group. public List<TargetHealthDescription> checkTargetHealth(String targetGroupName) { DescribeTargetGroupsRequest targetGroupsRequest = DescribeTargetGroupsRequest.builder() .names(targetGroupName) .build(); DescribeTargetGroupsResponse tgResponse = getLoadBalancerClient().describeTargetGroups(targetGroupsRequest); DescribeTargetHealthRequest healthRequest = DescribeTargetHealthRequest.builder() .targetGroupArn(tgResponse.targetGroups().get(0).targetGroupArn()) .build(); DescribeTargetHealthResponse healthResponse = getLoadBalancerClient().describeTargetHealth(healthRequest); return healthResponse.targetHealthDescriptions(); } // Gets the HTTP endpoint of the load balancer. public String getEndpoint(String lbName) { DescribeLoadBalancersResponse res = getLoadBalancerClient() .describeLoadBalancers(describe -> describe.names(lbName)); return res.loadBalancers().get(0).dnsName(); } // Deletes a load balancer. public void deleteLoadBalancer(String lbName) { try { // Use a waiter to delete the Load Balancer. DescribeLoadBalancersResponse res = getLoadBalancerClient() .describeLoadBalancers(describe -> describe.names(lbName)); ElasticLoadBalancingV2Waiter loadBalancerWaiter = getLoadBalancerClient().waiter(); DescribeLoadBalancersRequest request = DescribeLoadBalancersRequest.builder() .loadBalancerArns(res.loadBalancers().get(0).loadBalancerArn()) .build(); getLoadBalancerClient().deleteLoadBalancer( builder -> builder.loadBalancerArn(res.loadBalancers().get(0).loadBalancerArn())); WaiterResponse<DescribeLoadBalancersResponse> waiterResponse = loadBalancerWaiter .waitUntilLoadBalancersDeleted(request); waiterResponse.matched().response().ifPresent(System.out::println); } catch (ElasticLoadBalancingV2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); } System.out.println(lbName + " was deleted."); } // Deletes the target group. public void deleteTargetGroup(String targetGroupName) { try { DescribeTargetGroupsResponse res = getLoadBalancerClient() .describeTargetGroups(describe -> describe.names(targetGroupName)); getLoadBalancerClient() .deleteTargetGroup(builder -> builder.targetGroupArn(res.targetGroups().get(0).targetGroupArn())); } catch (ElasticLoadBalancingV2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); } System.out.println(targetGroupName + " was deleted."); } // Verify this computer can successfully send a GET request to the load balancer // endpoint. public boolean verifyLoadBalancerEndpoint(String elbDnsName) throws IOException, InterruptedException { boolean success = false; int retries = 3; CloseableHttpClient httpClient = HttpClients.createDefault(); // Create an HTTP GET request to the ELB. HttpGet httpGet = new HttpGet("http://" + elbDnsName); try { while ((!success) && (retries > 0)) { // Execute the request and get the response. HttpResponse response = httpClient.execute(httpGet); int statusCode = response.getStatusLine().getStatusCode(); System.out.println("HTTP Status Code: " + statusCode); if (statusCode == 200) { success = true; } else { retries--; System.out.println("Got connection error from load balancer endpoint, retrying..."); TimeUnit.SECONDS.sleep(15); } } } catch (org.apache.http.conn.HttpHostConnectException e) { System.out.println(e.getMessage()); } System.out.println("Status.." + success); return success; } /* * Creates an Elastic Load Balancing target group. The target group specifies * how * the load balancer forward requests to instances in the group and how instance * health is checked. */ public String createTargetGroup(String protocol, int port, String vpcId, String targetGroupName) { CreateTargetGroupRequest targetGroupRequest = CreateTargetGroupRequest.builder() .healthCheckPath("/healthcheck") .healthCheckTimeoutSeconds(5) .port(port) .vpcId(vpcId) .name(targetGroupName) .protocol(protocol) .build(); CreateTargetGroupResponse targetGroupResponse = getLoadBalancerClient().createTargetGroup(targetGroupRequest); String targetGroupArn = targetGroupResponse.targetGroups().get(0).targetGroupArn(); String targetGroup = targetGroupResponse.targetGroups().get(0).targetGroupName(); System.out.println("The " + targetGroup + " was created with ARN" + targetGroupArn); return targetGroupArn; } /* * Creates an Elastic Load Balancing load balancer that uses the specified * subnets * and forwards requests to the specified target group. */ public String createLoadBalancer(List<Subnet> subnetIds, String targetGroupARN, String lbName, int port, String protocol) { try { List<String> subnetIdStrings = subnetIds.stream() .map(Subnet::subnetId) .collect(Collectors.toList()); CreateLoadBalancerRequest balancerRequest = CreateLoadBalancerRequest.builder() .subnets(subnetIdStrings) .name(lbName) .scheme("internet-facing") .build(); // Create and wait for the load balancer to become available. CreateLoadBalancerResponse lsResponse = getLoadBalancerClient().createLoadBalancer(balancerRequest); String lbARN = lsResponse.loadBalancers().get(0).loadBalancerArn(); ElasticLoadBalancingV2Waiter loadBalancerWaiter = getLoadBalancerClient().waiter(); DescribeLoadBalancersRequest request = DescribeLoadBalancersRequest.builder() .loadBalancerArns(lbARN) .build(); System.out.println("Waiting for Load Balancer " + lbName + " to become available."); WaiterResponse<DescribeLoadBalancersResponse> waiterResponse = loadBalancerWaiter .waitUntilLoadBalancerAvailable(request); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("Load Balancer " + lbName + " is available."); // Get the DNS name (endpoint) of the load balancer. String lbDNSName = lsResponse.loadBalancers().get(0).dnsName(); System.out.println("*** Load Balancer DNS Name: " + lbDNSName); // Create a listener for the load balance. Action action = Action.builder() .targetGroupArn(targetGroupARN) .type("forward") .build(); CreateListenerRequest listenerRequest = CreateListenerRequest.builder() .loadBalancerArn(lsResponse.loadBalancers().get(0).loadBalancerArn()) .defaultActions(action) .port(port) .protocol(protocol) .defaultActions(action) .build(); getLoadBalancerClient().createListener(listenerRequest); System.out.println("Created listener to forward traffic from load balancer " + lbName + " to target group " + targetGroupARN); // Return the load balancer DNS name. return lbDNSName; } catch (ElasticLoadBalancingV2Exception e) { e.printStackTrace(); } return ""; } }
创建一个使用 DynamoDB 模拟推荐服务的类。
public class Database { private static DynamoDbClient dynamoDbClient; public static DynamoDbClient getDynamoDbClient() { if (dynamoDbClient == null) { dynamoDbClient = DynamoDbClient.builder() .region(Region.US_EAST_1) .build(); } return dynamoDbClient; } // Checks to see if the Amazon DynamoDB table exists. private boolean doesTableExist(String tableName) { try { // Describe the table and catch any exceptions. DescribeTableRequest describeTableRequest = DescribeTableRequest.builder() .tableName(tableName) .build(); getDynamoDbClient().describeTable(describeTableRequest); System.out.println("Table '" + tableName + "' exists."); return true; } catch (ResourceNotFoundException e) { System.out.println("Table '" + tableName + "' does not exist."); } catch (DynamoDbException e) { System.err.println("Error checking table existence: " + e.getMessage()); } return false; } /* * Creates a DynamoDB table to use a recommendation service. The table has a * hash key named 'MediaType' that defines the type of media recommended, such * as * Book or Movie, and a range key named 'ItemId' that, combined with the * MediaType, * forms a unique identifier for the recommended item. */ public void createTable(String tableName, String fileName) throws IOException { // First check to see if the table exists. boolean doesExist = doesTableExist(tableName); if (!doesExist) { DynamoDbWaiter dbWaiter = getDynamoDbClient().waiter(); CreateTableRequest createTableRequest = CreateTableRequest.builder() .tableName(tableName) .attributeDefinitions( AttributeDefinition.builder() .attributeName("MediaType") .attributeType(ScalarAttributeType.S) .build(), AttributeDefinition.builder() .attributeName("ItemId") .attributeType(ScalarAttributeType.N) .build()) .keySchema( KeySchemaElement.builder() .attributeName("MediaType") .keyType(KeyType.HASH) .build(), KeySchemaElement.builder() .attributeName("ItemId") .keyType(KeyType.RANGE) .build()) .provisionedThroughput( ProvisionedThroughput.builder() .readCapacityUnits(5L) .writeCapacityUnits(5L) .build()) .build(); getDynamoDbClient().createTable(createTableRequest); System.out.println("Creating table " + tableName + "..."); // Wait until the Amazon DynamoDB table is created. DescribeTableRequest tableRequest = DescribeTableRequest.builder() .tableName(tableName) .build(); WaiterResponse<DescribeTableResponse> waiterResponse = dbWaiter.waitUntilTableExists(tableRequest); waiterResponse.matched().response().ifPresent(System.out::println); System.out.println("Table " + tableName + " created."); // Add records to the table. populateTable(fileName, tableName); } } public void deleteTable(String tableName) { getDynamoDbClient().deleteTable(table -> table.tableName(tableName)); System.out.println("Table " + tableName + " deleted."); } // Populates the table with data located in a JSON file using the DynamoDB // enhanced client. public void populateTable(String fileName, String tableName) throws IOException { DynamoDbEnhancedClient enhancedClient = DynamoDbEnhancedClient.builder() .dynamoDbClient(getDynamoDbClient()) .build(); ObjectMapper objectMapper = new ObjectMapper(); File jsonFile = new File(fileName); JsonNode rootNode = objectMapper.readTree(jsonFile); DynamoDbTable<Recommendation> mappedTable = enhancedClient.table(tableName, TableSchema.fromBean(Recommendation.class)); for (JsonNode currentNode : rootNode) { String mediaType = currentNode.path("MediaType").path("S").asText(); int itemId = currentNode.path("ItemId").path("N").asInt(); String title = currentNode.path("Title").path("S").asText(); String creator = currentNode.path("Creator").path("S").asText(); // Create a Recommendation object and set its properties. Recommendation rec = new Recommendation(); rec.setMediaType(mediaType); rec.setItemId(itemId); rec.setTitle(title); rec.setCreator(creator); // Put the item into the DynamoDB table. mappedTable.putItem(rec); // Add the Recommendation to the list. } System.out.println("Added all records to the " + tableName); } }
创建一个包含 Systems Manager 操作的类。
public class ParameterHelper { String tableName = "doc-example-resilient-architecture-table"; String dyntable = "doc-example-recommendation-service"; String failureResponse = "doc-example-resilient-architecture-failure-response"; String healthCheck = "doc-example-resilient-architecture-health-check"; public void reset() { put(dyntable, tableName); put(failureResponse, "none"); put(healthCheck, "shallow"); } public void put(String name, String value) { SsmClient ssmClient = SsmClient.builder() .region(Region.US_EAST_1) .build(); PutParameterRequest parameterRequest = PutParameterRequest.builder() .name(name) .value(value) .overwrite(true) .type("String") .build(); ssmClient.putParameter(parameterRequest); System.out.printf("Setting demo parameter %s to '%s'.", name, value); } }
-
有关API详细信息,请参阅 “参AWS SDK for Java 2.x API考” 中的以下主题。
-