Skip to content

Instantly share code, notes, and snippets.

@thomaswitt
Created October 25, 2012 12:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thomaswitt/3952303 to your computer and use it in GitHub Desktop.
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
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