amazon-web-servicesaws-lambdaaws-event-bridgeaws-dms

How to create a single CloudWatch EventBridge rule for multiple AWS DMS tasks finishing full load?


I'm using AWS DMS to migrate data from several sources into Redshift. For each replication task, I want to trigger a Lambda function when the full load finishes. Currently, I'm creating a separate AWS::Events::Rule for each DMS task like this:

EventPattern: !Sub |
  {
    "detail-type": ["DMS Replication Task State Change"],
    "source": ["aws.dms"],
    "account": ["${AWS::AccountId}"],
    "region": ["${AWS::Region}"],
    "resources": ["${DBToRedshiftPart1FullLoad}"],
    "detail": {
      "type": ["REPLICATION_TASK"],
      "category": ["StateChange"],
      "eventType": ["REPLICATION_TASK_STOPPED"],
      "eventId": ["DMS-EVENT-0079"]
    }
  }

But this becomes unmanageable as I have many replication tasks. Is there a way to define a single EventBridge rule that matches multiple (or all) DMS tasks and triggers a Lambda function with the ARN of the task that stopped passed in the event? I know I can have something like:

"resources": ["${DBToRedshiftPart1FullLoad}", "${DBToRedshiftPart2FullLoad}"]

But I don't know how to pass the ARN of the DMS task which exactly triggered this event to the Lambda. I want a single EventBridge rule that matches all my DMS tasks and passes the task ARN (i.e., the one that stopped) to the Lambda so it knows which task triggered it.

Ideally, I'd like to avoid duplicating rules for each task in CloudFormation. Is it possible to dynamically pass the triggering resource ARN in the event input or extract it from the event structure inside the Lambda?

Any suggestions or examples would be appreciated.


Solution

  • Looks like this one did the job

    DmsFullLoadTaskStoppedEventRule:
      Type: AWS::Events::Rule
      Properties:
        Name: DMSFullLoadTaskStoppedRule
        State: ENABLED
        Description: "Trigger Lambda after specific DMS full load tasks stop"
        Targets:
          - Arn: !GetAtt SomeLambdaFunction.Arn
            Id: "LambdaTarget"
            InputTransformer:
              InputPathsMap:
                taskArn: "$.resources[0]"
              InputTemplate: |
                {
                  "phase": "DMS_FULL_LOAD_ENDED",
                  "task_arn": "<taskArn>"
                }
        EventPattern: !Sub |
          {
            "detail-type": ["DMS Replication Task State Change"],
            "source": ["aws.dms"],
            "account": ["${AWS::AccountId}"],
            "region": ["${AWS::Region}"],
            "resources": ["${SomeDmsTask1}", "${SomeDmsTask2}"],
            "detail": {
              "type": ["REPLICATION_TASK"],
              "category": ["StateChange"],
              "eventType": ["REPLICATION_TASK_STOPPED"],
              "eventId": ["DMS-EVENT-0079"]
            }
          }