Created
May 25, 2016 12:30
-
-
Save akira345/a5363c1aa4f84bc5b117ac1efc957c25 to your computer and use it in GitHub Desktop.
自分が所有するSnapShotで、AMIがないものを探索するスクリプトです。
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
# -*- coding: utf-8 -*- | |
# | |
# | |
# 自分が所有するSnapShotで、AMIがないものを探索するスクリプトです。 | |
# 要 AWS SDK for Ruby V2 | |
# | |
require 'aws-sdk-core' | |
require 'yaml' | |
require 'pp' | |
config=YAML.load(File.read("config.yml")) | |
Aws.config[:credentials] = Aws::Credentials.new(config['access_key_id'],config['secret_access_key']) | |
ec2=Aws::EC2::Client.new(region:config['region']) | |
# 設定 | |
aws_owner_id = "<SnapShotのオーナーID>" | |
ec2.describe_snapshots({ owner_ids: [aws_owner_id] }).each_page do |res| | |
res.snapshots.each do | snap | | |
ami_id = snap.description.match(/ami-[0-9a-zA-Z]{0,}/) # descriptionからAMIを割り出す。 | |
instance_id = snap.description.match(/i-[0-9a-zA-Z]{0,}/) | |
if ami_id != nil | |
begin | |
ec2.describe_images({ | |
image_ids: [ami_id[0]], | |
}) | |
rescue Aws::EC2::Errors::InvalidAMIIDNotFound | |
# AMIが存在しない。 | |
pp "SnapShotID:#{snap.snapshot_id} is Not Found AMI #{ami_id[0]} Instance ID:#{instance_id[0]}" | |
end | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment