[英]How to start and stop an Amazon EC2 instance programmatically in java
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.