@@ -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
0 commit comments