Module: Ec2Ssh::Cli::Aws

Defined in:
lib/ec2-ssh/aws.rb

Instance Method Summary collapse

Instance Method Details

#aws_init(profile, region) ⇒ Object



3
4
5
6
7
8
9
10
11
12
# File 'lib/ec2-ssh/aws.rb', line 3

def aws_init(profile, region)
  ENV['AWS_PROFILE'] = profile
  Aws.config.update({region: region})

  @region = region
  @as = Aws::AutoScaling::Client.new()
  @ec2 = Aws::EC2::Client.new()

  say "Currently running AWS User is: #{Aws::IAM::CurrentUser.new.arn}"
end

#get_auto_scale_groupsObject



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/ec2-ssh/aws.rb', line 14

def get_auto_scale_groups
  say "Fetching AutoScale Groups - please wait..."
    @as_groups = @as.describe_auto_scaling_groups({max_records: 100}).auto_scaling_groups

    as_group_names = @as_groups.inject([]) {|acc, asg| acc << asg.auto_scaling_group_name; acc }

    as_selection = {}
    as_group_names.each_with_index.inject(as_selection) {|acc, pair|
      element, index = pair
      as_selection[index] = element
      acc
    } 
    
    say "AutoScale Group in #{options[:region]}:\n"
    as_selection.each {|k,v| say "#{k}: #{v}"}

    selected_as = ask("Which server group do you want to ssh to?", color = :yellow)
    
    get_instances('aws:autoscaling:groupName', as_selection[selected_as.to_i])
end

#get_instances(tag_key, tag_value) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ec2-ssh/aws.rb', line 35

def get_instances(tag_key, tag_value)
  @all_servers = []

  say "Fetching instances with #{tag_key}: #{tag_value}", color = :white
  response = @ec2.describe_instances({
    filters: [
    {
      name: 'instance-state-code',
      values: ['16']

    },{
      name: 'tag-key',
      values: ["#{tag_key}"]
    },{
      name: 'tag-value',
      values: ["#{tag_value}"]
    }
  ]
  })

  if !response.reservations.empty?
    response.reservations.each {|r| r.instances.inject(@all_servers){|acc, k| 
      if k.public_ip_address.nil?
        say "Could not find public ip address for instance: #{k.instance_id}, falling back to private ip address", color = :yellow
        acc << k.private_ip_address; acc
      else
        acc << k.public_ip_address; acc
      end
    }}
  else
    say "could not find any instances with the tag #{tag_key}: #{tag_value} on #{@region}"
  end
  say "All servers: #{@all_servers}"
end