Skip to content

Commit df40e5d

Browse files
authored
Merge pull request #4 from tecracer-chef/fix/better-ec2-enumeration
Fix EC2 enumeration
2 parents 2a20a9d + a17bb32 commit df40e5d

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
## Version 0.2.1
6+
7+
- Fix EC2 instance enumeration to use paging
8+
59
## Version 0.2.0
610

711
- Add more parameter and status checks

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ You can build and install this gem on your local system as well via a Rake task:
2525
| `execution_timeout` | Maximum time until timeout | 60 |
2626
| `recheck_invocation` | Interval of rechecking AWS command invocation | 1.0 |
2727
| `recheck_execution` | Interval of rechecking completion of command | 1.0 |
28+
| `instance_pagesize` | Paging size for EC2 instance retrieval | 100 |
2829

2930
## Limitations
3031

lib/train-awsssm/connection.rb

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module TrainPlugins
77
module AWSSSM
88
class Connection < Train::Plugins::Transport::BaseConnection
99
attr_reader :instance_id, :options
10-
attr_writer :ssm, :ec2
10+
attr_writer :ssm, :ec2, :instances
1111

1212
def initialize(options)
1313
super(options)
@@ -85,8 +85,6 @@ def check_options
8585
def resolve_instance_id(address)
8686
logger.debug format("[AWS-SSM] Trying to resolve address %s", address)
8787

88-
instances = ec2.describe_instances.reservations.collect { |r| r.instances.first }
89-
9088
# Resolve, if DNS name and not Amazon default
9189
if dns_name?(address) && !amazon_dns?(address)
9290
address = Resolv.getaddress(address)
@@ -112,6 +110,28 @@ def resolve_instance_id(address)
112110
raise ArgumentError, format("Error looking up Instance ID for %s: %s", address, e.message)
113111
end
114112

113+
# List up EC2 instances in the account.
114+
#
115+
# @param [Boolean] cache Cache results
116+
# @return [Array] List of instances
117+
# @todo Implement paging
118+
def instances(caching: true)
119+
return @instances unless @instances.nil? || !caching
120+
121+
results = []
122+
123+
ec2_instances = ec2.describe_instances(max_results: options[:instance_pagesize])
124+
loop do
125+
results.concat ec2_instances.reservations.map(&:instances).flatten
126+
127+
break unless ec2_instances.next_token
128+
129+
ec2_instances = ec2.describe_instances(max_results: options[:instance_pagesize], next_token: ec2_instances.next_token)
130+
end
131+
132+
@instances = results
133+
end
134+
115135
# Check if this is an IP address
116136
#
117137
# @param [String] address Host, IP address or other input

lib/train-awsssm/transport.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class Transport < Train.plugin(1)
1111
option :execution_timeout, default: 60.0
1212
option :recheck_invocation, default: 1.0
1313
option :recheck_execution, default: 1.0
14+
option :instance_pagesize, default: 100
1415

1516
def connection(_instance_opts = nil)
1617
@connection ||= TrainPlugins::AWSSSM::Connection.new(@options)

lib/train-awsssm/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module TrainPlugins
22
module AWSSSM
3-
VERSION = "0.2.0".freeze
3+
VERSION = "0.2.1".freeze
44
end
55
end

0 commit comments

Comments
 (0)