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?

已提問 2 年前檢視次數 1191 次
1 個回答
1
已接受的答案

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
專家
Uri
已回答 2 年前
  • 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!

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

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

回答問題指南