I'am using sqlalchemy and psycopg2 to connect python to redshift.
engine = create_engine('postgresql://user:password@hostname:port/database_name')
I want to avoid using my password to connect to redshift and using IAM Role.
I'am using sqlalchemy and psycopg2 to connect python to redshift.
engine = create_engine('postgresql://user:password@hostname:port/database_name')
I want to avoid using my password to connect to redshift and using IAM Role.
Since some time ago AWS has a native Redshift connector for Python.
It supports connecting using IAM, given your IAM credentials allows you to call get-cluster-credentials.
Example:
import redshift_connector
conn = redshift_connector.connect(
iam=True,
database='dev',
db_user='<username>', # the database user in call to get-cluster-credentials
cluster_identifier='my-redshift-cluster', # identifier of your cluster
profile='redshift_profile' # profile in ~./aws/config with correct permissions
)
cursor = redshift_connector.Cursor = conn.cursor()
cursor.execute('SELECT 1;')
A nice feature of this connector is that it calls describe-clusters internally using the cluster_identifier, so you don't even need to specify host and port.