Updating EventBridge Target in a separate CloudFormation script

0

Hello!

I have two separate serverless.yml projects that have to interact with an EventBridge rule and I'm struggling to figure out how to update the resources section in both to support this. Here's an example of what I mean:

EntryPoint Lambda:

This lambda gets the request from the front end and then submits a message to the EventBridge rule we have setup. The EventBridge rule is setup in the serverless.yml file like so:

resources:
  Resources:
    RegisterEventBridge:
      Type: "AWS::Events::Rule"
      Properties:
        Name: "RegisterCloseEventBridge"
        Description: "Event rule for Closing Register event."
        State: "ENABLED"
        EventPattern:
          source:
            - "register.close.${self:provider.stage}"

Consumer:

This lambda function gets triggered by a SQS message. The SQS is a target for the EventBridge rule so it seems like the queue itself should be created in this service. Below is the resources:

resources:
  Resources:
    ClosingRegisterQueue:
      Type: "AWS::SQS::Queue"
      Properties:
        QueueName: "closing-register-sqs-${self:provider.stage}"
    RegisterEventBridge:
      Type: "AWS::Events::Rule"
      Properties:
        Name: "RegisterCloseEventBridge"
        EventPattern:
          source:
            - "register.close.${self:provider.stage}"
        Targets:
          - Arn: !GetAtt ClosingRegisterQueue.Arn
            Id: "SA"

But obviously this does not work because the RegisterEventBridge rule already exists in the previous stack. Is there any way I can simply import it into this stack for this purpose?

1 Answer
1
Accepted Answer

You should think of the rule with all its targets as a single entity. The idea is that the consumer of the event knows what events it is interested in.

Even if you have different targets that need the same rule, they should create two different rules. The exception is that if the two targets are part of the same sub system and are defined in the same template . In that case you can create a single rule with multiple targets.

So in your case I would create the rule with the SQS queue and the Lambda function in the same template.

profile pictureAWS
EXPERT
Uri
answered 2 years ago
  • Okay that makes sense. I was putting it in the Entry Point because that lambda itself submits to the EventBridge rule... So I had assumed that means that the service itself should create it. It makes it way easier to keep EventBridge and SQS inside of the single Consumer lambda. Thank you for your assistance!

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