Triggering a Lambda function upon deleting a user

2020-05-26 03:45发布

问题:

AWS Cognito User Pools have some pre-defined events to handle user signup, confirmation etc. The full list is here.

However, there is no apparent trigger for deleting a user.

So, is there any way one can trigger a Lambda function when a user is deleted from Cognito User Pool (of course, with arguments like username and/or email address)?

回答1:

If you are using "Amazon Cognito Sync":

Amazon Cognito raises the Sync Trigger event when a dataset is synchronized. You can use the Sync Trigger event to take an action when a user is updated or deleted. Please have a look on below official document for more information and steps.

Ref: https://docs.aws.amazon.com/cognito/latest/developerguide/cognito-events.html

Records updated by the app user will have the 'op' field set as “replace” and the records deleted will have 'op' field as "remove".

According to above point in referenced documentation op field can help you to identify operation, So if value is "removed" in op then you can perform your actions for your business logic requirement.

If you are not using "Amazon Cognito Sync":

  1. Create user records in DynamoDB table "user" using Post Confirmation Lambda Trigger.
  2. Build your own user listing based on DynamoDB table "user".
  3. Build your own user delete api using lambda function and aws api gateway.
  4. You lambda function should handle delete user from cognito & also your business logic that you want to perform.