Created
October 25, 2012 12:26
-
-
Save thomaswitt/3952303 to your computer and use it in GitHub Desktop.
Find the ARN/AWS Account ID (whoami) for a given credential set via AWS Ruby SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk' | |
def get_aws_account_id(access_key_id, secret_access_key) | |
begin | |
iam = AWS::IAM.new(:access_key_id => access_key_id, | |
:secret_access_key => secret_access_key) | |
iam = iam.client.get_user | |
id, user = iam[:user][:arn].match('^arn:aws:iam::([0-9]{12}):(.*)$').captures | |
rescue AWS::IAM::Errors::AccessDenied | |
result = $! | |
id, user = result.to_s.match('^User: arn:aws:iam::([0-9]{12}):(.*) is not auth.*$').captures | |
end | |
raise 'incorrect account id' unless id.match(/^[0-9]{12}$/) | |
[id, user] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment