AWS Cognito - Create User Programmatically using Java

  • Last updated Apr 25, 2024

To programmatically create a new user within Amazon Cognito using Java, you can follow these steps:

  1. First, add AWS Java SDK For Amazon Cognito Identity Provider Service dependency to your project.
  2. Here's a sample Java code:
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProvider;
import com.amazonaws.services.cognitoidp.AWSCognitoIdentityProviderClientBuilder;
import com.amazonaws.services.cognitoidp.model.AWSCognitoIdentityProviderException;
import com.amazonaws.services.cognitoidp.model.AdminCreateUserRequest;
import com.amazonaws.services.cognitoidp.model.AdminCreateUserResult;
import com.amazonaws.services.cognitoidp.model.AdminSetUserPasswordRequest;
import com.amazonaws.services.cognitoidp.model.AttributeType;
import com.amazonaws.services.cognitoidp.model.MessageActionType;

public class CognitoExample {

  public static void main(String args) {
    // AWS credentials
    String ACCESS_KEY = "AKIASI5XVTY2KVH46OND";
    String SECRET_KEY = "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";

    // Cognito credentials
    String userPoolId = "us-east-1_c174bztKi";

    // Test user data
    String email = "testbuddy@example.com";
    String password = "Test123$";
    // New password is only required if the user status is FORCED_CHANGE_PASSWORD
    
    BasicAWSCredentials awsCreds = new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY);

    AWSCognitoIdentityProvider cognitoClient = AWSCognitoIdentityProviderClientBuilder.standard()
        .withCredentials(new AWSStaticCredentialsProvider(awsCreds)).withRegion("us-east-1")
        .build();

    try {
      AttributeType emailAttr = new AttributeType().withName("email").withValue(email);
      AttributeType emailVerifiedAttr =
          new AttributeType().withName("email_verified").withValue("false");

      AdminCreateUserRequest userRequest =
          new AdminCreateUserRequest().withUserPoolId(userPoolId).withUsername(email)
              .withTemporaryPassword(password).withUserAttributes(emailAttr, emailVerifiedAttr)
              .withMessageAction(MessageActionType.SUPPRESS);

      AdminCreateUserResult createUserResult = cognitoClient.adminCreateUser(userRequest);

      System.out.println("User " + createUserResult.getUser().getUsername()
          + " is created. Status: " + createUserResult.getUser().getUserStatus());

      // Make the password permanent and not temporary
      AdminSetUserPasswordRequest adminSetUserPasswordRequest =
          new AdminSetUserPasswordRequest().withUsername(email).withUserPoolId(userPoolId)
              .withPassword(password).withPermanent(true);
      cognitoClient.adminSetUserPassword(adminSetUserPasswordRequest);
    } catch (AWSCognitoIdentityProviderException e) {
      System.out.println(e.getErrorMessage());
    } catch (Exception e) {
      System.out.println(e);
    }
  }

}