Update an IAM user using an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Update an IAM user using an AWS SDK

The following code examples show how to update an IAM user.

C++
SDK for C++
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

bool AwsDoc::IAM::updateUser(const Aws::String &currentUserName, const Aws::String &newUserName, const Aws::Client::ClientConfiguration &clientConfig) { Aws::IAM::IAMClient iam(clientConfig); Aws::IAM::Model::UpdateUserRequest request; request.SetUserName(currentUserName); request.SetNewUserName(newUserName); auto outcome = iam.UpdateUser(request); if (outcome.IsSuccess()) { std::cout << "IAM user " << currentUserName << " successfully updated with new user name " << newUserName << std::endl; } else { std::cerr << "Error updating user name for IAM user " << currentUserName << ":" << outcome.GetError().GetMessage() << std::endl; } return outcome.IsSuccess(); }
  • For API details, see UpdateUser in AWS SDK for C++ API Reference.

Java
SDK for Java 2.x
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

public static void updateIAMUser(IamClient iam, String curName,String newName ) { try { UpdateUserRequest request = UpdateUserRequest.builder() .userName(curName) .newUserName(newName) .build(); iam.updateUser(request); System.out.printf("Successfully updated user to username %s", newName); } catch (IamException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } }
  • For API details, see UpdateUser in AWS SDK for Java 2.x API Reference.

JavaScript
SDK for JavaScript V3
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

Create the client.

import { IAMClient } from "@aws-sdk/client-iam"; // Set the AWS Region. const REGION = "REGION"; // For example, "us-east-1". // Create an IAM service client object. const iamClient = new IAMClient({ region: REGION }); export { iamClient };

Update the user.

// Import required AWS SDK clients and commands for Node.js. import { iamClient } from "./libs/iamClient.js"; import { UpdateUserCommand } from "@aws-sdk/client-iam"; // Set the parameters. export const params = { UserName: "ORIGINAL_USER_NAME", //ORIGINAL_USER_NAME NewUserName: "NEW_USER_NAME", //NEW_USER_NAME }; export const run = async () => { try { const data = await iamClient.send(new UpdateUserCommand(params)); console.log("Success, username updated"); return data; } catch (err) { console.log("Error", err); } }; run();
SDK for JavaScript V2
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the IAM service object var iam = new AWS.IAM({apiVersion: '2010-05-08'}); var params = { UserName: process.argv[2], NewUserName: process.argv[3] }; iam.updateUser(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
Kotlin
SDK for Kotlin
Note

This is prerelease documentation for a feature in preview release. It is subject to change.

Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

suspend fun updateIAMUser(curName: String?, newName: String?) { val request = UpdateUserRequest { userName = curName newUserName = newName } IamClient { region = "AWS_GLOBAL" }.use { iamClient -> iamClient.updateUser(request) println("Successfully updated user to $newName") } }
  • For API details, see UpdateUser in AWS SDK for Kotlin API reference.

Python
SDK for Python (Boto3)
Note

There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository.

def update_user(user_name, new_user_name): """ Updates a user's name. :param user_name: The current name of the user to update. :param new_user_name: The new name to assign to the user. :return: The updated user. """ try: user = iam.User(user_name) user.update(NewUserName=new_user_name) logger.info("Renamed %s to %s.", user_name, new_user_name) except ClientError: logger.exception("Couldn't update name for user %s.", user_name) raise return user
  • For API details, see UpdateUser in AWS SDK for Python (Boto3) API Reference.