Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to use Boto3 to get the details of multiple triggers at a time?
Problem Statement − Use boto3 library in Python to get the triggers that are available in your account. For example, get the details of triggers that are allowed in your AWS Glue account.
Approach/Algorithm to solve this problem
Step 1 − Import boto3 and botocore exceptions to handle exceptions.
Step 2 − No parameter is required for this function. It will fetch all the listed triggers for the user account and then display the metadata of each trigger.
Step 3 − Create an AWS session using boto3 library. Make sure the 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 − Now use list_triggers function to get all triggers those are listed in user account.
Step 6 − Call batch_get_triggers and pass the trigger names fetched in previous function.
Step 7 − It returns list_of_triggers and metadata of each trigger.
Step 8 − Handle the generic exception if something went wrong while checking the triggers.
Example
Use the following code to fetch the details of each trigger listed in user account −
import boto3
from botocore.exceptions import ClientError
def get_resource_metadata_of_triggers():
session = boto3.session.Session()
glue_client = session.client('glue')
try:
list_of_triggers = glue_client.list_triggers()
response = glue_client.batch_get_triggers(TriggerNames=list_of_triggers['TriggerNames'])
return list_of_triggers, response
except ClientError as e:
raise Exception("boto3 client error in get_resource_metadata_of_triggers: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_resource_metadata_of_triggers: " + e.__str__())
# Call the function
triggers_list, triggers_metadata = get_resource_metadata_of_triggers()
# List of Triggers
print("List of Triggers:")
print(triggers_list)
# Resource metadata of each Trigger
print("\nResource metadata of each Trigger:")
print(triggers_metadata)
Output
List of Triggers:
{'TriggerNames': ['01_PythonShellTest1'],
'NextToken':
'eyJleHBpcmF0aW9uIjp7InNlY29uZHMiOjE2MTQxNzE2OTksIm5hbm9zIjo1MTYwMDAwMDB
9LCJsYXN0RXZhbHV
zFiMzAzNzAxMzRmNDk3NWM3M2MyMjhjYTk5MDgzZTA3YjQ0ZWEyOTZlIn19fQ==',
'ResponseMetadata': {'RequestId': '5d3eb19a-41f5-b24e-2d59ed9664b5',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Tue, 23 Feb 2021
13:01:39 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-
length': '1134', 'connection': 'keep-alive', 'x-amzn-request-
id': '5d3eb19a-41f5-b24e-2d59ed9664b5'}, 'RetryAttempts': 0}}
Resource metadata of each Trigger:
{'Triggers': [{'Name': '01_PythonShellTest1', 'WorkflowName':
'arn:aws:iam::1234:role/dev-edl', 'Id': 'string', 'Type': 'string',
'State':
'CREATING'|'CREATED'|'ACTIVATING'|'ACTIVATED'|'DEACTIVATING'|'DEACTIVATE
D'|'DELETING'|'UPDATING', 'Description': 'string', 'Schedule': 'string'
}]}
Key Points
The list_triggers() method returns a list of trigger names, while batch_get_triggers() provides detailed metadata for each trigger including:
- Name − The trigger name
- Type − The trigger type (scheduled, on-demand, etc.)
- State − Current state of the trigger
- Schedule − Cron expression for scheduled triggers
- Description − Optional description of the trigger
Conclusion
Use list_triggers() to get all trigger names and batch_get_triggers() to fetch detailed metadata. This approach efficiently retrieves comprehensive information about multiple AWS Glue triggers in a single operation.
