ruby/example_code/iam/iam-ruby-example-access-keys.rb
# Lists information about access keys for a user in
# AWS Identity and Access Management (IAM).
#
# Prerequisites:
# - The user in IAM.
#
# @param iam [Aws::IAM::Client] An initialized IAM client.
# @param user_name [String] The name of the user.
# @example
#   puts list_access_keys(Aws::IAM::Client.new, 'my-user')

Lists information about access keys for a user in
AWS Identity and Access Management (IAM).

The user in IAM.
Lists information about access keys for a user in AWS Identity and Access Management ( IAM ) . The user in IAM .
def list_access_keys(iam, user_name)
  response = iam.list_access_keys(user_name: user_name)
  if response.access_key_metadata.count.positive?
    puts 'Access key IDs:'
    response.access_key_metadata.each do |key_metadata|
      puts "  #{key_metadata.access_key_id}"
    end
  else
    puts "No access keys found for user '#{user_name}'."
  end
rescue Aws::IAM::Errors::NoSuchEntity
  puts "Error listing access keys: cannot find user '#{user_name}'."
  exit 1
rescue StandardError => e
  puts "Error listing access keys: #{e.message}"
end
def list_access_keys ( iam , user_name ) response = iam . list_access_keys ( user_name : user_name ) if response . access_key_metadata . count . positive? puts 'Access key IDs:' response . access_key_metadata . each do | key_metadata | puts "  #{key_metadata.access_key_id}" end else puts "No access keys found for user '#{user_name}'." end rescue Aws :: IAM :: Errors :: NoSuchEntity puts "Error listing access keys: cannot find user '#{user_name}'." exit 1 rescue StandardError => e puts "Error listing access keys: #{e.message}" end
Lists information about access keys for a user in
AWS Identity and Access Management (IAM).
Lists information about access keys for a user in AWS Identity and Access Management ( IAM ) .
iam
An initialized IAM client.
An initialized IAM client .
user_name
The name of the user.
The name of the user .
example
puts list_access_keys(Aws::IAM::Client.new, 'my-user')
puts list_access_keys ( Aws :: IAM :: Client . new ' my - user ' )