Cognitoユーザープールにカスタム属性を持つユーザーを作成してみた

2020.08.31

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部の若槻です。

前回、カスタム属性を持つCognitoユーザプールを作成しましたが、今回はそのユーザープールに実際にカスタム属性を持つユーザーをコンソールとAWS CLIから作成できるか確認してみました。

マネジメントコンソールから作成

結論から言うと、マネジメントコンソールではユーザー作成時や作成後に、必須でない標準属性やカスタム属性を設定することはできませんでした。

マネジメントコンソールのユーザープール一覧でユーザーを作成したいユーザープールを開き、[全般設定] - [ユーザーとグループ]で[ユーザーの作成]をクリック。 image.png

[ユーザーの作成]ダイアログが開きますが、必須属性の入力項目しかありません。ユーザーの情報を入力したら[ユーザーの作成]をクリック。 image.png

するとInvalid phone number format.というエラーが出ました。 image.png

電話番号は+XXXXXXXXXの形式とする必要があるようです。

電話番号の入力を修正して再度[ユーザーの作成]をクリック。 image.png

次はPassword did not conform with password policy: Password must have symbol charactersというエラーが出ました。パスワード文字列に記号が含まれておらずポリシー違反となったようです。 image.png

仮パスワードの入力を修正して再度[ユーザーの作成]をクリック。 image.png

作成できました。ユーザー名をクリック。 image.png

ユーザー詳細ページでは属性値を編集するメニューがありません。 image.png

マネジメントコンソールではユーザー作成時や作成後に、必須でない標準属性やカスタム属性を設定することができないようです。

AWS CLIでの作成

AWS CLIの場合はカスタム属性を持つユーザーを作成可能です。

まずlist-user-poolsコマンドによるユーザー作成先のユーザープールIDの確認をします。

% aws cognito-idp list-user-pools
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:

  aws help
  aws <command> help
  aws <command> <subcommand> help
aws: error: the following arguments are required: --max-results

list-user-poolsコマンドでは--max-resultsオプションの指定が必要のようです。指定して再実行したら取得できました。

% aws cognito-idp list-user-pools --max-results 3
{
    "UserPools": [
        {
            "Id": "ap-northeast-1_AwJpGCDnr",
            "Name": "myPool01",
            "LambdaConfig": {},
            "LastModifiedDate": "2020-08-30T15:16:13.141000+09:00",
            "CreationDate": "2020-08-30T15:16:13.141000+09:00"
        },
        ...
    ]
}

次にadmin-create-userコマンドでユーザーuser02を作成します。user-attributesでカスタム属性custom:attr01を設定しています。しかし、コマンドを実行するとUser pool does not have SMS configuration to send messages.というエラーとなりました。

% aws cognito-idp admin-create-user \
--user-pool-id ap-northeast-1_AwJpGCDnr \
--username user02 \
--user-attributes \
  Name=email,Value=user02@example.com \
  Name=phone_number,Value="+8111111111" \
  Name=email_verified,Value=True \
  Name=phone_number_verified,Value=True \
  Name=custom:attr01,Value=りんご

An error occurred (InvalidParameterException) when calling the AdminCreateUser operation: User pool does not have SMS configuration to send messages.

これは、ユーザープールでSMSを送信する設定が構成されていないが、DesiredDeliveryMedium属性で既定のSMSが指定されている場合に発生するエラーのようです。

そして作成コマンドはエラーとはなりましたが、ユーザーuser02は作成できていました。カスタム属性もきちんと設定できていました。

% aws cognito-idp admin-get-user --user-pool-id ap-northeast-1_AwJpGCDnr --username user02
{
    "Username": "user02",
    "UserAttributes": [
        {
            "Name": "sub",
            "Value": "3b1e480d-2866-4212-9917-7ad9ca8c551b"
        },
        {
            "Name": "email_verified",
            "Value": "True"
        },
        {
            "Name": "phone_number_verified",
            "Value": "True"
        },
        {
            "Name": "phone_number",
            "Value": "+8111111111"
        },
        {
            "Name": "email",
            "Value": "user02@example.com"
        },
        {
            "Name": "custom:attr01",
            "Value": "りんご"
        }
    ],
    "UserCreateDate": "2020-08-31T03:47:19.336000+09:00",
    "UserLastModifiedDate": "2020-08-31T03:47:19.336000+09:00",
    "Enabled": true,
    "UserStatus": "FORCE_CHANGE_PASSWORD"
}

また、desired-delivery-mediumsオプションでEMAILを指定してadmin-create-userコマンドで再度ユーザーuser03の作成を実行したら次はエラー無く実行できました。

% aws cognito-idp admin-create-user \
--user-pool-id ap-northeast-1_AwJpGCDnr \
--username user03 \
--user-attributes \
  Name=email,Value=user03@example.com \
  Name=phone_number,Value="+8111111111" \
  Name=email_verified,Value=True \
  Name=phone_number_verified,Value=True \
  Name=custom:attr01,Value=みかん \
--desired-delivery-mediums EMAIL

{
    "User": {
        "Username": "user03",
        "Attributes": [
            {
                "Name": "sub",
                "Value": "5182c2f5-cb3f-40b4-9e64-94bd87075692"
            },
            {
                "Name": "email_verified",
                "Value": "True"
            },
            {
                "Name": "phone_number_verified",
                "Value": "True"
            },
            {
                "Name": "phone_number",
                "Value": "+8111111111"
            },
            {
                "Name": "email",
                "Value": "user03@example.com"
            },
            {
                "Name": "custom:attr01",
                "Value": "みかん"
            }
        ],
        "UserCreateDate": "2020-08-31T04:15:36.452000+09:00",
        "UserLastModifiedDate": "2020-08-31T04:15:36.452000+09:00",
        "Enabled": true,
        "UserStatus": "FORCE_CHANGE_PASSWORD"
    }
}

おわりに

Cognitoユーザープールにカスタム属性を持つユーザーをコンソールとAWS CLIから作成できるか確認してみました。

カスタム属性の関係ないところでの躓きが多かったですが、Cognitoユーザー作成の勘所が少しずつ分かってきました。

参考

以上