Module: Hashicorptools::Ec2Utilities

Included in:
Packer
Defined in:
lib/hashicorptools/ec2_utilities.rb

Instance Method Summary collapse

Instance Method Details

#amis(tag = tag_name) ⇒ Object



9
10
11
12
# File 'lib/hashicorptools/ec2_utilities.rb', line 9

def amis(tag = tag_name)
  images = ec2.describe_images({owners: ['self'], filters: [{name: 'tag:Name', values: [tag]}]}).images
  sort_by_created_at(images)
end

#current_ami(tag = tag_name) ⇒ Object



5
6
7
# File 'lib/hashicorptools/ec2_utilities.rb', line 5

def current_ami(tag = tag_name)
  amis(tag).first
end

#ec2Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/hashicorptools/ec2_utilities.rb', line 14

def ec2
  return @_ec2 unless @_ec2.nil?

  reg = if self.methods.include?(:region)
          self.region
        else
          'us-east-1'
        end

  @_ec2 = Aws::EC2::Client.new(region: reg)
end

#internet_gateway_for_vpc(vpc_id) ⇒ Object



30
31
32
# File 'lib/hashicorptools/ec2_utilities.rb', line 30

def internet_gateway_for_vpc(vpc_id)
  ec2.describe_internet_gateways({filters: [{name: 'attachment.vpc-id', values: [vpc_id]}]}).internet_gateways.first
end

#sort_by_created_at(collection) ⇒ Object



34
35
36
# File 'lib/hashicorptools/ec2_utilities.rb', line 34

def sort_by_created_at(collection)
  collection.sort{|a, b| b.creation_date <=> a.creation_date }
end

#vpc_with_name(name) ⇒ Object



26
27
28
# File 'lib/hashicorptools/ec2_utilities.rb', line 26

def vpc_with_name(name)
  ec2.describe_vpcs({filters: [{name: 'tag:Name', values: [name]}]}).vpcs.first
end