Search code examples
javaamazon-web-servicesaws-lambdaaws-java-sdk

How to receive a Cognito Custom Email Sender trigger event in Java AWS SDK?


I tried writing a customEmailSender function in JavaScript and it seems the event contains a code parameter that contains either a verification code or a temporary password, depending on the triggerSource. A customSmsSender function would also contain a code parameter. But, I decided to go with Java to create my functions, and I am using version 3.11.0 (docs) of aws-lambda-java-events. Currently, my only option (that I know of) is to use CognitoUserPoolCustomMessageEvent as my event type.

@Slf4j
@RequiredArgsConstructor
public class CustomEmailSenderHandler implements RequestHandler<CognitoUserPoolCustomMessageEvent, Void> {

  @Override
  public Void handleRequest(final CognitoUserPoolCustomMessageEvent cognitoEvent, final Context context) {
    log.info("Received CognitoUserPoolEvent {}", cognitoEvent);

    return null;
  }

}

The event gets logged with a codeParameter field that is null, but from the JavaScript example, I know mine contains a code field.

Also, the Cognito user pool contains a customMessage lambda trigger and I believe this is what the CognitoUserPoolCustomMessageEvent maps to. Every other trigger has an event type, except customEmailSender and customSmsSender. Is there a way to do this without just having to go back to Node functions for these 2?


Solution

  • I was unable to find an Event class for both the CustomEmailSender and CustomSmsSender lambda functions, so I decided to just make my own. Based on the parameters shown in the docs for the custom message lambda trigger, codeParameter is correct, but it is not what I need since the triggers that I am using, use code instead of codeParameter. I went off of the current CognitoUserPoolCustomMessageEvent class to create my own. Surprisingly, the event for both the custom sms sender and custom email sender are the same, the only thing that differentiates them is the type parameter.

    @Data
    @EqualsAndHashCode(callSuper = true)
    @NoArgsConstructor
    public class CognitoUserPoolCustomSenderEvent extends CognitoUserPoolEvent {
    
      private Request request;
    
      @Builder(setterPrefix = "with")
      public CognitoUserPoolCustomSenderEvent(
        String version,
        String triggerSource,
        String region,
        String userPoolId,
        String userName,
        CallerContext callerContext,
        Request request
      ) {
        super(version, triggerSource, region, userPoolId, userName, callerContext);
        this.request = request;
      }
    
      @Data
      @EqualsAndHashCode(calSuper = true)
      @NoArgsConstructor
      public static class Request extends CognitoUserPoolEvent.Request {
    
        private String type;
    
        private Map<String, String> clientMetadata;
    
        private String code;
    
        @Builder(setterPrefix = "with")
        public Request(Map<String, String> userAttributes, String type, Map<String, String> clientMetadata, String code) {
          super(userAttributes);
          this.type = type;
          this.clientMetadata = clientMetadata;
          this.code = code;
        }
    
      }
    
    }