我正在尝试使用 CDK (TS) 连接 Lex 与 Lambda。
我正在
Invalid Bot Configuration: Access denied while invoking lambda function arn:aws:lambda:xyz from arn:aws:lex:zyx. Please check the policy on this function
我尝试使用 AWS 控制台应用内联策略。 但没有任何效果。 我试图附加 * 政策 - 也不起作用。
我的代码看起来像这样:
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: ['*'],
}),
],
}),
},
}),
}
);
const bot = new CfnBot(this, 'bot-name', {
name: 'bot-name',
roleArn: role.roleArn,
...
{ testAliasConfig with arn of botLambdaHandler }
});
const botVersion = new CfnBotVersion(this, 'bot-prod-version', {
botId: bot.attrId,
botVersionLocaleSpecification: [
{
localeId: 'en_US',
botVersionLocaleDetails: {
sourceBotVersion: 'DRAFT',
},
},
],
});
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,
},
},
},
},
],
});
我尝试使用 AWS 控制台应用内联策略。没有任何效果:( 我将很高兴得到您的帮助!
Lambda 函数的基于资源的策略不允许 AWS Lex 调用它,因此出现“访问被拒绝,同时调用 lambda 函数”错误。
addPermission(...)
修改策略以允许机器人调用您的 Lambda:
botLambdaHandler.addPermission('lex-fulfillment', {
action: 'lambda:InvokeFunction',
principal: new iam.ServicePrincipal('lex.amazonaws.com'),
});