Issues with SAM Script: EventBridge Rule Set for Multiple Lambdas Not Targeting Functions

0

I'm currently utilizing serverless architecture with AWS SAM, employing Lambda, EventBridge, and SAM services for my application.

I'm encountering an issue with a Single EventBridge rule set that triggers multiple Lambda functions. The Lambda function names are correctly displayed in the event rule targets with ARN. However, when I inspect the Lambda function triggers, the names are not visible, and as a result, the functions are not being targeted.

Here's the relevant section of my SAM script:

## EventBridge rule to trigger Lambda functions
ScheduleRule:
  Type: AWS::Events::Rule
  Properties:
    Name: ScheduleRule-Event-Lambda
    Description: Event rule for scheduling Lambda
    EventBusName: default
    ScheduleExpression: rate(10 minutes) # Adjust the interval as needed
    State: ENABLED
    Targets:
      - Arn: !GetAtt function1.Arn
        Id: !Sub function1RuleTarget
      - Arn: !GetAtt function2.Arn
        Id: !Sub function2RuleTarget

When attempting to add this rule manually through the console, it works as expected. However, when using SAM, the addition is not allowed. I'm unsure if there's an issue with the script or if it's a permissions-related issue.

My ultimate goal is to establish a single rule for multiple Lambda functions without creating the same rule multiple times. Any insights into potential script or permission issues would be greatly appreciated.

已提問 4 個月前檢視次數 223 次
1 個回答
1

Hello.

Have you set a resource-based policy on Lambda as shown below?

Lambdafunction1Permission:
  Type: AWS::Lambda::Permission
  Properties:
    FunctionName:
      Ref: function1
    Action: lambda:InvokeFunction
    Principal: events.amazonaws.com
    SourceArn: !GetAtt ScheduleRule.Arn

Lambdafunction2Permission:
  Type: AWS::Lambda::Permission
  Properties:
    FunctionName:
      Ref: function2
    Action: lambda:InvokeFunction
    Principal: events.amazonaws.com
    SourceArn: !GetAtt ScheduleRule.Arn
profile picture
專家
已回答 4 個月前
  • Thank you for the prompt response. While this solution seems effective, a potential issue arises when dealing with multiple Lambda functions. Specifically, if there are 20 Lambda functions or more, I would need to add this script to each one individually. Are there alternative methods to address this issue?

  • Lambda's resource-based policy requires resources for each Lambda, so with AWS SAM, I think the only option is to add them one by one. With CloudFormation, I feel like I can loop with "Fn::ForEach", but AWS SAM doesn't seem to be able to use it yet, as shown in the GitHub issue below. https://github.com/aws/aws-sam-cli/issues/4835 For example, if you are using CDK, you can use a for statement to create the same resource in a loop.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南