LoginSignup
3
3

More than 5 years have passed since last update.

最新のAmazon Linux AMIのIDを取得する

Posted at

CLIからEC2を起動!みたいなときに「えーっと、最新のAmazon LinuxのAMIのIDってどれだっけ・・・?」みたいな感じにManagement Consoleを覗くのはアレだよね。

ということでCLIで取得するツールを書いた。

latest_amazonlinux_ami.rb
#!/usr/bin/env ruby
require 'aws-sdk-core'

ec2 = Aws::EC2::Client.new(region:'ap-northeast-1')
images = ec2.describe_images(
    owners: ['amazon'],
    filters: [
        {
            name:"architecture",
            values:["x86_64"]
        },
        {
            name:"hypervisor",
            values:["xen"]
        }
    ]
)

latest_ami = images[:images].find_all{|image|
    image[:description]
}.find_all{|image|
        image[:description].match(/Amazon Linux/)
}.find_all{|image|
        image[:description].match(/EBS/)
}.find_all{|image|
        image[:description].match(/HVM/)
}.sort_by{|image|
    image[:creation_date]
}.reverse.first

puts latest_ami[:image_id]

どろくさい・・・ みんなどうしてるの?

Disclaimer

このポストは個人のメモであり、私の雇用主を代表するものではりません。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3