📝

[小ネタ] AWS CLI で Cognito のパスワードに `!` を指定する際はエスケープする必要がある話

2025/01/21に公開

今から始める Amazon Cognito 入門 #1:ユーザープールと ID プールの違い | DevelopersIO
上記ブログに沿って AWS CLI の sign-up コマンドを実行したところ、以下のエラーが発生しました。

aws cognito-idp sign-up \
--client-id xxx \
--username xxx \
--password !xxx \
bash: !xxx: event not found
--user-attributes Name=email,Value=xxx

An error occurred (InvalidParameterException) when calling the SignUp operation: 1 validation error detected: Value at 'password'failed to satisfy constraint: Member must not be null

原因

原因は ! が bash におけるヒストリー機能のデフォルト値として使用される点でした。

History Interaction (Bash Reference Manual)

History expansions are introduced by the appearance of the history expansion character, which is ‘!’ by default.

bashに !' で event no found と怒られた理由を考えてみる #Bash - Qiita

回避策

\! のようにエスケープすれば ! を文字列として使用可能です。

aws cognito-idp sign-up \
--client-id xxx \
--username xxx \
--password \!xxx \
--user-attributes Name=email,Value=xxx

また、文字列の末尾に使用した場合でもリクエストが成功しました。

aws cognito-idp sign-up \
--client-id xxx \
--username xxx \
--password xxx! \
--user-attributes Name=email,Value=xxx

CloudTrail の記録

上記エラー発生時の CloudTrail には以下の通り password パラメータが存在しないリクエストとして記録されていました。

SignUp
"requestParameters": {
    "clientId": "xxx",
    "username": "HIDDEN_DUE_TO_SECURITY_REASONS",
    "userAttributes": "HIDDEN_DUE_TO_SECURITY_REASONS"
},

まとめ

今回は AWS CLI で Cognito のパスワードに ! を指定する際はエスケープする必要がある話を紹介しました。
結果的には Cognito に限った内容ではなく bash の仕様でしたが、どなたかの参考になれば幸いです。

参考資料

Discussion