Created
August 13, 2013 15:40
-
-
Save masazdream/6222496 to your computer and use it in GitHub Desktop.
AWSのEC2を起動、停止するプログラム
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package jp.masaz.aws; | |
| import java.util.ArrayList; | |
| import java.util.List; | |
| import com.amazonaws.auth.BasicAWSCredentials; | |
| import com.amazonaws.regions.Region; | |
| import com.amazonaws.regions.Regions; | |
| import com.amazonaws.services.ec2.AmazonEC2Client; | |
| import com.amazonaws.services.ec2.model.InstanceState; | |
| import com.amazonaws.services.ec2.model.InstanceStateChange; | |
| import com.amazonaws.services.ec2.model.StartInstancesRequest; | |
| import com.amazonaws.services.ec2.model.StartInstancesResult; | |
| import com.amazonaws.services.ec2.model.StopInstancesRequest; | |
| import com.amazonaws.services.ec2.model.StopInstancesResult; | |
| /** | |
| * EC2を操作するクラス | |
| * | |
| * @author masahiro | |
| * | |
| */ | |
| public class EC2Tools { | |
| public void startEc2(String intanceId, boolean isDebug) { | |
| try { | |
| if (isDebug) { | |
| System.out.println("start: " + intanceId); | |
| return; | |
| } | |
| BasicAWSCredentials basicAwsCredentials = new BasicAWSCredentials( | |
| AWSConsts.ACCESS_KEY, AWSConsts.SECRET_ACCESS_KEY); | |
| AmazonEC2Client amazonEC2Client = new AmazonEC2Client( | |
| basicAwsCredentials); | |
| amazonEC2Client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); | |
| List<String> instanceIds = new ArrayList<String>(); | |
| instanceIds.add(intanceId); | |
| StartInstancesRequest startInstancesRequest = new StartInstancesRequest( | |
| instanceIds); | |
| // インスタンスの起動 | |
| StartInstancesResult startInstancesResult = amazonEC2Client | |
| .startInstances(startInstancesRequest); | |
| List<InstanceStateChange> stateChanges = startInstancesResult | |
| .getStartingInstances(); | |
| List<String> stateList = getResultStatus(stateChanges); | |
| int changeCnt = stateList.size(); | |
| System.out.println("start instance cnt: " + changeCnt); | |
| for (String output : stateList) { | |
| System.out.println("start result:" + output); | |
| } | |
| } catch (Exception exception) { | |
| System.out.println("[Error] failed to start ec2 instance: " | |
| + exception.getMessage()); | |
| } | |
| } | |
| public void stopEc2(String intanceId, boolean isDebug) { | |
| try { | |
| if (isDebug) { | |
| System.out.println("stop: " + intanceId); | |
| return; | |
| } | |
| BasicAWSCredentials basicAwsCredentials = new BasicAWSCredentials( | |
| AWSConsts.ACCESS_KEY, AWSConsts.SECRET_ACCESS_KEY); | |
| AmazonEC2Client amazonEC2Client = new AmazonEC2Client( | |
| basicAwsCredentials); | |
| amazonEC2Client.setRegion(Region.getRegion(Regions.AP_NORTHEAST_1)); | |
| List<String> instanceIds = new ArrayList<String>(); | |
| instanceIds.add(intanceId); | |
| StopInstancesRequest stopInstancesRequest = new StopInstancesRequest( | |
| instanceIds); | |
| // インスタンスの停止 | |
| StopInstancesResult stopInstancesResult = amazonEC2Client | |
| .stopInstances(stopInstancesRequest); | |
| List<InstanceStateChange> stateChanges = stopInstancesResult | |
| .getStoppingInstances(); | |
| List<String> stateList = getResultStatus(stateChanges); | |
| int changeCnt = stateList.size(); | |
| System.out.println("stop instance cnt: " + changeCnt); | |
| for (String output : stateList) { | |
| System.out.println("stop result: " + output); | |
| } | |
| } catch (Exception exception) { | |
| System.out.println("[Error] failed to stop ec2 instance: " | |
| + exception.getMessage()); | |
| } | |
| } | |
| private List<String> getResultStatus(List<InstanceStateChange> stateChanges) { | |
| ArrayList<String> resultList = new ArrayList<String>(); | |
| for (InstanceStateChange state : stateChanges) { | |
| String id = state.getInstanceId(); | |
| InstanceState pState = state.getPreviousState(); | |
| InstanceState cState = state.getCurrentState(); | |
| StringBuilder sb = new StringBuilder(); | |
| sb.append(id); | |
| sb.append("\t"); | |
| sb.append("pState: "); | |
| sb.append(pState.getName()); | |
| sb.append(","); | |
| sb.append(pState.getCode()); | |
| sb.append("\t"); | |
| sb.append("cState: "); | |
| sb.append(cState.getName()); | |
| sb.append(","); | |
| sb.append(cState.getCode()); | |
| resultList.add(sb.toString()); | |
| } | |
| return resultList; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment