Managing and Sharing AMIs

Image management is an advanced subject that will be discussed in detail in Chapter 6. For the time being, we will merely summarize the API operations that are available to manage your AMIs in EC2.

The RegisterImage operation outlined in Table 5-24 registers an AMI to make it available in the EC2 environment. Before it can be registered, the AMI must first be bundled into image files and uploaded to S3 with a manifest XML file.

Here is an XML document returned by the operation. It is very simple, containing only the element imageId which contains a unique identifier for the AMI that has been registered.

<RegisterImageResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <imageId>ami-d39075ba</imageId>
</RegisterImageResponse>

Example 5-17 defines a method to register an AMI that is described by an image manifest file stored in S3. The method returns the unique identifier value assigned to the registered AMI.

The DeregisterImage operation shown in Table 5-25 deregisters an image. You must deregister an image if you have removed the bundled image files from S3 or if you have updated these image files to store a new version of your AMI.

Warning

Do not forget to deregister an image if you make any changes to it. Failing to do so can lead to odd situations in which the old image can still be launched in some circumstances and not in others.

Here is an XML document returned by the operation. It is very simple, containing only the element return, which contains a Boolean value indicating whether the operation was successful.

<DeregisterImageResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <return>true</return>
</DeregisterImageResponse>

A method that deregisters an AMI image is shown in Example 5-18.

An AMI registered for use in EC2 can have attributes associated with it. These attributes may be listed, modified, and reset by the AMI’s owner using API operations. Image attributes are used for two purposes: sharing images and setting product codes.

The ModifyImageAttribute operation modifies the attributes associated with an AMI. The modification operation can either add or remove attribute values.

Table 5-26 lists the mandatory parameters required to modify an attribute for an AMI image. Depending on which attribute you are modifying, you must also provide additional parameters.

Table 5-27 lists the additional query parameters that are necessary to modify the launchPermission image attribute.

Table 5-28 lists the additional query parameters that are necessary to modify the productCodes image attribute.

Here is an XML document returned by the operation. It is very simple as it includes only the element return, which contains a Boolean value indicating whether the operation was successful.

<ModifyImageAttributeResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <return>true</return>
</ModifyImageAttributeResponse>

Example 5-19 defines a method that adds attributes to, or removes them from, an AMI registered in EC2.

If you wish to allow other people to start an AMI you own, you can use the modify_image_attribute method to define the set of EC2 users allowed to run your AMI. You specify which users to allow using their EC2 Account IDs. The commands below demonstrate how to permit EC2 users 123456789012 and 210987654321 to run the AMI with ID ami-889075e1.

irb> ec2.modify_image_attribute('ami-889075e1', 'launchPermission', 'add', 
                               {'UserId'=>['123456789012','210987654321']})

To allow all EC2 users to run your image—that is, to make your AMI publicly accessible—you would add a launchPermission attribute specifying the user group named all.

irb> ec2.modify_image_attribute('ami-889075e1', 'launchPermission', 'add', 
                                {'UserGroup'=>'all'})

The DescribeImageAttribute operation, outlined in Table 5-29, lists the attribute settings that apply to your AMI. The response document returned by this operation will vary according to the attribute you ask to be described.

Here is an XML document returned when you request details about the launchPermission attribute. This document includes a listing of permission items, containing either the number (userId) of the user who has been granted permission to launch the AMI, or the name of a group that has been granted that permission. If no permissions have been granted, the launchPermission element will be empty.

<DescribeImageAttributeResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <imageId>ami-889075e1</imageId>
  <launchPermission>
    <item>
      <userId>123456789012</userId>
    </item>
    <item>
      <group>all</group>
    </item>
  </launchPermission>
</DescribeImageAttributeResponse>

Here is an XML document returned when you request details about the productCodes attribute. This document includes a listing of product code strings inside item/productCode elements.

<DescribeImageAttributeResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <imageId>ami-889075e1</imageId>
  <productCodes>
    <item>
      <productCode>123456789012</productCode>
    </item>
  </productCodes>
</DescribeImageAttributeResponse>

Example 5-20 defines a method that retrieves the value of an attribute that applies to an AMI and interprets the response document. The method can interpret the details for both the productCodes and launchPermission attributes. The product codes associated with an AMI are returned in the :product_codes hash item, while user and group launch permissions are returned in the :launch_perms_user and :launch_perms_group hash items respectively.

We can use this method to list the launch permissions for our AMI or the product codes associated with it.

# List the launch permissions for our AMI (2 user perms, 1 group perm)
irb> ec2.describe_image_attribute('ami-889075e1', 'launchPermission')
{:id=>"ami-889075e1",
 :launch_perms_group=>["all"],
 :launch_perms_user=>["210987654321", "123456789012"]}
 
# List the product codes associated for our AMI
irb> puts ec2.describe_image_attribute('ami-889075e1', 'productCodes')
{:product_codes=>[], :id=>"ami-889075e1"}

The RestImageAttribute operation detailed in Table 5-30 resets an AMI’s attribute value back to its default setting.

Here is an XML document returned by the operation. It is very simple, containing only the element return, which contains a Boolean value indicating whether the operation was successful.

<ResetImageAttributeResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <return>true</return>
</ResetImageAttributeResponse>

Example 5-21 defines a method that resets the named attribute to its default value. Unless you specify a different attribute name, the method will reset the launchPermission attribute.

Resetting the launchPermission image attribute will remove the fake user launch permission we added earlier.

# Reset the launchPermission attribute
irb> ec2.reset_image_attribute('ami-889075e1', 'launchPermission')
=> true

# Confirm that all the launch permissions have been removed
irb> ec2.describe_image_attribute('ami-889075e1', 'launchPermission')
=> {:launch_perms_group=>[], :id=>"ami-889075e1", :launch_perms_user=>[]}

The ConfirmProductInstance operation shown in Table 5-31 checks whether a running instance is based on an AMI with a given product code; if it is, the operation returns the identifier of the instance’s owner.

This operation is only useful to EC2 developers who have sold an AMI to a customer with a support agreement and an associated product code. In this circumstance, it allows you to confirm that the instance a customer is running is eligible for support.

Here is an XML document returned by the operation:

<ConfirmProductInstanceResponse xmlns='http://ec2.amazonaws.com/doc/2007-08-29/'>
  <result>true</result>
  <ownerId>123456789012</ownerId>
</ConfirmProductInstanceResponse>

The response document from the operation includes the confirmation information shown in Table 5-32.

Example 5-22 defines a method that confirms whether a running instance is based on an AMI with a product code attribute that matches a specific value.