CMoudy
CMoudy

Reputation: 63

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.

Upvotes: 6

Views: 7153

Answers (3)

shaunak1111
shaunak1111

Reputation: 961

For existing pools please use existing:true and forceDeploy: true as in the article here

https://forum.serverless.com/t/how-to-specify-an-existing-cognito-user-pool-in-servreless-yml/2412/18

enter image description here

Upvotes: 0

zeke
zeke

Reputation: 11

I found the issue in your serverless.yml. You are missing an indentation under cognitoUserPool. I tried it both ways and it works with the additional indentation.

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

Upvotes: 1

Erez
Erez

Reputation: 1750

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

Upvotes: 6

Related Questions