Skip to content

Instantly share code, notes, and snippets.

@Twinuma
Last active August 29, 2015 14:10
Show Gist options
  • Save Twinuma/6150d0fc160a29dbc657 to your computer and use it in GitHub Desktop.
Save Twinuma/6150d0fc160a29dbc657 to your computer and use it in GitHub Desktop.
特定のタグが付いているEC2インスタンスを起動
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