You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用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,
  },
});

验证步骤

  1. 执行cdk deploy重新部署栈
  2. 登录AWS控制台,进入Cognito用户池的触发器页面,确认对应的Lambda触发器已自动配置
  3. 可查看CloudFormation模板中的AWS::Cognito::UserPool资源,确认LambdaConfig字段包含了对应触发器的ARN

内容的提问来源于stack exchange,提问作者MartinP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 09:54:22