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

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

管理 IAM 账户别名

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

在此示例中,您将带有 IAM 的 Ruby Amazon 开发工具包用于:

  1. 使用 Aws:: IAM:: Client #list_account_aliases 列出 Amazon 账户别名。

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

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

先决条件

在运行示例代码之前,您需要安装和配置适用于 Ruby 的 Amazon SDK,如中所述:

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

示例

# 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__