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

使用CDK(TS)集成AWS Lex与Lambda时遇权限错误求助

问题分析与解决方案

错误Invalid Bot Configuration: Access denied while invoking lambda function...的核心原因是:你的Lambda函数没有配置允许Lex服务调用它的资源策略。你给Bot角色和Lambda执行角色开了全权限,但Lambda的资源策略是控制「谁能调用该函数」的关键,这部分缺失了。

另外你的代码里还有一处明显笔误:创建CfnBot时用了role.roleArn,但你定义的角色是botRole,这里应该改为botRole.roleArn。

具体修复步骤

  1. 修复Bot角色ARN的笔误
    把CfnBot中的roleArn: role.roleArn改为roleArn: botRole.roleArn。

  2. 给Lambda添加资源策略,允许Lex调用
    在创建botLambdaHandler后,添加以下代码:

botLambdaHandler.addPermission('LexInvokePermission', {
  principal: new ServicePrincipal('lex.amazonaws.com'),
  action: 'lambda:InvokeFunction',
  sourceArn: `arn:aws:lex:${this.region}:${this.account}:bot-alias/${bot.attrId}/BotProd`,
});

这里的sourceArn是你的Bot别名的ARN,确保Lex只有这个特定的Bot别名能调用该Lambda,也可以用'*'临时测试,但生产环境建议限定具体资源。

  1. 确保版本与别名的依赖关系(可选但推荐)
    CDK通常会自动处理依赖,但如果出现部署顺序问题,可以给CfnBotAlias添加对Lambda权限的依赖:
const botAlias = new CfnBotAlias(this, 'bot-prod-alias', {
  // ... 你的原有配置
});
botAlias.node.addDependency(botLambdaHandler.permissionsNode.findChild('LexInvokePermission'));

修改后的完整关键代码片段

const botRole = new Role(this, 'bot-role', {
  assumedBy: new ServicePrincipal('lex.amazonaws.com'),
  inlinePolicies: {
    'allow-everything': new PolicyDocument({
      statements: [
        new PolicyStatement({
          effect: Effect.ALLOW,
          resources: ['*'],
          actions: ['*'],
        }),
      ],
    }),
  },
});

const botLambdaHandler = new NodejsFunction(
  this,
  'bot-lambda-handle',
  {
    entry: join(__dirname, 'resources', 'bot-handler.ts'),
    ...nodeJsFunctionProps,
    role: new Role(this, 'bot-execution-role', {
      assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
      inlinePolicies: {
        'allow-everything': new PolicyDocument({
          statements: [
            new PolicyStatement({
              effect: Effect.ALLOW,
              resources: ['*'],
              actions: ['*'],
            }),
          ],
        }),
      },
    }),
  }
);

// 添加Lex调用Lambda的权限
botLambdaHandler.addPermission('LexInvokePermission', {
  principal: new ServicePrincipal('lex.amazonaws.com'),
  action: 'lambda:InvokeFunction',
  sourceArn: `arn:aws:lex:${this.region}:${this.account}:bot-alias/${bot.attrId}/BotProd`,
});

const bot = new CfnBot(this, 'bot-name', {
  name: 'bot-name',
  roleArn: botRole.roleArn, // 修复笔误
  // ... 你的其他配置
});

const botVersion = new CfnBotVersion(this, 'bot-prod-version', {
  botId: bot.attrId,
  botVersionLocaleSpecification: [
    {
      localeId: 'en_US',
      botVersionLocaleDetails: {
        sourceBotVersion: 'DRAFT',
      },
    },
  ],
});

const botAlias = new CfnBotAlias(this, 'bot-prod-alias', {
  botAliasName: 'BotProd',
  botId: bot.attrId,
  botVersion: botVersion.attrBotVersion,
  botAliasTags: tags,
  botAliasLocaleSettings: [
    {
      localeId: 'en_US',
      botAliasLocaleSetting: {
        enabled: true,
        codeHookSpecification: {
          lambdaCodeHook: {
            codeHookInterfaceVersion: '1.0',
            lambdaArn: botLambdaHandler.functionArn,
          },
        },
      },
    },
  ],
});

// 可选:确保权限先于别名创建
botAlias.node.addDependency(botLambdaHandler.permissionsNode.findChild('LexInvokePermission'));

为什么之前的操作没用?

你在控制台给Lambda加的是执行角色的权限,这是Lambda运行时调用其他服务的权限,但谁能调用Lambda是由Lambda的资源策略控制的,这是两个完全不同的权限体系。必须给Lambda添加允许Lex调用的资源策略,才能解决这个权限错误。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 00:03:18