Adding a New IAM User - Amazon SDK for Ruby
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Adding a New IAM User

The following example creates the IAM user my_groovy_user in the us-west-2 region with the password REPLACE_ME, and displays the user’s account ID. If a user with that name already exists, it displays a message and does not create a new user.

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