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.

asked 3 months ago214 views
1 Answer
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
EXPERT
answered 3 months ago
  • 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.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions