この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。
こんにちは、CX事業本部の若槻です。
前回、カスタム属性を持つCognitoユーザプールを作成しましたが、今回はそのユーザープールに実際にカスタム属性を持つユーザーをコンソールとAWS CLIから作成できるか確認してみました。
マネジメントコンソールから作成
結論から言うと、マネジメントコンソールではユーザー作成時や作成後に、必須でない標準属性やカスタム属性を設定することはできませんでした。
マネジメントコンソールのユーザープール一覧でユーザーを作成したいユーザープールを開き、[全般設定] - [ユーザーとグループ]で[ユーザーの作成]をクリック。
[ユーザーの作成]ダイアログが開きますが、必須属性の入力項目しかありません。ユーザーの情報を入力したら[ユーザーの作成]をクリック。
するとInvalid phone number format.
というエラーが出ました。
電話番号は+XXXXXXXXX
の形式とする必要があるようです。
電話番号の入力を修正して再度[ユーザーの作成]をクリック。
次はPassword did not conform with password policy: Password must have symbol characters
というエラーが出ました。パスワード文字列に記号が含まれておらずポリシー違反となったようです。
仮パスワードの入力を修正して再度[ユーザーの作成]をクリック。
作成できました。ユーザー名をクリック。
ユーザー詳細ページでは属性値を編集するメニューがありません。
マネジメントコンソールではユーザー作成時や作成後に、必須でない標準属性やカスタム属性を設定することができないようです。
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ユーザー作成の勘所が少しずつ分かってきました。
参考
- Invalid phone number format on Cognito signup | stackoverflow
- list-user-pools | aws . cognito-idp
- Cognito User Pool trying to send SMS when it's configured for email sending | stackoverflow
- admin-get-user | aws . cognito-idp
以上