The AWS SDK for Java 1.x has entered maintenance mode as of July 31, 2024,
and will reach end-of-support
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
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
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 ListUsersResult 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
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
final AmazonIdentityManagement iam = AmazonIdentityManagementClientBuilder.defaultClient(); boolean done = false; ListUsersRequest request = new ListUsersRequest(); while(!done) { 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
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
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
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
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
-
IAM Users in the IAM User Guide
-
Managing IAM Users in the IAM User Guide
-
CreateUser in the IAM API Reference
-
ListUsers in the IAM API Reference
-
UpdateUser in the IAM API Reference
-
DeleteUser in the IAM API Reference