Last active
August 29, 2015 14:08
-
-
Save uzresk/63ef2550cb8d4a251084 to your computer and use it in GitHub Desktop.
EC2 EBSのsnapshotを取得する。[SDK for Java] ref: http://qiita.com/uzresk/items/77a7ebc037334609b728
This file contains 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 com.amazonaws.ClientConfiguration; | |
import com.amazonaws.auth.AWSCredentialsProvider; | |
import com.amazonaws.auth.profile.ProfileCredentialsProvider; | |
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.CreateSnapshotRequest; | |
import com.amazonaws.services.ec2.model.DescribeVolumesResult; | |
import com.amazonaws.services.ec2.model.Volume; | |
import com.amazonaws.services.ec2.model.VolumeAttachment; | |
public class Ec2EbsSnapshot { | |
/** | |
* 第一引数で指定されたEC2のインスタンスIDに紐づくEBSのsnapshotを作成します。 | |
* | |
* @param args[0] ec2 instance id | |
*/ | |
public static void main(String[] args) { | |
String ec2InstanceId = args[0]; | |
if (args.length != 1 || ec2InstanceId == null | |
|| "".equals(ec2InstanceId)) { | |
throw new IllegalArgumentException("EC2 instance id is null."); | |
} | |
AWSCredentialsProvider provider = new ProfileCredentialsProvider("uzr"); | |
AmazonEC2 ec2_tokyo = Region.getRegion(Regions.AP_NORTHEAST_1) | |
.createClient(AmazonEC2Client.class, provider, | |
new ClientConfiguration()); | |
DescribeVolumesResult volumes = ec2_tokyo.describeVolumes(); | |
String volumeId = null; | |
for (Volume volume : volumes.getVolumes()) { | |
for (VolumeAttachment volumeAttatchment : volume.getAttachments()) { | |
if (ec2InstanceId.equals(volumeAttatchment.getInstanceId())) { | |
volumeId = volumeAttatchment.getVolumeId(); | |
System.out.println(ec2InstanceId + " : " + volumeId); | |
break; | |
} | |
} | |
} | |
if (volumeId == null || "".equals(volumeId)) { | |
System.out.println(ec2InstanceId + "に対応するvolumeIdが見つかりません。"); | |
} else { | |
ec2_tokyo.createSnapshot(new CreateSnapshotRequest(volumeId, | |
volumeId + " snapshot.")); | |
System.out.println("snapshotの作成を要求しました。"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment