Search code examples
amazon-web-servicesaws-lambdaamazon-cognitoserverless-frameworkeventtrigger

How to configure Serverless Cognito Lambda Triggers


Using the Serverless framework to create a Cognito User Pool as well as several lambdas to be used for cognito events during TOPT SMS Authorization. Everything is created however the lambda functions are not registered with Cognito.

Relatively new to Serverless jut can't seem to get them to connect. Have tried pool names as others have tried to mark as already present at the end of creation the pool is there and the lambdas are there but there is no connection.

Currently following another post tried changing user pool to CognitoUserPoolMyUserPool and then in lambda referencing it as MyUserPool. Have also tried just CognitoUserPool in both locations and neither work.

Example serverless.yaml file:

service: cognito-authentication

frameworkVersion: ">=1.1.0 <2.0.0"

package:
  individually: false

plugins:
  - serverless-bundle 

custom:
  stage: ${opt:stage, self:provider.stage}
  poolName: ${self:custom.stage}-user-pool

provider:
  name: aws
  runtime: nodejs10.x
  stage: dev
  iamRoleStatements:
    - Effect: Allow
      Action:
        - sns:*
      Resource: 
        - "*"

functions:

  preSignUp:
    handler: functions/pre-signup.main
    events:
      - cognitoUserPool:
        pool: MyUserPool
        trigger: PreSignUp

  defineAuthChallenge:
    handler: functions/define-auth-challenge.main
    events:
      - cognitoUserPool:
        pool: MyUserPool
        trigger: DefineAuthChallenge

  createAuthChallenge:
    handler: functions/create-auth-challenge.main
    events:
      - cognitoUserPool:
        pool: MyUserPool
        trigger: CreateAuthChallenge

  verifyAuthChallengeResponse:
    handler: functions/verify-auth-challenge-response.main
    events:
      - cognitoUserPool:
        pool: MyUserPool
        trigger: VerifyAuthChallengeResponse

resources:
  Resources:
    CognitoUserPoolMyUserPool:
      Type: "AWS::Cognito::UserPool"
      Properties:
        # Generate a name based on the stage
        UserPoolName: ${self:custom.poolName}
        # Set phone_number as an alias
        UsernameAttributes:
          - phone_number
        Policies:
          PasswordPolicy:
            MinimumLength: 6
            RequireLowercase: False
            RequireNumbers: False
            RequireSymbols: False
            RequireUppercase: False

    CognitoUserPoolClient:
      Type: "AWS::Cognito::UserPoolClient"
      Properties:
        # Generate an app client name based on the stage
        ClientName: ${self:custom.stage}-sms-auth-client
        UserPoolId:
          Ref: CognitoUserPoolMyUserPool
        ExplicitAuthFlows:
          - CUSTOM_AUTH_FLOW_ONLY
        GenerateSecret: false

Expectation is the User Pool is correctly created and configured to use the lambdas for triggered workflow execution.


Solution

  • I've copied pasted your code (and added relevant Lambda functions) and it works for me.

    I've tested the PreSignUp with the following command: aws cognito-idp admin-create-user --region <region> --user-pool-id <user-pool-id> --username <phone>

    While not showing in the AWS Console Lambda UI, the triggers do show up in the Cognito->User Pools->dev-user-pool->Triggers, which is confusing.

    Example repo: https://github.com/erezrokah/serverless-cognito-triggers