Menu
AWS SDK for Java
Developer Guide

Managing IAM Users

Creating a User

Create a new IAM user by providing the user name to the AmazonIdentityManagementClient's createUser method, either directly or using a CreateUserRequest object containing the user name.

Imports

Copy
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement; import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder; import com.amazonaws.services.identitymanagement.model.CreateUserRequest; import com.amazonaws.services.identitymanagement.model.CreateUserResult;

Code

Copy
final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient(); CreateUserRequest request = new CreateUserRequest() .withUserName(username); CreateUserResult response = iam.createUser(request);

See the complete example.

Listing Users

To list the IAM users for your account, create a new ListUsersRequest and pass it to the AmazonIdentityManagementClient's listUsers method. You can retrieve the list of users by calling getUsers on the returned ListUsersResponse object.

The list of users returned by listUsers is paged. You can check to see there are more results to retrieve by calling the response object's getIsTruncated method. If it returns true, then call the request object's setMarker() method, passing it the return value of the response object's getMarker() method.

Imports

Copy
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement; import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder; import com.amazonaws.services.identitymanagement.model.ListUsersRequest; import com.amazonaws.services.identitymanagement.model.ListUsersResult; import com.amazonaws.services.identitymanagement.model.User;

Code

Copy
final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient(); boolean done = false; while(!done) { ListUsersRequest request = new ListUsersRequest(); ListUsersResult response = iam.listUsers(request); for(User user : response.getUsers()) { System.out.format("Retrieved user %s", user.getUserName()); } request.setMarker(response.getMarker()); if(!response.getIsTruncated()) { done = true; } }

See the complete example.

Updating a User

To update a user, call the AmazonIdentityManagementClient object's updateUser method, which takes a UpdateUserRequest object that you can use to change the user's name or path.

Imports

Copy
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement; import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder; import com.amazonaws.services.identitymanagement.model.UpdateUserRequest; import com.amazonaws.services.identitymanagement.model.UpdateUserResult;

Code

Copy
final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient(); UpdateUserRequest request = new UpdateUserRequest() .withUserName(cur_name) .withNewUserName(new_name); UpdateUserResult response = iam.updateUser(request);

See the complete example.

Deleting a User

To delete a user, call the AmazonIdentityManagementClient's deleteUser request with a UpdateUserRequest object set with the user name to delete.

Imports

Copy
import com.amazonaws.services.identitymanagement.AmazonIdentityManagement; import com.amazonaws.services.identitymanagement.AmazonIdentityManagementClientBuilder; import com.amazonaws.services.identitymanagement.model.DeleteConflictException; import com.amazonaws.services.identitymanagement.model.DeleteUserRequest;

Code

Copy
final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient(); DeleteUserRequest request = new DeleteUserRequest() .withUserName(username); try { iam.deleteUser(request); } catch (DeleteConflictException e) { System.out.println("Unable to delete user. Verify user is not" + " associated with any resources"); throw e; }

See the complete example.

More Information