Skip to content

Instantly share code, notes, and snippets.

@viatcheslavmogilevsky
Created September 18, 2024 14:35
Show Gist options
  • Save viatcheslavmogilevsky/28ff87e8b1ed76b5d589f509c9295dd4 to your computer and use it in GitHub Desktop.
Save viatcheslavmogilevsky/28ff87e8b1ed76b5d589f509c9295dd4 to your computer and use it in GitHub Desktop.
Ruby AWS client example
source "https://rubygems.org"
gem 'aws-sdk-ec2', '~> 1'
gem 'aws-sdk-autoscaling', '~> 1'
gem 'base64'
gem 'nokogiri'
gem 'terminal-table'
#!/usr/bin/env ruby
require 'aws-sdk-autoscaling'
require 'aws-sdk-ec2'
require 'terminal-table'
auto_scaling_groups = []
cluster_id = "example-cluster"
get_launch_key = ->(g) {[g.launch_configuration_name, g.launch_template&.launch_template_name, g.launch_template&.version].compact.map(&:to_s).join(';')}
asg_client = Aws::AutoScaling::Client.new
ec2_client = Aws::EC2::Client.new
#
asg_client.describe_auto_scaling_groups({filters: [{name: "tag:k8s.io/cluster/#{cluster_id}", values: ["owned"]}]}).each do |resp|
auto_scaling_groups += resp.auto_scaling_groups.map do |asg|
asg_result = {
auto_scaling_group_name: asg.auto_scaling_group_name,
}
launch_key = get_launch_key.call(asg)
asg_result[:launch_key] = launch_key
asg_result[:instance_ids] = asg.instances.map(&:instance_id)
instances_by_launch_key = { "#{launch_key}" => 0 }
asg.instances.each do |i|
lki = get_launch_key.call(i)
if !instances_by_launch_key[lki]
instances_by_launch_key[lki] = 1
else
instances_by_launch_key[lki] += 1
end
end
asg_result[:outliers_count] = 0
instances_by_launch_key.keys.reject { |key| key == asg_result[:launch_key] }.each do |key|
asg_result[:outliers_count] += instances_by_launch_key[key]
end
asg_result[:instances_by_launch_key] = instances_by_launch_key
asg_result[:desired] = asg.desired_capacity
asg_result[:max] = asg.max_size
asg_result[:autoscaler_tag] = asg.tags.select {|t| t.key == "k8s.io/cluster-autoscaler/enabled" }.fetch(0, {value: "false"})[:value] == "true"
asg_result
end
end
asg_rows = auto_scaling_groups.map do |asg|
[
asg[:auto_scaling_group_name],
asg[:launch_key],
asg[:instances_by_launch_key].keys.map {|key| "#{key}=#{asg[:instances_by_launch_key][key]}"}.join('\n'),
"#{asg[:desired]}/#{asg[:max]}",
asg[:autoscaler_tag],
asg[:outliers_count]
]
end
asg_table = Terminal::Table.new :title => "Auto Scaling Groups",
:headings => ['Name', 'Current launch conf/template', 'Instances by launch conf/template', 'Desired/Max', 'Autoscaler tag', 'Outliers count'],
:rows => asg_rows
# puts(auto_scaling_groups)
puts(asg_table)
auto_scaling_groups.each do |asg|
if asg[:instance_ids].empty?
puts "#{asg[:auto_scaling_group_name]} has no instances. Skipping..."
next
end
full_instances = []
ec2_client.describe_instances({instance_ids: asg[:instance_ids]}).each do |resp|
full_instances += resp.reservations.map {|rsv| rsv.instances }.flatten.map do |inst|
instance_result = {
instance_id: inst.instance_id,
status: inst.state.name,
image_id: inst.image_id,
private_dns_name: inst.private_dns_name
}
end
end
instance_rows = full_instances.map do |inst|
[
inst[:instance_id],
inst[:status],
inst[:image_id],
inst[:private_dns_name]
]
end
instance_table = Terminal::Table.new :title => "Instances of #{asg[:auto_scaling_group_name]}",
:headings => ['ID', 'Status', 'Image', 'Private DNS name'],
:rows => instance_rows
puts(instance_table)
end
# puts
# puts(resp.to_h)
# Columns:
#
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/PageableResponse.html
# https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/AutoScaling/Client.html#describe_auto_scaling_groups-instance_method
# https://github.com/tj/terminal-table
# while response.next_page? do
# response = response.next_page
# # Use the response data here...
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment