简体   繁体   English

EventBridge 调用 Lambda 作为目标

[英]EventBridge invokes Lambda as a target

We are designing a solution which has a Lambda function as a source and another Lambda function as a target to EventBridge.我们正在设计一个解决方案,其中一个 Lambda function 作为源,另一个 Lambda function 作为 EventBridge 的目标。 I read that for the put_rule part, the RoleArn has the ARN for the role which the rule will use needs to have the permission to invoke the target Lambda function. But for Lambda we don't use IAM roles, but use resource bases policies instead.我读到对于put_rule部分, RoleArn具有规则将使用的角色的 ARN,需要具有调用目标 Lambda function 的权限。但是对于 Lambda,我们不使用 IAM 角色,而是使用资源库策略.

The question is, where we are specifying the resource based policy in the code.问题是,我们在代码中的何处指定基于资源的策略。 In the RoleArn ?RoleArn And what's the fields in the targets part for the target Lambda function?目标 Lambda function 的目标部分的字段是什么?

eventclient = boto3.client('events')

response = eventclient.put_rule(
    Name='notificationScheduler',
    ScheduleExpression='at(2023-02-01T02:30:00)',
    State='ENABLED',
    Description='schedule notifications reminders '
    **RoleArn**='string', ## The ARN for the role which the rule will use needs to have the permission to invoke the target lambda function
)

response = eventclient.put_targets(
    Rule='notificationScheduler',
    Targets=[{ ??? }]
)

You must use a Lambda resource policy to grant EventBridge invoke permissions, not a role.您必须使用 Lambda 资源策略来授予 EventBridge 调用权限,而不是角色。

Docs : Amazon SQS, Amazon SNS, Lambda, CloudWatch Logs, and EventBridge bus targets do not use roles, and permissions to EventBridge must be granted via a resource policy. 文档:Amazon SQS、Amazon SNS、Lambda、CloudWatch Logs 和 EventBridge 总线目标不使用角色,必须通过资源策略授予对 EventBridge 的权限。 API Gateway targets can use either resource policies or IAM roles. API 网关目标可以使用资源策略或 IAM 角色。

Create the Lambda permissions (= resource policy) with the AddPermission API:使用AddPermission API 创建 Lambda 权限(= 资源策略):

lambdaclient.add_permission(FunctionName=func_name, StatementId="MyPermissionId", Action="lambda:Invoke", Principal="events.amazonaws.com")

Add the Lambda function as a rule target by ARN:通过 ARN 添加 Lambda function 作为规则目标:

eventclient.put_targets(Rule="notificationScheduler",  Targets=[{"Id": "MyLambdaTarget", "Arn": func_arn }])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM