Cognito postConfirmation function not triggered

Viewed 113

I'm signing up users to my application using serverless and AWS Lambda/Cognito, and I'd like to send a confirmation email after the users sign up. To do this, I created a postConfirmation function definition in my serverless.yml to be triggered after the postConfirmation Cognito event fires, but that function doesn't seem to get called at all. What could be the issue?

serverless.yml

...
  postConfirmation:
    description: Cognito handler for when a user is confirmed, here it is used for storing the data to database and sending a confirmation email
    name: "${self:service}-users-post-confirmation-cognito-trigger-${self:provider.stage}"
    handler: src/service/users/postConfirmationTrigger.handler
    dependsOn:
      - CognitoUserPoolMyUserPool
    events:
      - cognitoUserPool:
          pool: ${self:service}-user-pool-${self:provider.stage}
          trigger: PostConfirmation
          existing: true

I'm trying to see if the postConfirmation handler is called at all:

exports.handler =  (event, context, callback)  => {
    console.log(`this function fires!`)
    console.log(event)
}

but I don't see anything in the logs. Not sure how to trigger the handler

2 Answers

Looks like a classic Lambda callback issue. Your Lambda is finishing before a callback is completed. Try this:

exports.handler =  (event, context, callback)  => {
    console.log(`this function fires!`)
    console.log(event)
    context.done(null, event);
}

or this

exports.handler =  (event, context, callback)  => {
    console.log(`this function fires!`)
    console.log(event)
    callback(null, event);
}

Also make sure your Lambda configuration is setup so that it doesn't timeout. The default is 3s which is often too short.

PostConfirmation Lambda is triggered when the user is confirmed not only signup, so make sure that the user verifies his email or phone number after signup by setting autoVerify email using console:

enter image description here

Or using serverless:

Resources:
  CognitoUserPool:
    Type: AWS::Cognito::UserPool
    Properties:
      UserPoolName: user-pool
      # Set email as an alias
      UsernameAttributes:
        - email
      AutoVerifiedAttributes:
        - email  <--- HERE
Related