How to use Boto3 to get the details of a connection from AWS Glue Data catalog?


Problem Statement − Use boto3 library in Python to get details of a connection present in AWS Glue Data catalog.

Example − Get the details of a connection definition, ‘aurora-test’.

Approach/Algorithm to solve this problem

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

Step 2 − Pass the parameter connection_name whose definition needs to check.

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 − Call get_connection function and pass the connection_name as Name parameter.

Step 6 − It will fetch the details of the connection definition from AWS Glue Data Catalog.

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

Example

Use the following code to get definition of a connection in AWS Glue Data catalog −

import boto3
from botocore.exceptions import ClientError

def get_details_of_a_connection(connection_name):
   session = boto3.session.Session()
   glue_client = session.client('glue')
   try:
      response = glue_client.get_connection(Name= connection_name)
      return response
   except ClientError as e:
      raise Exception("boto3 client error in get_details_of_a_connection: " + e.__str__())
   except Exception as e:
      raise Exception("Unexpected error in get_details_of_a_connection: " + e.__str__())
print(get_details_of_a_connection("aurora-poc"))

Output

{'Connection': {'Name': 'aurora-poc', 'ConnectionType': 'JDBC',
'ConnectionProperties': {'JDBC_CONNECTION_URL': 'jdbc:postgresql://abcpostgresql-cluster.cluster-abc.us-east-1.rds.amazonaws.com:0132/abc,
'JDBC_ENFORCE_SSL': 'false', 'PASSWORD': '******', 'USERNAME':
'abc***'}, 'PhysicalConnectionRequirements': {'SubnetId': 'subnet351*****', 'SecurityGroupIdList': ['sg-caa******', 'sg-*************'],
'AvailabilityZone': 'us-east-1c'}, 'CreationTime':
datetime.datetime(2020, 11, 18, 12, 38, 29, 625000, tzinfo=tzlocal()),
'LastUpdatedTime': datetime.datetime(2020, 11, 18, 12, 51, 16, 59000,
tzinfo=tzlocal())}, 'ResponseMetadata': {'RequestId': '6f13524b-4175-
454b-bc60-c7f408967098', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date':
'Sun, 28 Feb 2021 11:19:18 GMT', 'content-type': 'application/x-amzjson-1.1', 'content-length': '523', 'connection': 'keep-alive', 'x-amznrequestid': '6f13524b-*****************7098'}, 'RetryAttempts': 0}}

Updated on: 23-Mar-2021

819 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements