amazon-cognitoamazon-cognito-triggers

Serverless - Adding trigger to Using existing pools fails deployment - CREATE_FAILED: CustomDashresourceDashexistingDashcupLambdaFunction


I'm adding a new trigger to an existing Cognito User Pool, but when using the "existing: true" to it, the deployment fails with this error:

Environment: darwin, node 16.16.0, framework 3.35.2 (local), plugin 7.0.5, SDK 4.4.0
Credentials: Local, "XXX-XXXX" profile
Docs:        docs.serverless.com
Support:     forum.serverless.com
Bugs:        github.com/serverless/serverless/issues


Error:
CREATE_FAILED: CustomDashresourceDashexistingDashcupLambdaFunction (AWS::Lambda::Function)
Resource handler returned message: "Function creation failed because the function already exists" (RequestToken: XXXXX-XXXX-25bb-XXX-07099XXX, HandlerErrorCode: AlreadyExists)

I've followed the official documentation docs-providers-aws-events-cognito-user-pool#forcing-deploying-of-triggers

  postConfirmation:
    handler: triggers.postConfirmation
    events:
      - cognitoUserPool:
          pool: ${self:custom.userPoolName}
          trigger: PostConfirmation
          existing: true #here's the issue

Solution

  • I've found the solution.

    Even after removing the entire stack, the failure persisted, which was extremely frustrating.

    I realized that Serverless Framework deploys a lambda function to handle pre-existing Cognito resources. Even if you remove the stack, this lambda function is not deleted and is not linked to any stack. As a result, every time I attempted a new deployment, the lambda was already created, and the failure occurred because it could not recreate the same lambda.

    enter image description here

    Although my question was posted less than a day ago, I had been trying to resolve it for over a week.