Sha256: a0387faa35b1bc2d1e81f4f05cfab051355eb2eb652aea4910a6c28028e352d6

Contents?: true

Size: 855 Bytes

Versions: 3

Compression:

Stored size: 855 Bytes

Contents

module Forger::Template::Helper::AmiHelper
  include Forger::AwsServices

  # Example:
  #
  #   latest_ami("ruby-2.5.0_*") => ami-122
  #
  # Equivalent aws cli test command:
  #
  #   $ aws ec2 describe-images --owners self --filters="Name=name,Values=ruby-2.5.0_*" | jq '.Images | length'
  #
  # Returns latest ami ami
  def latest_ami(query, owners=["self"])
    images = search_ami(query, owners)
    image = images.sort_by(&:name).reverse.first
    if image
      image.image_id
    else
      puts "latest_ami helper method could not find an AMI with the query of: #{query.inspect}".color(:red)
      exit 1
    end
  end

  def search_ami(query, owners=["self"])
    ec2.describe_images(
      owners: owners,
      filters: [
        {name: "name", values: [query]},
        {name: "state", values: ["available"]}
      ]
    ).images
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
forger-3.0.2 lib/forger/template/helper/ami_helper.rb
forger-3.0.1 lib/forger/template/helper/ami_helper.rb
forger-3.0.0 lib/forger/template/helper/ami_helper.rb