How to use Boto3 to get the details of a database from AWS Glue Data Catalog?

Boto3PythonServer Side ProgrammingProgramming

Problem Statement − Use boto3 library in Python to retrieve the definition of a database.

Example − Retrieve the definition of a database ‘QA-test’.

Approach/Algorithm to solve this problem

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

Step 2database_name is the mandatory parameter. It fetches definition of given database.

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 get_database function and pass the database_name as Name parameter.

Step 6 − It returns the definition of a given database.

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


Use the following code to retrieve the definition of a database −

import boto3
from botocore.exceptions import ClientError

def retrieves_database_details(database_name)
   session = boto3.session.Session()
   glue_client = session.client('glue')
      response = glue_client.get_database(Name = database_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in retrieves_database_details: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in retrieves_database_details: " + e.__str__())
Published on 23-Mar-2021 06:33:31