question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[cognito] can't add cognito trigger after UserPool.fromUserPoolId

See original GitHub issue

version:

"@aws-cdk/aws-cognito": "^1.60.0",

Code

    const amplifyUserPool = cognito.UserPool.fromUserPoolId(
      this,
      "amplifyUserPool",
      "us-east-1_ABCDE"
    );

    console.log("addTrigger" in amplifyUserPool);

Expect

true

Receive

false

Documentation

Importing User Pools addTrigger

More

The documentation indeed mentions:

However, imported user pools have limited configurability. As a rule of thumb, none of the properties that is are part of the AWS::Cognito::UserPool CloudFormation resource can be configured.

But I am when I am using serverless framework, it works like a charm. Just wonder any workarounds against this? I want to move my full serverless stack to CDK, it is such a beautiful tech.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:14
  • Comments:19 (5 by maintainers)

github_iconTop GitHub Comments

10reactions
cststcommented, Jun 3, 2021

I was not able to get the above solution working as I did not have access to the underlying CfnUserPool via fromUserPoolId or node.defaultChild.

I was however able to add triggers to a user pool imported via fromUserPoolId by using a custom resource:

    const userPool = Cognito.UserPool.fromUserPoolId(this, "UserPool", userPoolId);

    new CustomResources.AwsCustomResource(this, "UpdateUserPool", {
      resourceType: "Custom::UpdateUserPool",
      onCreate: {
        region: this.region,
        service: "CognitoIdentityServiceProvider",
        action: "updateUserPool",
        parameters: {
          UserPoolId: userPool.userPoolId,
          LambdaConfig: {
            PreSignUp: preSignUpHandler.functionArn,
            DefineAuthChallenge: defineAuthChallengeHandler.functionArn,
            CreateAuthChallenge: createAuthChallengeHandler.functionArn,
            VerifyAuthChallengeResponse: verifyAuthChallengeResponseHandler.functionArn,
          },
        },
        physicalResourceId: CustomResources.PhysicalResourceId.of(userPool.userPoolId),
      },
      policy: CustomResources.AwsCustomResourcePolicy.fromSdkCalls({ resources: CustomResources.AwsCustomResourcePolicy.ANY_RESOURCE }),
    });
4reactions
brianiumcommented, Feb 3, 2021

@skinny85 that worked a treat! Thank you so much 🙇🏻

For anyone else encountering this issue, the solution looks like this:

const cfnPool = template.getResource("UserPool") as cognito.CfnUserPool;
cfnPool.lambdaConfig = {
  userMigration: migrate.functionArn,
};
Read more comments on GitHub >

github_iconTop Results From Across the Web

Customizing user pool workflows with Lambda triggers
When you create a Lambda trigger outside of the Amazon Cognito console, you must add permissions to the Lambda function. When you add...
Read more >
Post authentication Lambda trigger - Amazon Cognito
Because Amazon Cognito invokes this trigger after signing in a user, you can add custom logic after Amazon Cognito authenticates the user.
Read more >
Add Lambda trigger to imported Cognito User Pool with AWS ...
This was discussed in this issue. You can add triggers to an existing User Pool using a Custom Resource: import * as CustomResources...
Read more >
Creating the CustomSMS Trigger in AWS Cognito using lambda
After this let everything be default and create the userpool. ... aws cognito-idp update-user-pool --user-pool-id userpool-id --lambda-config ...
Read more >
Send AWS Cognito emails with 3rd party ESPs
Create Cognito User Pool # ... This is the most confusing part. We need to set the LambdaConfig setting of the User Pool....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found