How to get the details of all the triggers associated with a job from AWS Glue Data catalog using Boto3

AWSBoto3PythonServer Side ProgrammingProgramming

In this article, we will see how a user can get the details of all the triggers associated with a job from AWS Glue Data Catalog.

Example

Get the details of all the triggers associated with a job - 'employee_details'.

Problem Statement: Use boto3 library in Python to get the details of all the triggers that is associated with a job.

Approach/Algorithm to solve this problem

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

  • Step 2: job_name is the optional parameter for this function. If job_name is provided, it retrieves all the triggers those are associated with the same job and can start this job. However, if there is no such trigger, it returns all the triggers. By default, value is None, so if the job_name is not passed while calling this function, it will return the details of all the triggers.

  • 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_triggers and pass the job_name as DependentJobName

  • Step 6: It returns the details of triggers associated with this job.

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

Example Code

The following code fetches the details of all the triggers associated with a job −

import boto3
from botocore.exceptions import ClientError

def get_details_of_triggers(job_name=None):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_triggers(DependentJobName=job_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in get_details_of_triggers: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in get_details_of_triggers: " + e.__str__())
a = get_details_of_triggers('employee_details')
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 16:07:31
Advertisements