Search code examples
amazon-web-servicesfog

Make AWS AMI public by fog


I am using fog to create an ami from running instance:

@fog = Fog::Compute.new(
        :provider => 'AWS',
        :region => 'us-west-2',
        :aws_access_key_id => aws_access_key,
        :aws_secret_access_key => aws_secret_key
    )
@fog.create_image(instance.identity,image_name,image_description)

Can I use fog to make this ami public?


Solution

  • I've found how to do it, I need to add one line to the code above:

    @fog.modify_image_attribute(image_id,{'Add.Group' => ['all']}
    

    If the image has status 'pending' (like in my case), it will throw an exception, so the final modification is:

    @fog = Fog::Compute.new(
            :provider => 'AWS',
            :region => 'us-west-2',
            :aws_access_key_id => aws_access_key,
            :aws_secret_access_key => aws_secret_key
        )
    data = @fog.create_image(instance.identity,image_name,image_description)
    
    image_id = data.body['imageId']
    
    print 'Waiting ami to come up'
    
    begin
      @fog.modify_image_attribute(image_id,{'Add.Group' => ['all']})
    rescue
      print(".")
      sleep(10)
      retry
    end
    
    puts 'READY!'