简体   繁体   English

如何在java中以编程方式启动和停止Amazon EC2实例

[英]How to start and stop an Amazon EC2 instance programmatically in java

How do i start and stop an amazon EC2 instance programmatically using aws-sdk in java? 如何在java中使用aws-sdk以编程方式启动和停止亚马逊EC2实例?

Any helps are greatly appreciated as I have spent a day while trying to sort this out. 任何帮助都非常感谢,因为我花了一天时间试图解决这个问题。

I've recently implemented this functionality within the Bamboo AWS Plugin ; 我最近在Bamboo AWS插件中实现了这个功能; it's Open Source and the code is available on Bitbucket , you can find a complete example how to start/stop/reboot an instance within EC2Task.java (should be a separate class actually, alas ...). 它是开源的, 代码可以在Bitbucket上找到,你可以找到一个完整的例子,说明如何在EC2Task.java中启动/停止/重启实例(实际应该是一个单独的类,唉...)。

Fortunately this is not complicated at all, for example, an instance can be started like so: 幸运的是,这并不复杂,例如,可以像这样启动一个实例:

private String startInstance(final String instanceId, AmazonEC2 ec2, final BuildLogger buildLogger)
        throws AmazonServiceException, AmazonClientException, InterruptedException
{
    StartInstancesRequest startRequest = new StartInstancesRequest().withInstanceIds(instanceId);
    StartInstancesResult startResult = ec2.startInstances(startRequest);
    List<InstanceStateChange> stateChangeList = startResult.getStartingInstances();
    buildLogger.addBuildLogEntry("Starting instance '" + instanceId + "':");

    // Wait for the instance to be started
    return waitForTransitionCompletion(stateChangeList, "running", ec2, instanceId, buildLogger); }

BuildLogger is Bamboo specific and waitForTransitionCompletion() is an implementation specific helper to report back on the process/result. BuildLogger是特定 Bamboo的, waitForTransitionCompletion()是一个特定于实现的帮助程序,用于报告进程/结果。 The AmazonEC2 ec2 parameter passes the reference to an AmazonEC2Client object by means of the AmazonEC2 interface, which defines all relevant methods (amongst many others), specifically: AmazonEC2 ec2参数通过AmazonEC2接口传递对AmazonEC2Client对象的引用,该接口定义了所有相关方法(以及许多其他方法),具体为:

If you have already used AWS API , it's simple call on AmazonEC2Client object. 如果您已经使用过AWS API ,那么可以在AmazonEC2Client对象上进行简单调用。 Use the following methods 使用以下方法

Also, you might be knowing the start/stop mechanism works only for the images with root device backed by EBS. 此外,您可能知道启动/停止机制适用于具有EBS支持的根设备的映像。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Java中使用标签名称停止EC2实例? - How to stop EC2 instance using Tag name in java? 如何使用Java Apache FTPClient将文件从一个Amazon EC2实例下载到另一个EC2实例 - How to download file from one Amazon EC2 instance to another EC2 instance using java apache FTPClient 如何将图像文件从amazon s3移动到amazon ec2并使用java运行实例 - How to move an image file from amazon s3 to amazon ec2 and run the instance using java 具有特定 EBS 卷大小 Java 开发工具包的 Amazon EC2 实例 - Amazon EC2 instance with a specific EBS Volume Size Java SDK 从JAVA API获取Amazon EC2实例的公共DNS - Get Public DNS of Amazon EC2 Instance from JAVA API AWS EC2 Java SDK:使用自定义实例类型启动实例 - AWS EC2 Java SDK: Start an instance with custom instance type 如何使用与EC2实例关联的IAM角色从Amazon检索临时AWS凭证(在Java中)? - How to retrieve temporary AWS credentials from Amazon using IAM role associated with the EC2 instance(in java)? 在java中,如何让Amazon EC2实例查看自己的标签? - In java, how can I get an Amazon EC2 Instance to see its own tags? 如何设置 Amazon EC2 实例以在 tomcat 上运行多个 java spring 应用程序 - How to Setup Amazon EC2 instance for running multiple java spring apps on tomcat 如何通过Java以编程方式显示特定的EC2实例成本(价格明细) - How to display particular EC2 instance cost (price details) through java programmatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM