获取有关 IAM 用户的信息 - 适用于 Ruby 的 AWS 开发工具包
AWS 文档中描述的 AWS 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 AWS 服务入门

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

获取有关 IAM 用户的信息

以下示例列出了 IDs 区域中的 IAM 用户的组、策略和访问密钥 us-west-2。如果有 100 个以上的用户,则 iam.list_users.IsTruncatedtrue,并且 iam.list_users.Marker 包含可用于获取有关其他用户的信息的值。请参阅 Aws::IAM::Client.list_users 主题以获取更多信息。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-iam' # Displays information about available users in # AWS Identity and Access Management (IAM) including users' # names, associated group names, inline embedded user policy names, # and access key IDs. # # @param iam_client [Aws::IAM::Client] An initialized IAM client. # @example # get_user_details(Aws::IAM::Client.new) def get_user_details(iam_client) users_response = iam_client.list_users if users_response.key?('users') && users_response.users.count.positive? # Are there more users available than can be displayed? if users_response.key?('is_truncated') && users_response.is_truncated puts '(Note: not all users are displayed here, ' \ "only the first #{users_response.users.count}.)" else puts "Found #{users_response.users.count} user(s):" end users_response.users.each do |user| name = user.user_name puts '-' * 30 puts "User name: #{name}" puts "Groups:" groups_response = iam_client.list_groups_for_user(user_name: name) if groups_response.key?('groups') && groups_response.groups.count.positive? groups_response.groups.each do |group| puts " #{group.group_name}" end else puts ' None' end puts 'Inline embedded user policies:' policies_response = iam_client.list_user_policies(user_name: name) if policies_response.key?('policy_names') && policies_response.policy_names.count.positive? policies_response.policy_names.each do |policy_name| puts " #{policy_name}" end else puts ' None' end puts 'Access keys:' access_keys_response = iam_client.list_access_keys(user_name: name) if access_keys_response.key?('access_key_metadata') && access_keys_response.access_key_metadata.count.positive? access_keys_response.access_key_metadata.each do |access_key| puts " #{access_key.access_key_id}" end else puts ' None' end end else puts 'No users found.' end rescue StandardError => e puts "Error getting user details: #{e.message}" end # Full example call: def run_me iam_client = Aws::IAM::Client.new puts 'Attempting to get details for available users...' get_user_details(iam_client) end run_me if $PROGRAM_NAME == __FILE__