托管 Amazon EC2 实例 - Amazon SDK for Java 2.x
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

托管 Amazon EC2 实例

创建实例

要创建新 Amazon EC2 实例,请调用 Ec2ClientrunInstances 方法,并为它提供 RunInstancesRequest,其中包含要使用的亚马逊机器映像 (AMI) 和一个实例类型

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.InstanceType; import software.amazon.awssdk.services.ec2.model.RunInstancesRequest; import software.amazon.awssdk.services.ec2.model.RunInstancesResponse; import software.amazon.awssdk.services.ec2.model.Tag; import software.amazon.awssdk.services.ec2.model.CreateTagsRequest; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

代码

public static String createEC2Instance(Ec2Client ec2,String name, String amiId ) { RunInstancesRequest runRequest = RunInstancesRequest.builder() .imageId(amiId) .instanceType(InstanceType.T1_MICRO) .maxCount(1) .minCount(1) .build(); RunInstancesResponse response = ec2.runInstances(runRequest); String instanceId = response.instances().get(0).instanceId(); Tag tag = Tag.builder() .key("Name") .value(name) .build(); CreateTagsRequest tagRequest = CreateTagsRequest.builder() .resources(instanceId) .tags(tag) .build(); try { ec2.createTags(tagRequest); System.out.printf( "Successfully started EC2 Instance %s based on AMI %s", instanceId, amiId); return instanceId; } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } return ""; }

请参阅 GitHub 上的完整示例

启动实例

要启动 Amazon EC2 实例,请调用 Ec2Client 的 startInstances 方法,并为它提供 StartInstancesRequest,其中包含要启动实例的 ID。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;

代码

public static void startInstance(Ec2Client ec2, String instanceId) { StartInstancesRequest request = StartInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.startInstances(request); System.out.printf("Successfully started instance %s", instanceId); }

请参阅 GitHub 上的完整示例

停止实例

要停止 Amazon EC2 实例,请调用 Ec2Client 的 stopInstances 方法,并为它提供 StopInstancesRequest,其中包含要停止的实例的 ID。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.StartInstancesRequest; import software.amazon.awssdk.services.ec2.model.StopInstancesRequest;

代码

public static void stopInstance(Ec2Client ec2, String instanceId) { StopInstancesRequest request = StopInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.stopInstances(request); System.out.printf("Successfully stopped instance %s", instanceId); }

请参阅 GitHub 上的完整示例

重启实例

要重启 Amazon EC2 实例,请调用 Ec2Client 的 rebootInstances 方法,并为它提供 RebootInstancesRequest,其中包含要重启实例的 ID。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.Ec2Exception; import software.amazon.awssdk.services.ec2.model.RebootInstancesRequest;

代码

public static void rebootEC2Instance(Ec2Client ec2, String instanceId) { try { RebootInstancesRequest request = RebootInstancesRequest.builder() .instanceIds(instanceId) .build(); ec2.rebootInstances(request); System.out.printf( "Successfully rebooted instance %s", instanceId); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

请参阅 GitHub 上的完整示例

描述实例

要列出您的实例,您需要创建 DescribeInstancesRequest 并调用 Ec2Client 的 describeInstances 方法。该方法将返回 DescribeInstancesResponse 对象,您可以用它来列出您的账户和区域的 Amazon EC2 实例。

实例按预留进行分组。每个预留对应对启动实例的 startInstances 的调用。要列出您的实例,您必须先调用 DescribeInstancesResponse 类的 reservations 方法,然后在每个返回的 instancesReservation 对象上调用

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.DescribeInstancesRequest; import software.amazon.awssdk.services.ec2.model.DescribeInstancesResponse; import software.amazon.awssdk.services.ec2.model.Instance; import software.amazon.awssdk.services.ec2.model.Reservation; import software.amazon.awssdk.services.ec2.model.Ec2Exception;

代码

public static void describeEC2Instances( Ec2Client ec2){ String nextToken = null; try { do { DescribeInstancesRequest request = DescribeInstancesRequest.builder().maxResults(6).nextToken(nextToken).build(); DescribeInstancesResponse response = ec2.describeInstances(request); for (Reservation reservation : response.reservations()) { for (Instance instance : reservation.instances()) { System.out.println("Instance Id is " + instance.instanceId()); System.out.println("Image id is "+ instance.imageId()); System.out.println("Instance type is "+ instance.instanceType()); System.out.println("Instance state name is "+ instance.state().name()); System.out.println("monitoring information is "+ instance.monitoring().state()); } } nextToken = response.nextToken(); } while (nextToken != null); } catch (Ec2Exception e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }

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

请参阅 GitHub 上的完整示例

监控实例

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

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

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;

代码

public static void monitorInstance( Ec2Client ec2, String instanceId) { MonitorInstancesRequest request = MonitorInstancesRequest.builder() .instanceIds(instanceId).build(); ec2.monitorInstances(request); System.out.printf( "Successfully enabled monitoring for instance %s", instanceId); }

请参阅 GitHub 上的完整示例

停止实例监控

要停止监控实例,您必须用要停止监控实例的 ID 创建一个 UnmonitorInstancesRequest,并将其传递给 Ec2Client 的 unmonitorInstances 方法。

导入

import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.ec2.Ec2Client; import software.amazon.awssdk.services.ec2.model.MonitorInstancesRequest; import software.amazon.awssdk.services.ec2.model.UnmonitorInstancesRequest;

代码

public static void unmonitorInstance(Ec2Client ec2, String instanceId) { UnmonitorInstancesRequest request = UnmonitorInstancesRequest.builder() .instanceIds(instanceId).build(); ec2.unmonitorInstances(request); System.out.printf( "Successfully disabled monitoring for instance %s", instanceId); }

请参阅 GitHub 上的完整示例

更多信息