Skip to content

Instantly share code, notes, and snippets.

@bhouse
Created August 16, 2016 17:31
Show Gist options
  • Save bhouse/f97980adc5df2b5db7fd435a6112c4d8 to your computer and use it in GitHub Desktop.
Save bhouse/f97980adc5df2b5db7fd435a6112c4d8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'aws-sdk'
$stdout.print 'Enter your MFA serial: '
mfa_serial = $stdin.gets.chomp
$stdout.print 'Enter your MFA code: '
mfa_code = $stdin.gets.chomp
$stdout.print 'Enter your creditials profile name: '
profile = $stdin.gets.chomp
sts = Aws::STS::Client.new(profile: profile, region: 'us-east-1')
sts_creds = sts.get_session_token(serial_number: mfa_serial, token_code: mfa_code, duration_seconds: 28800).credentials
creds_obj = Aws::Credentials.new(sts_creds.access_key_id, sts_creds.secret_access_key, sts_creds.session_token)
ec2 = Aws::EC2::Client.new(region: 'us-east-1', credentials: creds_obj)
puts ec2.describe_instances.reservations.first.instances.map { |i| i.instance_id }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment