How to use Boto3 to get the details of multiple triggers at a time?

Boto3PythonServer Side ProgrammingProgramming

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 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 triggers.

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 jobs those are listed in user account.

Step 6 − Call batch_get_triggers and pass the job names fetched in previous function.

Step 7 − It returns list_of_triggers and metadata of each triggers.

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

Example

Use the following code to fetch the details of each triggers listed in user account −

import boto3
from botocore.exceptions import ClientError

def get_resource_maetadata_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_maetadata_of_triggers: " + e.__str__())
   except Exception as e:
      raise Exception( "Unexpected error in get_resource_maetadata_of_triggers: " + e.__str__())

a, b = get_resource_metadat_of_triggers()
#List of Triggers
print(a)
#Resource metadata of each Triggers
print(b)

Output

#List of Triggers
{'TriggersNames': ['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', 'contentlength': '1134', 'connection': 'keep-alive', 'x-amzn-requestid':
'5d3eb19a-41f5-b24e-2d59ed9664b5'}, 'RetryAttempts': 0}}

#Resource metadata of each Triggers
{'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'
}]}
raja
Published on 22-Mar-2021 12:56:14
Advertisements