Last active
August 29, 2015 14:10
-
-
Save Twinuma/6150d0fc160a29dbc657 to your computer and use it in GitHub Desktop.
特定のタグが付いている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
import java.util.ArrayList; | |
import java.util.List; | |
import com.amazonaws.regions.Region; | |
import com.amazonaws.regions.Regions; | |
import com.amazonaws.services.ec2.AmazonEC2; | |
import com.amazonaws.services.ec2.AmazonEC2Client; | |
import com.amazonaws.services.ec2.model.DescribeTagsRequest; | |
import com.amazonaws.services.ec2.model.DescribeTagsResult; | |
import com.amazonaws.services.ec2.model.StartInstancesRequest; | |
import com.amazonaws.services.ec2.model.TagDescription; | |
/** | |
* 特定のタグが付いているインスタンスを起動 | |
* @author takachan | |
* | |
*/ | |
public class ec2InstanceStart { | |
public static void main(String[] args) { | |
AmazonEC2 ec2_us = new AmazonEC2Client(); | |
ec2_us.setRegion(Region.getRegion(Regions.US_EAST_1)); | |
List<String> startResourceIds = new ArrayList<String>(); | |
DescribeTagsResult tagsResult = ec2_us | |
.describeTags(new DescribeTagsRequest()); | |
// Tagに"NAME=environment, VALUE=dev"が入っているインスタンスを取得 | |
for (TagDescription description : tagsResult.getTags()) { | |
if ("environment".equals(description.getKey()) | |
&& "dev".equals(description.getValue())) { | |
startResourceIds.add(description.getResourceId()); | |
} | |
} | |
if (!startResourceIds.isEmpty()) { | |
// インスタンスを起動する。 | |
ec2_us.startInstances(new StartInstancesRequest(startResourceIds)); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment