How to get the details of a trigger from AWS Glue Data catalog using Boto3

AWSBoto3PythonServer Side ProgrammingProgramming

Let's see how a user can get the details of a trigger from AWS Glue Data Catalog.

Example

Get the details of a given trigger that is allowed in your account - '01_PythonShellTest1'.

Approach/Algorithm to solve this problem

  • Step 1: Import boto3 and botocore exceptions to handle exceptions.

  • Step 2: trigger_name is the required parameter for this function. It will fetch the details of the given trigger for a user account and then display its metadata.

  • Step 3: Create an AWS session using boto3 lib. Make sure region_name is mentioned in the default profile. If it is not mentioned, then explicitly pass the region_name while creating the session.

  • Step 4: Create an AWS client for glue.

  • Step 5: Call get_trigger and pass the trigger_name as Name

  • Step 6: It returns the details of the given trigger.

  • Step 8: Handle the generic exception if something went wrong while checking the job.

Example Code

The following code fetches the details of a trigger listed in a user account −

import boto3
from botocore.exceptions import ClientError

def get_resource_maetadata_of_trigger(trigger_name):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_trigger(Name=trigger_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in get_resource_maetadata_of_trigger: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in get_resource_maetadata_of_trigger: " + e.__str__())
a = get_resource_metadat_of_trigger('01_PythonShellTest1')
print(a)

Output

{'Triggers': [{'Name': '01_PythonShellTest1', 'WorkflowName': 'arn:aws:iam::1234:role/dev-edl, 'Id': 'string', 'Type': 'SCHEDULED'|'CONDITIONAL'|'ON_DEMAND', 'State': 'CREATING'|'CREATED'|'ACTIVATING'|'ACTIVATED'|'DEACTIVATING'|'DEACTIVATED'|'DELETING'|'UPDATING', 'Description': 'string', 'Schedule': 'string'
}]}
raja
Published on 15-Apr-2021 12:36:22
Advertisements