管理 Amazon EC2实例 - AWS SDK for Java

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

管理 Amazon EC2实例

创建实例

创建新的Amazon EC2通过调用 Amazonec2Client 来实例runInstances方法,为它提供RunInstancesRequest包含Amazon 系统映像 (AMI)要使用和实例类型.

导入

import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.InstanceType; import com.amazonaws.services.ec2.model.RunInstancesRequest; import com.amazonaws.services.ec2.model.RunInstancesResult; import com.amazonaws.services.ec2.model.Tag;

代码

RunInstancesRequest run_request = new RunInstancesRequest() .withImageId(ami_id) .withInstanceType(InstanceType.T1Micro) .withMaxCount(1) .withMinCount(1); RunInstancesResult run_response = ec2.runInstances(run_request); String reservation_id = run_response.getReservation().getInstances().get(0).getInstanceId();

请参阅完整示例

启动实例

启动Amazon EC2实例,请致电 Amazonec2C2ClientstartInstances方法,为它提供StartInstancesRequest包含要启动的实例的 ID。

导入

import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.StartInstancesRequest;

代码

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); StartInstancesRequest request = new StartInstancesRequest() .withInstanceIds(instance_id); ec2.startInstances(request);

请参阅完整示例

停止实例

要停止Amazon EC2实例,请致电 Amazonec2C2ClientstopInstances方法,为它提供StopInstancesRequest包含要停止的实例的 ID。

导入

import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.StopInstancesRequest;

代码

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); StopInstancesRequest request = new StopInstancesRequest() .withInstanceIds(instance_id); ec2.stopInstances(request);

请参阅完整示例

重启 实例

要重启Amazon EC2实例,请致电 Amazonec2C2ClientrebootInstances方法,为它提供RebootInstancesRequest包含要重启的实例的 ID。

导入

import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.RebootInstancesRequest; import com.amazonaws.services.ec2.model.RebootInstancesResult;

代码

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); RebootInstancesRequest request = new RebootInstancesRequest() .withInstanceIds(instance_id); RebootInstancesResult response = ec2.rebootInstances(request);

请参阅完整示例

描述 实例

要列出您的实例,您需要创建DescribeInstancesRequest然后致电 Amazonec2C2ClientdescribeInstances方法。它将返回 DescribeInstancesResult 对象,您可以用它来列出您的账户和区域的 Amazon EC2 实例。

实例按预留进行分组。每个预留对应对启动实例的 startInstances 的调用。要列出您的实例,您必须首先在每个返回的 DescribeInstancesResultReservationgetReservations' method, and then call `getInstances 对象上调用 类的

导入

import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.DescribeInstancesRequest; import com.amazonaws.services.ec2.model.DescribeInstancesResult; import com.amazonaws.services.ec2.model.Instance; import com.amazonaws.services.ec2.model.Reservation;

代码

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); boolean done = false; DescribeInstancesRequest request = new DescribeInstancesRequest(); while(!done) { DescribeInstancesResult response = ec2.describeInstances(request); for(Reservation reservation : response.getReservations()) { for(Instance instance : reservation.getInstances()) { System.out.printf( "Found instance with id %s, " + "AMI %s, " + "type %s, " + "state %s " + "and monitoring state %s", instance.getInstanceId(), instance.getImageId(), instance.getInstanceType(), instance.getState().getName(), instance.getMonitoring().getState()); } } request.setNextToken(response.getNextToken()); if(response.getNextToken() == null) { done = true; } }

结果将分页;您可以获取更多结果,方式是:将从结果对象的 getNextToken 方法返回的值传递到您的原始请求对象的 setNextToken 方法,然后在下一个 describeInstances 调用中使用相同的请求对象。

请参阅完整示例

监控实例

您可以监控 Amazon EC2 实例的各方面,例如 CPU 和网络利用率、可用内存和剩余磁盘空间。要了解有关实例监控的更多信息,请监控Amazon EC2中的Amazon EC2适用于 Linux 实例的用户指南。

要开始监控实例,您必须创建MonitorInstancesRequest要监控的实例的 ID,并将其传递给 Amazonec2ClientmonitorInstances方法。

导入

import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.MonitorInstancesRequest;

代码

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); MonitorInstancesRequest request = new MonitorInstancesRequest() .withInstanceIds(instance_id); ec2.monitorInstances(request);

请参阅完整示例

停止实例监控

要停止监控实例,请创建UnmonitorInstancesRequest要停止监控的实例的 ID,并将其传递给 Amazonec2ClientunmonitorInstances方法。

导入

import com.amazonaws.services.ec2.AmazonEC2; import com.amazonaws.services.ec2.AmazonEC2ClientBuilder; import com.amazonaws.services.ec2.model.UnmonitorInstancesRequest;

代码

final AmazonEC2 ec2 = AmazonEC2ClientBuilder.defaultClient(); UnmonitorInstancesRequest request = new UnmonitorInstancesRequest() .withInstanceIds(instance_id); ec2.unmonitorInstances(request);

请参阅完整示例

更多信息