SESClient taking very long to initialize causing Cognito CreateAuthChallenge trigger to retry

0

I'm trying to implement custom authentication flow in Cognito for OTP-based authentication over email for which I have written my lambda functions using AWS Java 2 SDK. The problem is with the CreateAuthChallenge lambda function, which takes around 10 seconds to execute making the trigger retry leading to multiple events getting sent to the CreateAuthChallenge function. On examining the logs it turns out that the SES client initialization is taking time. Below is my code for sending the Email:

`

          logger.log("initializing client ...");
          try(SesClient sesClient = SesClient.builder()
                .region(Region.AP_SOUTH_1)
                .credentialsProvider(InstanceProfileCredentialsProvider.create())
                .build()) {
            logger.log("sending email ...");
            SendEmailRequest emailRequest = SendEmailRequest.builder()
                    .source(SENDER_EMAIL)
                    .destination(Destination.builder().toAddresses(userAttributes.get("email").asText()).build())
                    .message(Message.builder()
                            .subject(Content.builder().data(SUBJECT).build())
                            .body(Body.builder().text(Content.builder().data(secretCode).build()).build()).build())
                    .build();
            SendEmailResponse sendEmailResponse = sesClient.sendEmail(emailRequest);
            logger.log("sendEmailResult: " + sendEmailResponse.messageId());
          }

`

CloudWatch Logs:

`

2024-05-02T19:41:02.609+05:30	initializing client ...

2024-05-02T19:41:09.312+05:30	sending email ...

2024-05-02T19:41:11.870+05:30	sendEmailResult: 0109018f39a51657-1e8c8309-2cf3-483a-ac6f-201104bbe62d-000000

REPORT RequestId: fb7f2ce7-7eee-41f5-b53d-dfaf6ff3fd99	Duration: 9820.38 ms	Billed Duration: 9821 ms	Memory Size: 512 MB	 
Max Memory Used: 184 MB	Init Duration: 710.78 ms	

`

Is there any way I can speed up the SESClient initialization, or shall I move out the code for sending email to a separate lambda and invoke in async.

1 Answer

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions