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

0

0

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.

1 Answer
0

Yes, it's possible to create a single EventBridge rule that matches multiple or all AWS DMS tasks and triggers a Lambda function with the ARN of the specific task that stopped. Here's how you can achieve this:

  1. Create a more generic EventBridge rule that matches all DMS replication task state changes:
EventPattern:
  {
    "detail-type": ["DMS Replication Task State Change"],
    "source": ["aws.dms"],
    "detail": {
      "type": ["REPLICATION_TASK"],
      "category": ["StateChange"],
      "eventType": ["REPLICATION_TASK_STOPPED"],
      "eventId": ["DMS-EVENT-0079"]
    }
  }

This pattern will match any DMS replication task that stops after completing a full load, regardless of the specific task ARN.

  1. In your Lambda function, you can extract the specific task ARN from the event object. The event structure includes a 'resources' array that contains the ARN of the DMS task that triggered the event. You can access it like this in your Lambda function:
def lambda_handler(event, context):
    task_arn = event['resources'][0]
    # Your logic here using the task_arn

This approach allows you to have a single EventBridge rule that works for all your DMS tasks, and your Lambda function can determine which specific task triggered it by examining the event object.

By using this method, you avoid creating separate rules for each DMS task, making your CloudFormation template more manageable and scalable. The Lambda function will receive all the necessary information about the event, including the specific task that completed, allowing you to perform any required actions based on the task ARN.
Sources
Creating rules that react to events in Amazon EventBridge - Amazon EventBridge
Scenarios for EventBridge using AWS SDKs - Amazon EventBridge

profile picture
answered 7 days ago
profile picture
EXPERT
reviewed 7 days ago

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