How to configure Serverless Cognito Lambda Triggers
B

3

6

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.

Boman answered 7/8, 2019 at 23:58 Comment(0)
F
6

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

Fiore answered 11/8, 2019 at 14:38 Comment(3)
Erez thank you. Would you please provide the version of serverless you are using? I'm currently using 1.49.0 (Enterprise Plugin: 1.3.5, Platform SDK: 2.1.0) and I'm wondering if that might be the issue. The fact yours show in the user pool is exactly what mine are not doing though my lambda's correctly show upon deployed in lambda console.Boman
serverless --version output: 1.49.0 (Enterprise Plugin: 1.3.6, Platform SDK: 2.1.0) How about removing the stack and trying again without the custom settings for the the pool?Fiore
Erez I can't thank you enough for posting your repo which ironically was pretty much mine but I must have had something format wise wrong in my yml which I still can't find because yours did work. I literally pulled down your repo copied over your serverless.yml and it was working as expected.Boman
A
1

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
Alyssaalyssum answered 11/4, 2020 at 6:9 Comment(0)
C
0

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

Cloudburst answered 23/3, 2023 at 14:48 Comment(1)
Please edit your post to add code and data as text (using code formatting), not images. Images: A) don't allow us to copy-&-paste the code/errors/data for testing; B) don't permit searching based on the code/error/data contents; and many more reasons. Images should only be used, in addition to text in code format, if having the image adds something significant that is not conveyed by just the text code/error/data. See minimal reproducible example on what code is required.Selectee

© 2022 - 2024 — McMap. All rights reserved.