Skip to content

Commit 44de28d

Browse files
authored
Merge pull request aws-samples#2898 from jojo786/jojo786-feature-apigw-lambda-dsql
feature-apigw-lambda-dsql: use Aurora DSQL Connectors for Python
2 parents d936e0a + 127404a commit 44de28d

6 files changed

Lines changed: 17 additions & 89 deletions

File tree

apigw-lambda-dsql/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Amazon Aurora DSQL is the fastest serverless, distributed SQL database with active-active high availability and multi-Region strong consistency. Aurora DSQL enables you to build always available applications with virtually unlimited scalability, the highest availability, and zero infrastructure management. It is designed to make scaling and resilience effortless for your applications and offers the fastest distributed SQL reads and writes.
44

5-
This pattern deploys a API Gateway REST API, Lambda function and an Aurora DSQL PostgreSQL cluster.
5+
This pattern deploys a API Gateway REST API, Lambda function and an Aurora DSQL PostgreSQL cluster. The Lambda function uses Aurora DSQL Connectors for Python that simplify IAM authorization for customers using standard PostgreSQL drivers to connect to Aurora DSQL clusters.
66

77
Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-lambda-dsql)
88

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@
3333
{
3434
"text": "Amazon Aurora DSQL samples",
3535
"link": "https://github.com/aws-samples/aurora-dsql-samples/tree/main/python"
36+
},
37+
{
38+
"text": "Connectors for Aurora DSQL",
39+
"link": "https://aws.amazon.com/about-aws/whats-new/2025/11/aurora-dsql-python-node-js-jdbc-connectors-iam/"
3640
}
3741
]
3842
},

apigw-lambda-dsql/example-pattern.json

Lines changed: 0 additions & 62 deletions
This file was deleted.

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: 3 additions & 3 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,12 +42,12 @@ 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:
4949
- Key: project
50-
Value: "apigw-lambda-dsql"
50+
Value: "apigw-lambda-dsql"
5151

5252
Outputs:
5353
# ServerlessRestApi is an implicit API created out of Events key under Serverless::Function

0 commit comments

Comments
 (0)