'Use AWS CDK to trigger AWS Glue Worklfow from EventBridge

As mentioned in this link, it's not supported by Level 2 constructs. But, it's possible to use Level 1 Constructs to implement it. Can anyone show me how to do it using Level 1 constructs, or give me an example? Many thanks.



Solution 1:[1]

I just finished setting this up for my own use case. I'd be interested in hearing how others have done it is my solution seems like a sideways approach. The best I could figure out was to create a glue workflow with an EventBridge trigger. Then configure an Event rule with your workflow as the target. You should be able to configure S3 to post events directly however no events were showing up so I ended up using a intermediate Trail.

obj1 = events.CfnRule(self, 'tgr_id',
            #pattern to tgr on s3 Create Object events
            event_pattern={
                "source": ["aws.s3"],
                "detail-type": ["AWS API Call via CloudTrail"],
                "detail": {
                    "eventSource": ["s3.amazonaws.com"],
                    "eventName": ["PutObject", "CompleteMultipartUpload", "CopyObject", "RestoreObject"],
                    "requestParameters": {
                        "bucketName": ["bucket-name"],
                        "key": [{"prefix":"sample/"}]
                    }
                }
            },
            targets=[events.CfnRule.TargetProperty(
                arn=f'arn:aws:glue:{region}:{account-id}:workflow/{glue_workflow_name}',
                id=glue_wf_id,
                role_arn='arn:aws:iam::{account-id}:role/service-role/{role-name}'
            )]
        )

Solution 2:[2]

Here is the way I found. Very similar to Zcauchon's. The result looks like the one I created manually. But the code still looks hacky.

    trigger = glue.CfnTrigger(
        self,
        id=f"{job.name}-trigger",
        workflow_name=workflow.name,
        actions=[
            glue.CfnTrigger.ActionProperty(
                job_name=job.name,
            )
        ],
        type="EVENT",
        name=f"{job.name}-trigger",
        description=f"Scheduled run for {job.name}.",
    )

    event_pattern = {
        "source": ["aws.s3"],
        "detail-type": ["Object Created"],
        "detail": {
            "bucket": {
                "name": ["name"],
            },
            "object": {
                "key": [
                    {
                        "prefix": "prefix"
                    }
                ]
            },
        },
    }
    workflow_arn = (
        f"arn:aws:glue:{self.region}:{self.account}:workflow/{workflow_id}"
    )
    role = self._create_iam_role(workflow_arn)
    rule = CfnRule(
        self,
        "id",
        event_pattern=event_pattern,
        name="name",
        role_arn=role.attr_arn,
        targets=[
            CfnRule.TargetProperty(
                arn=workflow_arn,
                id="id",
                role_arn=role.attr_arn,
            )
        ],
    )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zcauchon
Solution 2 Gao Hao