0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS AppSyncでAPIを作ってみたAdvent Calendar 2022

Day 24

LambdaでCognitoユーザプールのSub情報からCognitoのユーザを削除する

Last updated at Posted at 2022-12-23

概要

Lambdaでsub情報からCognitoのユーザーを削除する方法を紹介します。

環境

  • ランタイム:Python3.9
  • リージョン:ap-northeast-1

Lambda関数

import boto3

cognito = boto3.client('cognito-idp', region_name='ap-northeast-1')
user_pool_id = <ユーザープールのID>

def get_cognito_user_name(sub):
    response = cognito.list_users(
        UserPoolId=user_pool_id,
        Filter='sub = "' + sub + '"'
    )
        
    user_name = response["Users"][0]['Username']

    return user_name

def delete_cognito_user(user_name):   
    response = cognito.admin_delete_user(
        UserPoolId=user_pool_id,
        Username=user_name
    )

def lambda_handler(event, context):
    sub = <sub値>

    # ユーザー名の取得
    user_name = get_cognito_user_name(sub)

    # Cognitoからユーザーを削除する
    delete_cognito_user(user_name)

    return "Success"

必要なIAM権限

  • cognito-idp:AdminDeleteUser
  • cognito-idp:AdminGetUser
  • cognito-idp:ListUsers

実行例

  • Cognitoユーザープールにテストユーザーを作成します。
    cognito-user.png

  • Lambdaを実行します。
    実行結果.png

  • Cognitoでユーザーが削除されたことを確認できました。
    スクリーンショット 2022-12-12 161436.png

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?