Module: Forger::Template::Helper::AmiHelper

Includes:
AwsServices
Included in:
Forger::Template::Helper
Defined in:
lib/forger/template/helper/ami_helper.rb

Instance Method Summary collapse

Methods included from AwsServices

#cfn, #ec2, #s3

Instance Method Details

#latest_ami(query, owners = ["self"]) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
# File 'lib/forger/template/helper/ami_helper.rb', line 13

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

#search_ami(query, owners = ["self"]) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/forger/template/helper/ami_helper.rb', line 24

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