How to use Boto3 to delete a crawler from AWS Glue Data Catalog?

Boto3PythonServer Side ProgrammingProgramming

Problem Statement − Use boto3 library in Python to delete a crawler that is created in your account.

Example − Delete a crawler ‘Portfolio’ that is created in your account.

Approach/Algorithm to solve this problem

Step 1 − Import boto3 and botocore exceptions to handle exceptions.

Step 2 − Pass the parameter crawler_name that should be deleted from AWS Glue Catalog.

Step 3 − Create an AWS session using boto3 library. 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 delete_crawler function and pass the crawler_name as Name parameter.

Step 6 − It will delete the crawler and return the response metadata.

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

Example

Use the following code to delete a crawler from AWS Glue Data Catalog −

import boto3
from botocore.exceptions import ClientError

def delete_a_crawler(crawler_name):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.delelte_crawler(Name=crawler_name)
      return response
   except ClientError as e:
      raise Exception( "boto3 client error in delete_a_crawler: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in delete_a_crawler: " + e.__str__())

print(delete_a_crawler("Portfolio"))

Output

{'ResponseMetadata': {'RequestId': '067b667f-0a74d4f30a5b',
'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Sat, 27 Feb 2021
14:54:30 GMT', 'content-type': 'application/x-amz-json-1.1', 'contentlength': '2', 'connection': 'keep-alive', 'x-amzn-requestid': '067b667f0a10-4f99-91be-0a74d4f30a5b'}, 'RetryAttempts': 0}}
raja
Published on 22-Mar-2021 08:29:59
Advertisements