Advertisement
Guest User

Untitled

a guest
May 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. #
  3. #
  4. # 自分が所有するSnapShotで、AMIがないものを探索するスクリプトです。
  5. # 要 AWS SDK for Ruby V2
  6. #
  7. require 'aws-sdk-core'
  8. require 'yaml'
  9. require 'pp'
  10.  
  11. config=YAML.load(File.read("config.yml"))
  12. Aws.config[:credentials] = Aws::Credentials.new(config['access_key_id'],config['secret_access_key'])
  13. ec2=Aws::EC2::Client.new(region:config['region'])
  14.  
  15. # 設定
  16. aws_owner_id = "<SnapShotのオーナーID>"
  17.  
  18. ec2.describe_snapshots({ owner_ids: [aws_owner_id] }).each_page do |res|
  19. res.snapshots.each do | snap |
  20. ami_id = snap.description.match(/ami-[0-9a-zA-Z]{0,}/) # descriptionからAMIを割り出す。
  21. instance_id = snap.description.match(/i-[0-9a-zA-Z]{0,}/)
  22. if ami_id != nil
  23. begin
  24. ec2.describe_images({
  25. image_ids: [ami_id[0]],
  26. })
  27. rescue Aws::EC2::Errors::InvalidAMIIDNotFound
  28. # AMIが存在しない。
  29. pp "SnapShotID:#{snap.snapshot_id} is Not Found AMI #{ami_id[0]} Instance ID:#{instance_id[0]}"
  30. end
  31. end
  32. end
  33. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement