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.
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.