How to use Boto3 to get the list of triggers present in an AWS account

AWSBoto3PythonServer Side ProgrammingProgramming

In this article, we will see how a user can get the list of all triggers present in an AWS account.

Example

Get the list of all triggers available in an AWS Glue Data Catalog.

Problem Statement: Use boto3 library in Python to get the list of all triggers.

Approach/Algorithm to solve this problem

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

  • Step 2: There are no parameters in this function.

  • Step 3: Create an AWS session using boto3 lib. Make sure region_name is mentioned in 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: Now use the list_triggers function.

  • Step 6: It returns the list of all triggers present in AWS Glue data catalog. If there are no triggers, it returns an empty dict. However, this function takes optional parameter as Tags so that the user can filter out triggers and return only those triggers associated with tags.

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

Example Code

The following code fetches the list of all triggers −

import boto3
from botocore.exceptions import ClientError

def list_of_triggers()
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      triggers = glue_client.list_triggers()
      return triggers
   except ClientError as e:
      raise Exception("boto3 client error in list_of_triggers: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in list_of_triggers: " + e.__str__())
print(list_of_triggers())

Output

{'TriggerNames':
['data-etl-file-passed-to-splitter',
'file-passed-to-worker',
'file-trigger',
'test-daily-jobs',
'test-daily-jobs-copy'
],
'ResponseMetadata': {'RequestId': '8e95115b****************90', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 27 Mar 2021 09:14:03 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '304', 'connection': 'keep-alive', 'x-amzn-requestid': '8e95115b*********************90'}, 'RetryAttempts': 0}}
raja
Published on 15-Apr-2021 16:18:47
Advertisements