Cognito のドメイン変更で ALB のユーザー認証に反映されないときの対処方法
困っていた内容
ALB のユーザー認証に Cognito を使用しています。
Cognito のドメインを変更しましたが、ALB のリダイレクトに反映されません。
ALB のユーザー認証にドメイン変更を反映する方法を教えてください。
どう対応すればいいの?
ALB リスナーの設定で「ユーザープールドメイン」を更新してください。
2025年2月時点の挙動として、AWS マネジメントコンソールには表示されていませんが、ALB で Cognito に紐付くドメインはAuthenticateCognitoActionConfigとして設定されています。
# AWS CLI の確認例
aws elbv2 describe-rules \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:123456789012:listener/app/hato-alb/abcd/1234 \
--query 'Rules[].Actions[?Type==`authenticate-cognito`].AuthenticateCognitoConfig'
[
[
{
"UserPoolArn": "arn:aws:cognito-idp:ap-northeast-1:123456789012:userpool/ap-northeast-1_hato",
"UserPoolClientId": "hatoClientId",
"UserPoolDomain": "hato-old",
"SessionCookieName": "AWSELBAuthSessionCookie",
"Scope": "openid",
"SessionTimeout": 604800,
"OnUnauthenticatedRequest": "authenticate"
}
]
]
また Cognito 側のドメイン設定を変更しても ALB 側のAuthenticateCognitoActionConfig設定には自動的に反映されません。
そのため、Cognito のドメインを変更する場合は、Cognito 側の設定変更と併せて ALB 側の設定も変更してください。
やってみた
Amazon Cognito コンソールから Cognito のドメインを次の通り変更します。
旧ドメイン:
https://hato-old.auth.ap-northeast-1.amazoncognito.com
新ドメイン:
https://hato-new.auth.ap-northeast-1.amazoncognito.com
AWS CLI のdescribe-rulesコマンドで ALB に設定されたドメインを確認すると、この時点では旧ドメインのままです。
aws elbv2 describe-rules \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:123456789012:listener/app/hato-alb/abcd/1234 \
--query 'Rules[].Actions[?Type==`authenticate-cognito`].AuthenticateCognitoConfig'
[
[
{
"UserPoolArn": "arn:aws:cognito-idp:ap-northeast-1:123456789012:userpool/ap-northeast-1_hato",
"UserPoolClientId": "hatoClientId",
"UserPoolDomain": "hato-old",
"SessionCookieName": "AWSELBAuthSessionCookie",
"Scope": "openid",
"SessionTimeout": 604800,
"OnUnauthenticatedRequest": "authenticate"
}
]
]
ALB 側の設定も変更します。
Amazon EC2 コンソールから対象のロードバランサーを選択します。
「リスナーの管理」から「リスナーの編集」を選択します。
「ユーザープールドメイン」で新しいドメインを選択し「変更内容の保存」をクリックします。
リスナーが正常に更新されたことを確認します。
describe-rulesコマンドで ALB に設定されたドメインを確認すると、新ドメインに更新されています。
aws elbv2 describe-rules \
--listener-arn arn:aws:elasticloadbalancing:ap-northeast-1:123456789012:listener/app/hato-alb/abcd/1234 \
--query 'Rules[].Actions[?Type==`authenticate-cognito`].AuthenticateCognitoConfig'
[
[
{
"UserPoolArn": "arn:aws:cognito-idp:ap-northeast-1:123456789012:userpool/ap-northeast-1_hato",
"UserPoolClientId": "hatoClientId",
"UserPoolDomain": "hato-new",
"SessionCookieName": "AWSELBAuthSessionCookie",
"Scope": "openid",
"SessionTimeout": 604800,
"OnUnauthenticatedRequest": "authenticate"
}
]
]
参考資料
UserPoolDomain
The domain prefix or fully-qualified domain name of the Amazon Cognito user pool.