Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please use AWS Ruby SDK instead of the AWS CLI #25

Open
dhs-rec opened this issue Feb 16, 2021 · 3 comments
Open

Please use AWS Ruby SDK instead of the AWS CLI #25

dhs-rec opened this issue Feb 16, 2021 · 3 comments

Comments

@dhs-rec
Copy link

dhs-rec commented Feb 16, 2021

I wonder about the usage of the AWS CLI here. This is all Ruby code, so why not use the Ruby SDK instead of pulling in a Python application? This would also handle the retries in case of rate limits (see commit 57b3cd6) automatically and with exponential backoff, if the client was initialized accordingly, for example

ec2       = Aws::EC2::Client.new(
  credentials: Aws::SharedCredentials.new(profile_name: some_profile),
  region:      region,
  retry_limit: 20
)

I didn't run into API rate limits anymore since I added the retry_limit: 20 part some years ago...

@dhs-rec
Copy link
Author

dhs-rec commented Feb 19, 2021

Here's a complete Ruby-only example for getting the tags of the instance the script is running on:

#!/usr/bin/ruby

require 'net/http'
require 'aws-sdk-ec2'

begin
  # No need for URI here
  http = Net::HTTP.new('169.254.169.254', 80)

  # Get instance ID
  instance_id = http.get('/latest/meta-data/instance-id').body

  # Get region (via availability zone)
  region = http.get('/latest/meta-data/placement/availability-zone').body[0..-2]
rescue
  puts 'Not an AWS instance'
end

# Initialize EC2 client (uses exponential backoff by default for retries)
ec2 = Aws::EC2::Client.new(
  region: region,
  retry_limit: 20,
)

# Get the tags
ec2.describe_tags({filters: [{ name: 'resource-id', values: [instance_id]}]}).tags.each do |tag|
  puts "\t" + tag[:key] + ' = ' + tag[:value]
end

@jimsheldon
Copy link

We have a desire to use this module, so we did some work on this. I have opened #28 which adds this feature.

@dhs-rec
Copy link
Author

dhs-rec commented Sep 27, 2021

BTW, two things can be changed in the example code above:

  1. The region can now be obtained directly (via .../placement/region)
  2. While still available, exponential back-off might not be the default retry strategy anymore (see SDK documentation for details).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants