Cognito ユーザープールの Lambda トリガーに関数のバージョンやエイリアスを指定してみた

2023.04.10

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

いわさです。

先日、Cognito ユーザープールで Lambda トリガーを使ってサインインプロセスをカスタマイズする方法を紹介しました。

Lambda トリガーでは、その名のとおり Cognito 側でどの Lambda 関数を使うかを指定します。
その際に、バージョンやエイリアスを指定することが出来ないことに気が付きました。

開発中はともかく、運用環境では LATEST ではなくてバージョンやエイリアスを使うことが多いと思います。
Cognito の Lambda トリガーでもバージョン使いたいぞと思っていたところ、コンソールを見る限りどうやら概念としてはありそうです。

本日は実際に Lambda トリガーでバージョンやエイリアスを使ってみましたので設定方法や注意点を紹介したいと思います。

コンソールから指定出来ない

まず、本日時点ではマネジメントコンソール上からの設定はどうやら出来ないようでした。
新コンソールと旧コンソールがあるのでそれぞれ確認してみましょう。

新コンソール

新コンソールでは次のように関数名の指定のみでした。(選択式)
バージョンやエイリアスを設定する箇所はありません。

旧コンソール

旧コンソールも同様に関数名の指定のみでした。(選択式)

AWS CLI からは指定可能

ユーザプールを更新する以下のコマンドで Lambda トリガーに関する情報を指定することが出来ます。

ここでは関数 ARN を指定することが出来るので、バージョン込みの ARN を指定してみましょう。

% cat updatelambda.json                                                     
{
    "UserPoolId": "ap-northeast-1_SjpNJWhzZ",
    "LambdaConfig": {
        "PreAuthentication": "arn:aws:lambda:ap-northeast-1:123456789012:function:cognito-pre-signin:2"
    },
    "AutoVerifiedAttributes": [
        "email"
    ],
    "UserAttributeUpdateSettings": {
        "AttributesRequireVerificationBeforeUpdate": [
            "email"
        ]
    }
}
% aws cognito-idp update-user-pool --cli-input-json file://updatelambda.json

エラーなく更新することが出来ました。

バージョン指定したので実行してみる

おそらくバージョンを指定出来たので、サインインしてみましょう。
指定したバージョンの関数は以下の内容となっています。(エラーメッセージがバージョンごとに異なる)

実行したところ失敗しました。
が、エラーメッセージを見る限りだとアクセス権限がないということで実行をしようとはしているように見えます。

% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UnexpectedLambdaException) when calling the InitiateAuth operation: PreAuthentication invocation failed due to error AccessDeniedException.

Lambda 関数ではバージョンごとにリソースベースポリシーの設定が必要で、新しく作成したバージョンを Cognito から呼び出すためのポリシーを指定していませんでした。
次のように追加しましょう。

権限付与したところ、次のようにサインイン失敗時に関数バージョン 2 のエラーメッセージが表示されました。   成功です。やったね。

% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UserLambdaValidationException) when calling the InitiateAuth operation: PreAuthentication failed with error hoge cannot authenticate.2.

エイリアスも使える

ARN でバージョンが指定出来たので、エイリアスもおそらくいけるでしょう。
試してみます。

% cat updatelambda.json                                                           
{
    "UserPoolId": "ap-northeast-1_SjpNJWhzZ",
    "LambdaConfig": {
        "PreAuthentication": "arn:aws:lambda:ap-northeast-1:123456789012:function:cognito-pre-signin:hoge"
    },
    "AutoVerifiedAttributes": [
        "email"
    ],
    "UserAttributeUpdateSettings": {
        "AttributesRequireVerificationBeforeUpdate": [
            "email"
        ]
    }
}
% aws cognito-idp update-user-pool --cli-input-json file://updatelambda.json

指定したエイリアスで加重エイリアスで 2 つのバージョンを指定してみます。

再びサインインを試したところ、エイリアスが機能していることがわかりますね。
2 つのバージョン間で振り分けされています。素敵です。

% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UserLambdaValidationException) when calling the InitiateAuth operation: PreAuthentication failed with error hoge cannot authenticate.2.
% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UserLambdaValidationException) when calling the InitiateAuth operation: PreAuthentication failed with error hoge cannot authenticate.1.
% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UserLambdaValidationException) when calling the InitiateAuth operation: PreAuthentication failed with error hoge cannot authenticate.2.
% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UserLambdaValidationException) when calling the InitiateAuth operation: PreAuthentication failed with error hoge cannot authenticate.2.
% aws cognito-idp initiate-auth --cli-input-json file://AllowUserPasswordAuth.json

An error occurred (UserLambdaValidationException) when calling the InitiateAuth operation: PreAuthentication failed with error hoge cannot authenticate.1.

注意:コンソールにはバージョンやエイリアスが表示されない

コンソールに Lambda トリガーのバージョン表示エリアがあったので、CLI から関数バージョンを指定すれば表示されるのかなと思って試してみましたが、表示されませんでした。

また、関数を編集しようとすると何も選択されていない状態となっています。
トリガー更新時はご注意ください。

旧コンソールの場合は関数名ではなく ARN が表示されるようになっていました。
ただし、こちらも編集時にバージョンやエイリアスの選択は出来ませんでした。

以上からもし Cognito で Lambda トリガーが期待と異なる動きをしていたら、バージョンやエイリアスが設定されている可能性があります。
AWS CLI だと取得出来るのでこちらで確認すると確実です。

% aws cognito-idp describe-user-pool --user-pool-id ap-northeast-1_SjpNJWhzZ | jq '.UserPool.LambdaConfig'
{
  "PreAuthentication": "arn:aws:lambda:ap-northeast-1:123456789012:function:cognito-pre-signin:hoge"
}

さいごに

本日は Cognito ユーザープールの Lambda トリガーに Lambda バージョンやエイリアスを指定してみました。

コンソールからの指定は出来ませんでしたが AWS CLI からは指定することが出来ました。
指定後にバージョンもエイリアスが正常に機能していることも確認することが出来ました。
ただし、試したところ動きはしましたが、本日時点で公式ドキュメント上には記述が見当たらないので、今後も使用出来るのかはわかりません。

また、本日時点では設定後もコンソールに表示されません。Lambda トリガーを使っている方はこのあたりの挙動を知っておくと良さそうですね。