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

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

添加新 IAM 用户

以下示例在 us-west-2 区域中使用密码 REPLACE_ME 创建 IAM 用户 my_groovy_user,并显示用户的账户 ID。如果该名称的用户已存在,则会显示一条消息,并且不会创建新用户。

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX - License - Identifier: Apache - 2.0 require 'aws-sdk-iam' # Creates a user in AWS Identity and Access Management (IAM). # # @param iam [Aws::IAM::Client] An initialized IAM client. # @param user_name [String] The name of the user. # @param initial_password [String] The initial password for the user. # @return [String] The ID of the user if the user was created, otherwise; # the string 'Error'. # @example # puts create_user(Aws::IAM::Client.new, 'my-user', 'my-!p@55w0rd!') def create_user(iam_client, user_name, initial_password) response = iam_client.create_user(user_name: user_name) iam_client.wait_until(:user_exists, user_name: user_name) iam_client.create_login_profile( password: initial_password, password_reset_required: true, user_name: user_name ) return response.user.user_id rescue Aws::IAM::Errors::EntityAlreadyExists puts "Error creating user '#{user_name}': user already exists." return 'Error' rescue StandardError => e puts "Error creating user '#{user_name}': #{e.message}" return 'Error' end # Full example call: def run_me user_name = 'my-user' initial_password = 'my-!p@55w0rd!' iam_client = Aws::IAM::Client.new puts "Attempting to create user '#{user_name}'..." user_id = create_user(iam_client, user_name, initial_password) if user_id == 'Error' puts 'User not created.' else puts "User '#{user_name}' created with ID '#{user_id}' and initial " \ "sign-in password '#{initial_password}'." end end run_me if $PROGRAM_NAME == __FILE__