使用CDK创建AWS Cognito时Lambda触发器未自动配置问题
解决AWS CDK创建Cognito用户池时Lambda触发器未自动配置的问题
问题核心是你在配置lambdaTriggers时使用了错误的键名,CDK无法识别为有效的Cognito触发器类型,导致触发器关联未生成。
错误原因
CDK的UserPoolProps中lambdaTriggers属性是UserPoolLambdaTriggers类型,它要求键名必须是Cognito预定义的触发器类型(比如postConfirmation、postAuthentication),而不是你自定义的Lambda变量名。你的代码中直接用postConfirmationLambda作为键,CDK无法将其映射到正确的触发器类型,因此不会创建触发器关联。
修正后的代码
将lambdaTriggers的键名替换为官方定义的触发器类型名称:
const postConfirmationLambda = new NodejsFunction( this, 'PostConfirmLambda', userPoolLambdasProps ); const postAuthenticationLambda = new NodejsFunction( this, 'PostAuthLambda', userPoolLambdasProps ); usersTable.grantReadWriteData(postConfirmationLambda); usersTable.grantReadWriteData(postAuthenticationLambda); const userPool = new UserPool(this, 'UserPool', { removalPolicy: RemovalPolicy.DESTROY, lambdaTriggers: { postConfirmation: postConfirmationLambda, // 使用官方触发器类型名作为键 postAuthentication: postAuthenticationLambda, }, });
验证步骤
- 执行
cdk deploy重新部署栈 - 登录AWS控制台,进入Cognito用户池的触发器页面,确认对应的Lambda触发器已自动配置
- 可查看CloudFormation模板中的
AWS::Cognito::UserPool资源,确认LambdaConfig字段包含了对应触发器的ARN
内容的提问来源于stack exchange,提问作者MartinP
相关产品推荐
相关产品推荐

