|
1 | | -#Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 1 | + #Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | #SPDX-License-Identifier: MIT-0 |
3 | 3 |
|
4 | 4 | import json |
5 | | -import boto3 |
6 | | -import psycopg2 |
7 | | -import psycopg2.extensions |
| 5 | +import aurora_dsql_psycopg2 as dsql |
8 | 6 | import os |
9 | 7 |
|
10 | 8 | cluster_endpoint = os.environ['cluster_endpoint'] |
11 | 9 | region = os.environ['AWS_REGION'] |
12 | 10 |
|
13 | | -client = boto3.client("dsql", region_name=region) |
14 | | - |
15 | 11 | def lambda_handler(event, context): |
16 | | - # Generate a fresh password token for each connection, to ensure the token is not expired when the connection is established |
17 | | - password_token = client.generate_db_connect_admin_auth_token(cluster_endpoint, region) |
18 | | - |
19 | | - conn_params = { |
20 | | - "dbname": "postgres", |
21 | | - "user": "admin", |
22 | | - "host": cluster_endpoint, |
23 | | - "port": "5432", |
24 | | - "sslmode": "require", |
25 | | - "password": password_token |
| 12 | + config = { |
| 13 | + 'host': cluster_endpoint, |
| 14 | + 'region': region, |
| 15 | + 'user': "admin", |
26 | 16 | } |
27 | 17 |
|
28 | | - # Use the more efficient connection method if it's supported. |
29 | | - if psycopg2.extensions.libpq_version() >= 170000: |
30 | | - conn_params["sslnegotiation"] = "direct" |
31 | | - |
32 | | - # Make a connection to the cluster |
33 | | - conn = psycopg2.connect(**conn_params) |
| 18 | + # Make a connection to the cluster |
| 19 | + conn = dsql.connect(**config) |
34 | 20 |
|
35 | 21 | try: |
36 | 22 | with conn.cursor() as cur: |
|
0 commit comments