Skip to content

Instantly share code, notes, and snippets.

@masaomoc
Last active August 29, 2015 14:06
Show Gist options
  • Save masaomoc/842053a2311d0b83c37d to your computer and use it in GitHub Desktop.
Save masaomoc/842053a2311d0b83c37d to your computer and use it in GitHub Desktop.
get RDS ARN for tags API
require 'aws-sdk'
# return 12 digits account number
# if failed, raise RuntimeError
def get_account_id
iam = AWS::IAM::Client.new
begin
iam.get_user.user.arn =~ /arn:aws:iam::(\d{12}):user\/.*/
rescue => e
e.message =~ /arn:aws:iam::(\d{12}):user\/.*/
end
raise RuntimeError if $1.nil?
$1
end
# return Array of RDS ARN
def get_rds_arn
rds = AWS::RDS.new
account_id = get_account_id
rds.instances.map{ |i| "arn:aws:rds:#{AWS.config.region}:#{account_id}:db:#{i.db_instance_identifier}" }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment