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



7
8
9
10
# File 'lib/hashicorptools/ec2_utilities.rb', line 7

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



3
4
5
# File 'lib/hashicorptools/ec2_utilities.rb', line 3

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

#ec2Object



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

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



28
29
30
# File 'lib/hashicorptools/ec2_utilities.rb', line 28

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



32
33
34
# File 'lib/hashicorptools/ec2_utilities.rb', line 32

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

#vpc_with_name(name) ⇒ Object



24
25
26
# File 'lib/hashicorptools/ec2_utilities.rb', line 24

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