Skip to content

Instantly share code, notes, and snippets.

@DoctahPopp871
Created December 6, 2016 19:15
Show Gist options
  • Save DoctahPopp871/9f2044f6aaac40d7ec74a02aa037ba51 to your computer and use it in GitHub Desktop.
Save DoctahPopp871/9f2044f6aaac40d7ec74a02aa037ba51 to your computer and use it in GitHub Desktop.
Assume Role ruby SDK. Assume role and call specific sdk method using commandline opts.
#!/usr/bin/env ruby
require 'rubygems'
require 'optparse'
require 'aws-sdk-core'
require 'pry'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: assume_role_creds-rb [options]"
opts.on("--rolearn ROLE", "define the account delegation role arn, eg. arn:aws:iam::99999999:root") do |value|
options[:role_arn] = value
end
opts.on("--session SESSION", "specify a name for the elevated session, eg. Develop") do |value|
options[:session] = value
end
opts.on("--region REGION", "specify the AWS region, e.g. us-west-2") do |value|
options[:region] = value
end
opts.on("--apiclient API", "specify the api you would like to work with. ex. ec2, dynamodb") do |value|
options[:api] = value
end
opts.on("-h", "--help") do
puts opts
exit
end
end.parse!
role_arn = options[:role_arn]
role_session_name = options[:session]
aws_region = options[:region]
api = []
assumed_creds = Aws::AssumeRoleCredentials.new(client: Aws::STS::Client.new(region: aws_region), role_arn: role_arn, role_session_name: role_session_name)
if options[:api] == 'ec2'
ec2api = Aws::EC2::Client.new(region: aws_region, credentials: assumed_creds)
ec2api.pry
end
if options[:api] == 'dynamodb'
dynamoapi = Aws::DynamoDB::Client.new(region: aws_region, credentials: assumed_creds)
dynamoapi.pry
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment