如何解决为什么将 Lambda 函数作为目标添加到 Event Bridge 规则未触发?
我正在尝试为 EventBridge 的规则添加一个规则和一个 lambda 目标。规则和目标添加成功,但是没有触发目标Lambda。当我导航到 AWS 控制台上的目标 Lambda 函数时,我看不到相关的触发器。
我正在使用无服务器框架来部署堆栈。 putRuleAndTarget
lambda 是创建规则并添加 notifyUser
作为目标的那个。以下是 serverless.ts
文件的相关部分:
functions: {
putRuleAndTarget: {
handler: 'src/functions/putRuleAndTarget.handler',name: 'putRuleAndTarget'
},notifyUser: {
handler: 'src/functions/notifyUser.handler',name: 'notifyUser'
}
}
这是 putRuleAndTarget
的代码(我使用的是 aws javascript sdk v3):
import { EventBridgeClient,PutRuleCommand,PutTargetsCommand,} from '@aws-sdk/client-eventbridge'
import type { PutRuleCommandInput,PutTargetsCommandInput } from '@aws-sdk/client-eventbridge'
const client = new EventBridgeClient({ region: 'eu-central-1' })
async function putRuleAndTarget( ) {
try {
const putRuleCommandInput: PutRuleCommandInput = {
Name: 'notifyUserRule',Description: 'Schedule a lambda to send notification to the user',ScheduleExpression: 'rate(1 minute)',}
const putRuleCommand = new PutRuleCommand(putRuleCommandInput)
const putRuleCommandOutput = await client.send(putRuleCommand)
const putTargetsCommandInput: PutTargetsCommandInput = {
Rule: 'notifyUserRule',Targets: [
{
Arn: 'arn:aws:lambda:eu-central-1:AccountID:function:notifyUser',Id: 'notifyUser'
},]
}
const putTargetsCommand = new PutTargetsCommand(putTargetsCommandInput)
const putTargetsCommandOutPut = await client.send(putTargetsCommand)
} catch (error) {
console.log('[ putRuleAndTarget error ]',error)
}
}
export const handler = putRuleAndTarget
运行putRuleAndTarget
后截图如下
在 AWS EventBridge 控制台上创建的规则:
The rule on EventBridge Console
显示目标的规则详细信息: Rule details and Target on EventBridge Console
Lambda 控制台上的notifyUser
函数,请注意缺少触发器:
notifyUser function on Lambda Console
此外,运行 sls logs -f notifyUser
不会产生任何日志语句,该函数永远不会被调用,尽管它应该每 1 分钟运行一次。
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。