Menghubungkan ke klaster DB menggunakan autentikasi IAM dan AWS SDK for Python (Boto3) - Amazon Aurora

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Menghubungkan ke klaster DB menggunakan autentikasi IAM dan AWS SDK for Python (Boto3)

Anda dapat menghubungkan ke klaster DB Aurora MySQL atau Aurora PostgreSQL dengan AWS SDK for Python (Boto3) seperti yang dijelaskan berikut ini.

Prasyarat

Berikut adalah prasyarat untuk menghubungkan ke instans DB menggunakan autentikasi IAM:

Selain itu, pastikan pustaka yang diimpor dalam kode sampel ada di sistem Anda.

Contoh

Contoh kode menggunakan profil untuk kredensial bersama. Untuk informasi tentang cara menentukan kredensial, lihat Kredensial dalam dokumentasi AWS SDK for Python (Boto3).

Contoh kode berikut ini menunjukkan cara membuat token autentikasi, lalu menggunakannya untuk menghubungkan ke instans DB.

Untuk menjalankan contoh kode ini, Anda memerlukan AWS SDK for Python (Boto3), yang ada di situs AWS.

Ubah nilai variabel berikut sesuai kebutuhan:

  • ENDPOINT – Titik akhir instans DB yang ingin Anda akses

  • PORT – Nomor port yang digunakan untuk menghubungkan ke instans DB Anda

  • USER – Akun basis data yang ingin Anda akses

  • REGION – Wilayah AWS tempat instans DB beroperasi

  • DBNAME – Basis data yang ingin Anda akses

  • SSLCERTIFICATE – Jalur lengkap ke sertifikat SSL untuk Amazon Aurora

    Untuk ssl_ca, tentukan sertifikat SSL. Untuk mengunduh sertifikat SSL, lihat .

catatan

Anda tidak dapat menggunakan data DNS Route 53 kustom atau titik akhir kustom Aurora sebagai pengganti titik akhir instans DB untuk menghasilkan token autentikasi.

Kode ini terhubung ke klaster DB Aurora MySQL.

Sebelum menjalankan kode ini, instal driver PyMySQL dengan mengikuti petunjuk dalam Indeks Paket Python.

import pymysql import sys import boto3 import os ENDPOINT="mysqlcluster.cluster-123456789012.us-east-1.rds.amazonaws.com" PORT="3306" USER="jane_doe" REGION="us-east-1" DBNAME="mydb" os.environ['LIBMYSQL_ENABLE_CLEARTEXT_PLUGIN'] = '1' #gets the credentials from .aws/credentials session = boto3.Session(profile_name='default') client = session.client('rds') token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USER, Region=REGION) try: conn = pymysql.connect(host=ENDPOINT, user=USER, passwd=token, port=PORT, database=DBNAME, ssl_ca='SSLCERTIFICATE') cur = conn.cursor() cur.execute("""SELECT now()""") query_results = cur.fetchall() print(query_results) except Exception as e: print("Database connection failed due to {}".format(e))

Kode ini terhubung ke klaster DB Aurora PostgreSQL.

Sebelum menjalankan kode ini, instal psycopg2 dengan mengikuti petunjuk dalam dokumentasi Psycopg.

import psycopg2 import sys import boto3 import os ENDPOINT="postgresmycluster.cluster-123456789012.us-east-1.rds.amazonaws.com" PORT="5432" USER="jane_doe" REGION="us-east-1" DBNAME="mydb" #gets the credentials from .aws/credentials session = boto3.Session(profile_name='RDSCreds') client = session.client('rds') token = client.generate_db_auth_token(DBHostname=ENDPOINT, Port=PORT, DBUsername=USER, Region=REGION) try: conn = psycopg2.connect(host=ENDPOINT, port=PORT, database=DBNAME, user=USER, password=token, sslrootcert="SSLCERTIFICATE") cur = conn.cursor() cur.execute("""SELECT now()""") query_results = cur.fetchall() print(query_results) except Exception as e: print("Database connection failed due to {}".format(e))

Jika Anda ingin terhubung ke klaster DB melalui proksi, lihat Terhubung ke sebuah proksi menggunakan autentikasi IAM.