Cognito launch lambda when user added to a user pool

Viewed 615

Is it possible to trigger a lambda when a user is added in a user pool (any one) via an EventBridge rule ?

1 Answers

It is possible to log AWS Cognito in CloudTrail and then use EventBridge to trigger a lambda based on calls in AWS CloudTrail API from AWS Cognito.

  1. It would be required to set a trail in AWS CloudTrail as mentioned in Logging Amazon Cognito API calls with AWS CloudTrail
  2. In EventBridge you can select an Event pattern from the service AWS Cognito User Pool and CloudTrail:

enter image description here

  1. You may filter the lambda to be triggered from a single operation, such as AdminCreateUser

List of support operations to filter the lambda trigger,supported operations.

You could also set a lambda as a trigger in AWS Cognito for a Pre sign-up or Pre-authentication on every user action instead of using Event Bridge.

As stated in the documentation Customizing user pool workflows with Lambda triggers:

You can create an AWS Lambda function and then trigger that function during user pool operations such as user sign-up, confirmation, and sign-in (authentication) with a Lambda trigger. You can add authentication challenges, migrate users, and customize verification messages.

To link a lambda as a trigger in AWS Cognito:

  1. Create your lambda if does not exist.
  2. In AWS Cognito, choose an existing user pool from the list, or create a user pool.
  3. In your user pool, choose the Triggers tab from the navigation bar.
  4. Choose a Lambda trigger, such as Pre sign-up or Pre-authentication, and then choose your Lambda function (Created before in AWS Lambda) from the Lambda function drop-down list.
  5. Choose Save changes.
Related