Monday 24 May 2021

Connect to Postgres database using AWS Secrets Manager and psycopg2


 
This post explains how to retrieve values from AWS secret manager and use them to connect to the Postgres database.


Requirements:

Library:

  • Boto3
  • psycopg2
Commands to install the required modules:

pip install psycopg2
pip install Boto3


Required Connector Parameters:

  • Host
  • Port
  • Username
  • Password
  • Database

Example Secret Manager: 


Knowing the attribute of the secret manager value helps you to understand the code better.

AW Secret Manager

Code to connect to Postgres database using AWS Secrets Manager and psycopg2 :


import boto3
import json


def get_rds_keys():
    try:
         region_name = 'us-west-2'
        session = boto3.session.Session()
#Replace the string secret_name_rds with your secret manager name
        client = session.client(service_name='secretsmanager',region_name=region_name)
        get_secret_value_response = client.get_secret_value(SecretId=secret_name_rds)  

        if 'SecretString' in get_secret_value_response:
            secret = get_secret_value_response['SecretString']
            secret_val = secret
        else:
            binary_secret_data = get_secret_value_response['SecretBinary']
            secret_val=binary_secret_data

# Every attribute name inside the secret_dict[''] should exact as it in the Secret Manager Key.
        secret_dict = eval(secret_val)
        RDS_USER=secret_dict['username']
        RDS_PASSWORD=secret_dict['password']
        RDS_HOST = secret_dict['host']
        RDS_PORT = secret_dict['port']
        RDS_DATABASE = secret_dict['database']

    except Exception as e:
        print('=== 1.1 ========= RDS Key Retrival - Error Occured : ', str(e))
    finally:
        return RDS_USER, RDS_PASSWORD, RDS_HOST, RDS_PORT, RDS_DATABASE

#Retrives the values of the attributes from the above function.
RDS_USER, RDS_PASSWORD, RDS_HOST, RDS_PORT, RDS_DATABASE  = get_rds_keys()

# Connects to the database using the retrieved attribute values
rdsData = psycopg2.connect(
    database=RDS_DATABASE,
    user=RDS_USER,
    password=RDS_PASSWORD,
    host=RDS_HOST,
    port=RDS_PORT
)

rds_cur = rdsData.cursor()

rds_cur.execute(select * from table;)

result = rds_cur.fetchall()

print(result)

No comments:

Post a Comment