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

现在,您可以使用Amazon S3转移经理(开发人员预览版)中的Amazon SDK for Java2.x 用于加速文件传输。试试一下然后告诉我们您的想法!顺便说一句,Amazon SDK for Java正在招聘团队软件开发工程师

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

ManageAmazon EC2实例

创建 实例

创建新的Amazon EC2通过调用 Ec2Client 的实例runInstances方法,为它提供RunInstancesRequest包含Amazon 系统映像 (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实例,调用 Ec2ClientstartInstances方法,为它提供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实例,调用 Ec2ClientstopInstances方法,为它提供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实例,调用 Ec2ClientrebootInstances方法,为它提供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然后致电 Ec2ClientdescribeInstances方法。该方法将返回 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){ boolean done = false; 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中的Amazon EC2适用于 Linux 实例的用户指南。

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

导入

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 上的完整示例

停止实例监控

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

导入

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 上的完整示例

更多信息