Ruby AWS SDK | How to Describe a Machine Image

Use Ruby AWS SDK to Describe a Machine Image

You will need to describe a machine image to find out

  • its kernel id
  • the ramdisk id and
  • the virtualization type

We use the Ruby AWS SDK to print out the key attributes of the amazon machine image (ami)

Pre-Conditions – The script assumes you have the Ruby interpreter installed and valid AWS credentials set (usually as environment variables).

Describe Machine Image Code

Put the Ruby script into a file named desccribe-ami.rb and then you will be able to run it with this command.

$ ruby describe-ami.rb ami-a8d2d7ce ami-3afff65c

Ruby AWS SDK | Describe Image

#!/usr/bin/ruby

system "gem install aws-sdk"
require "aws-sdk"
Aws.use_bundled_cert!

example_call = "$=> ruby describe-ami.rb ami-a8d2d7ce ami-3afff65c"
error_desc = "\n\nUsage error => Script needs a [SPACE] separated list of amazon machine image ids.\n"
raise error_desc unless ARGV.length > 0

class MachineImage

  # -- ---------------------------------------------------------------------------- -- #
  # -- Use parameter space separated image id list to log pertinent ami attributes. -- #
  # -- ---------------------------------------------------------------------------- -- #
  def log_machine_images image_id_list

    ami_array = Aws::EC2::Client.new.describe_images({ image_ids: image_id_list }).images
    ami_array.each do |machine_image|

      log_machine_image machine_image

    end

  end

  # -- ------------------------------------------------------------------------ -- #
  # -- Log the pertinent attributes of the registered amazon ec2 machine image. -- #
  # -- ------------------------------------------------------------------------ -- #
  def log_machine_image ami

    puts
    puts "# --- ---------------------------------------------------------------------- --- #"
    puts "# --- [EC2 MACHINE IMAGE] Vital attributes of the ec2 machine image.         --- #"
    puts "# --- ---------------------------------------------------------------------- --- #"
    puts "# --- Machine Image ID   :-] #{ami.image_id}"
    puts "# --- AMI Location Path  :-] #{ami.image_location}"
    puts "# --- Availability State :-] #{ami.state}"
    puts "# --- ID of Image Owner  :-] #{ami.owner_id}"
    puts "# --- Date AMI Created   :-] #{ami.creation_date}"
    puts "# --- Base Architecture  :-] #{ami.architecture}"
    puts "# --- Type of Image      :-] #{ami.image_type}"
    puts "# --- ---------------------------------------------------------------------- --- #"
    puts "# --- Kernel ID          :-] #{ami.kernel_id}"
    puts "# --- RamDisk Id         :-] #{ami.ramdisk_id}"
    puts "# --- ---------------------------------------------------------------------- --- #"
    puts "# --- Operating System   :-] #{ami.platform}"
    puts "# --- Operating Sys Name :-] #{ami.name}"
    puts "# --- Op Sys Description :-] #{ami.description}"
    puts "# --- The Storage Volume :-] #{ami.root_device_type}"
    puts "# --- The Root Partition :-] #{ami.root_device_name}"
    puts "# --- HVM or ParaVirtual :-] #{ami.virtualization_type}"
    puts "# --- Name of Hypervisor :-] #{ami.hypervisor}"
    puts "# --- ---------------------------------------------------------------------- --- #"
    puts

  end

end

# -- ------------------------------------------------------------------------ -- #
# -- Calling on the MachineImage class to log attributes of parameter images. -- #
# -- ------------------------------------------------------------------------ -- #
MachineImage.new.log_machine_images ARGV

Machine Image Description Text

If all goes well the output will be similar to the below.

# — ———————————————————————- — #
# — [EC2 MACHINE IMAGE] Vital attributes of the ec2 machine image. — #
# — ———————————————————————- — #
# — Machine Image ID   :-] ami-3afff65c
# — AMI Location Path  :-] amazon/Windows_Server-2016-English-Full-SQL_2016 …
# — Availability State :-] available
# — ID of Image Owner  :-] 801119661308
# — Date AMI Created   :-] 2017-05-14T00:07:39.000Z
# — Base Architecture  :-] x86_64
# — Type of Image      :-] machine
# — ———————————————————————- — #
# — Kernel ID          :-]
# — RamDisk Id         :-]
# — ———————————————————————- — #
# — Operating System   :-] windows
# — Operating Sys Name :-] Windows_Server-2016-English-Full-SQL_2016_SP1_Express …
# — Op Sys Description :-] Microsoft Windows Server 2016 Locale English with …
# — The Storage Volume :-] ebs
# — The Root Partition :-] /dev/sda1
# — HVM or ParaVirtual :-] hvm
# — Name of Hypervisor :-] xen
# — ———————————————————————- — #

# — ———————————————————————- — #
# — [EC2 MACHINE IMAGE] Vital attributes of the ec2 machine image. — #
# — ———————————————————————- — #
# — Machine Image ID   :-] ami-a8d2d7ce
# — AMI Location Path  :-] 099720109477/ubuntu/images/hvm-ssd/ubuntu-xenial-16.04 …
# — Availability State :-] available
# — ID of Image Owner  :-] 099720109477
# — Date AMI Created   :-] 2017-04-14T16:48:48.000Z
# — Base Architecture  :-] x86_64
# — Type of Image      :-] machine
# — ———————————————————————- — #
# — Kernel ID          :-]
# — RamDisk Id         :-]
# — ———————————————————————- — #
# — Operating System   :-]
# — Operating Sys Name :-] ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20170414
# — Op Sys Description :-] Canonical, Ubuntu, 16.04 LTS, amd64 xenial image …
# — The Storage Volume :-] ebs
# — The Root Partition :-] /dev/sda1
# — HVM or ParaVirtual :-] hvm
# — Name of Hypervisor :-] xen
# — ———————————————————————- — #

Leave a Reply

Your email address will not be published. Required fields are marked *