Skip to content
This repository has been archived by the owner on Jun 5, 2020. It is now read-only.

Prefetch subnets info in one call - quite big performance boost #146

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/puppet/provider/ec2_instance/v2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,22 @@ def self.instances
regions.collect do |region|
begin
instances = []
subnets = Hash.new()

subnets_response = ec2_client(region).describe_subnets()
subnets_response.data.subnets.each do |subnet|
name_tag = subnet.tags.detect { |tag| tag.key == 'Name' }
if name_tag then
subnets[subnet.subnet_id] = name_tag.value
end
end

ec2_client(region).describe_instances(filters: [
{name: 'instance-state-name', values: ['pending', 'running', 'stopping', 'stopped']}
]).each do |response|
response.data.reservations.each do |reservation|
reservation.instances.each do |instance|
hash = instance_to_hash(region, instance)
hash = instance_to_hash(region, instance, subnets)
instances << new(hash) if has_name?(hash)
end
end
Expand All @@ -40,7 +50,7 @@ def self.prefetch(resources)
end
end

def self.instance_to_hash(region, instance)
def self.instance_to_hash(region, instance, subnets)
name_tag = instance.tags.detect { |tag| tag.key == 'Name' }
monitoring = instance.monitoring.state == "enabled" ? true : false
tags = {}
Expand All @@ -49,10 +59,8 @@ def self.instance_to_hash(region, instance)
end
subnet_name = nil
if instance.subnet_id
subnet_response = ec2_client(region).describe_subnets(subnet_ids: [instance.subnet_id])
subnet_name_tag = subnet_response.data.subnets.first.tags.detect { |tag| tag.key == 'Name' }
subnet_name = subnets[instance.subnet_id] ? subnets[instance.subnet_id] : nil
end
subnet_name = subnet_name_tag ? subnet_name_tag.value : nil

devices = instance.block_device_mappings.collect do |mapping|
{
Expand Down