Skip to content

Commit d57f829

Browse files
committed
use Aurora DSQL Connectors for Python
1 parent 492a348 commit d57f829

3 files changed

Lines changed: 11 additions & 25 deletions

File tree

apigw-lambda-dsql/src/app.py

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,22 @@
1-
#Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
1+
#Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
22
#SPDX-License-Identifier: MIT-0
33

44
import json
5-
import boto3
6-
import psycopg2
7-
import psycopg2.extensions
5+
import aurora_dsql_psycopg2 as dsql
86
import os
97

108
cluster_endpoint = os.environ['cluster_endpoint']
119
region = os.environ['AWS_REGION']
1210

13-
client = boto3.client("dsql", region_name=region)
14-
1511
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",
2616
}
2717

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)
3420

3521
try:
3622
with conn.cursor() as cur:
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
psycopg2-binary>=2.9
22
botocore>=1.35.74
3-
boto3>=1.35.74
3+
aurora-dsql-python-connector

apigw-lambda-dsql/template.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Resources:
2727
- Statement:
2828
- Effect: Allow
2929
Action:
30-
- dsql:DbConnectAdmin
30+
- dsql:DbConnectAdmin #IAM action dsql:DbConnectAdmin required to connect to the cluster.
3131
Resource:
3232
- !Sub arn:${AWS::Partition}:dsql:${AWS::Region}:${AWS::AccountId}:cluster/${DSQL}
3333
Environment: # Function environment variables
@@ -42,7 +42,7 @@ Resources:
4242
Method: get
4343

4444
DSQL:
45-
Type: AWS::DSQL::Cluster
45+
Type: AWS::DSQL::Cluster #Creates a single-Region DSQL cluster
4646
Properties:
4747
DeletionProtectionEnabled: false
4848
Tags:

0 commit comments

Comments
 (0)