How to use Boto3 to start a trigger in AWS Glue Data Catalog

AWSBoto3PythonServer Side ProgrammingProgramming

In this article, we will see how a user can start a trigger in AWS Glue Data Catalog.

Example

Problem Statement: Use boto3 library in Python to start a trigger.

Approach/Algorithm to solve this problem

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

  • Step 2: trigger_name is the parameter in this function.

  • 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: Now use the start_trigger function and pass the parameter trigger_name as Name.

  • Step 6: It returns the response metadata and starts the triggers irrespective of its schedule.

  • Step 7: Handle the generic exception if something went wrong while starting a trigger.

Example Code

The following code starts a trigger in AWS Glue Data Catalog −

import boto3
from botocore.exceptions import ClientError

def start_a_trigger(trigger_name)
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.start_trigger(Name=trigger_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in start_a_trigger: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in start_a_trigger: " + e.__str__())
print(start_a_trigger("test-daily"))

Output

{'Name': 'test-daily', 'ResponseMetadata': {'RequestId': 'b2109689-*******************-d', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sun, 28 Mar 2021 08:00:04 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '26', 'connection': 'keep-alive', 'x-amzn-requestid': 'b2109689-***********************-d'}, 'RetryAttempts': 0}}
raja
Published on 15-Apr-2021 12:57:33
Advertisements