管理 IAM 账户别名 - 适用于 Ruby 的 Amazon 开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

管理 IAM 账户别名

如果您希望在登录页面的 URL 用贵公司名称或其他友好标识符取代您的Amazon账户 ID,您可以为Amazon账户 ID。如果您创建 IAM 账户别名,您的登录页面 URL 将更改以包含该别名。有关 IAM 账户别名的更多信息,请参阅您的Amazon账户 ID 及其别名

在此示例中,您使用Amazon适 SDK for Ruby 可以:

  1. 列表Amazon帐户别名,使用AWS::IAM::Client #list_account_aliases

  2. 使用 Aws::IAM::Client#create_account_alias 创建账户别名。

  3. 使用 Aws::IAM::Client#delete_account_alias 删除账户别名。

Prerequisites

在运行示例代码之前,您需要安装并配置Amazon适 SDK for Ruby,如下所述:

在示例代码中,将 my-account-alias 字符串更改为在所有 Amazon Web Services 产品中唯一的字符串。

Example

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 # The following code example shows how to: # 1. List available AWS account aliases. # 2. Create an account alias. # 3. Delete an account alias. require 'aws-sdk-iam' # Lists available AWS account aliases. # # @param iam [Aws::IAM::Client] An initialized IAM client. # @example # puts list_aliases(Aws::IAM::Client.new) def list_aliases(iam) response = iam.list_account_aliases if response.account_aliases.count.positive? response.account_aliases.each do |account_alias| puts " #{account_alias}" end else puts 'No account aliases found.' end rescue StandardError => e puts "Error listing account aliases: #{e.message}" end # Creates an AWS account alias. # # @param iam [Aws::IAM::Client] An initialized IAM client. # @param account_alias [String] The name of the account alias to create. # @return [Boolean] true if the account alias was created; otherwise, false. # @example # exit 1 unless alias_created?(Aws::IAM::Client.new, 'my-account-alias') def alias_created?(iam, account_alias) iam.create_account_alias(account_alias: account_alias) return true rescue StandardError => e puts "Error creating account alias: #{e.message}" return false end # Deletes an AWS account alias. # # @param iam [Aws::IAM::Client] An initialized IAM client. # @param account_alias [String] The name of the account alias to delete. # @return [Boolean] true if the account alias was deleted; otherwise, false. # @example # exit 1 unless alias_deleted?(Aws::IAM::Client.new, 'my-account-alias') def alias_deleted?(iam, account_alias) iam.delete_account_alias(account_alias: account_alias) return true rescue StandardError => e puts "Error deleting account alias: #{e.message}" return false end # Full example call: def run_me iam = Aws::IAM::Client.new account_alias = 'my-account-alias' create_alias = true # Change to false to not generate an account alias. delete_alias = true # Change to false to not delete any generated account alias. puts 'Account aliases are:' list_aliases(iam) if create_alias puts 'Attempting to create account alias...' if alias_created?(iam, account_alias) puts 'Account alias created. Account aliases now are:' list_aliases(iam) else puts 'Account alias not created. Stopping program.' exit 1 end end if create_alias && delete_alias puts 'Attempting to delete account alias...' if alias_deleted?(iam, account_alias) puts 'Account alias deleted. Account aliases now are:' list_aliases(iam) else puts 'Account alias not deleted. You will need to delete ' \ 'the alias yourself.' end end end run_me if $PROGRAM_NAME == __FILE__