diff --git a/apigw-lambda-dsql/README.md b/apigw-lambda-dsql/README.md index 0014cec51a..11b8378f1f 100644 --- a/apigw-lambda-dsql/README.md +++ b/apigw-lambda-dsql/README.md @@ -2,7 +2,7 @@ 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. -This pattern deploys a API Gateway REST API, Lambda function and an Aurora DSQL PostgreSQL cluster. +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. Learn more about this pattern at [Serverless Land Patterns](https://serverlessland.com/patterns/apigw-lambda-dsql) diff --git a/apigw-lambda-dsql/apigw-lambda-dsql b/apigw-lambda-dsql/apigw-lambda-dsql.json similarity index 94% rename from apigw-lambda-dsql/apigw-lambda-dsql rename to apigw-lambda-dsql/apigw-lambda-dsql.json index b21ce931b2..a19d453600 100644 --- a/apigw-lambda-dsql/apigw-lambda-dsql +++ b/apigw-lambda-dsql/apigw-lambda-dsql.json @@ -33,6 +33,10 @@ { "text": "Amazon Aurora DSQL samples", "link": "https://github.com/aws-samples/aurora-dsql-samples/tree/main/python" + }, + { + "text": "Connectors for Aurora DSQL", + "link": "https://aws.amazon.com/about-aws/whats-new/2025/11/aurora-dsql-python-node-js-jdbc-connectors-iam/" } ] }, diff --git a/apigw-lambda-dsql/example-pattern.json b/apigw-lambda-dsql/example-pattern.json deleted file mode 100644 index 5007dbcc7a..0000000000 --- a/apigw-lambda-dsql/example-pattern.json +++ /dev/null @@ -1,62 +0,0 @@ -{ - "title": "Amazon API Gateway, AWS Lambda and Amazon Aurora DSQL", - "description": "Creates an API Gateway REST API integrated with an AWS Lambda function that connects to an Amazon Aurora DSQL PostgreSQL cluster", - "language": "Python", - "level": "200", - "framework": "AWS SAM", - "introBox": { - "headline": "How it works", - "text": [ - "This sample project demonstrates how to use a Lambda function (invoked by API Gateway), that stores and retrieves data from an Amazon Aurora DSQL PostgreSQL cluster.", - "Aurora DSQL enables you to build always available applications with virtually unlimited scalability, the highest availability, and zero infrastructure management.", - "This pattern deploys a API Gateway REST API, Lambda function and an Aurora DSQL PostgreSQL cluster." - ] - }, - "gitHub": { - "template": { - "repoURL": "https://github.com/aws-samples/serverless-patterns/tree/main/apigw-lambda-dsql", - "templateURL": "serverless-patterns/apigw-lambda-dsql", - "projectFolder": "apigw-lambda-dsql", - "templateFile": "template.yaml" - } - }, - "resources": { - "bullets": [ - { - "text": "Amazon Aurora DSQL, the fastest serverless distributed SQL database is now generally available", - "link": "https://aws.amazon.com/blogs/aws/amazon-aurora-dsql-is-now-generally-available/" - }, - { - "text": "DSQL Vignette: Aurora DSQL, and A Personal Story", - "link": "https://brooker.co.za/blog/2024/12/03/aurora-dsql.html/" - }, - { - "text": "Amazon Aurora DSQL samples", - "link": "https://github.com/aws-samples/aurora-dsql-samples/tree/main/python" - } - ] - }, - "deploy": { - "text": [ - "sam deploy" - ] - }, - "testing": { - "text": [ - "See the GitHub repo for detailed testing instructions." - ] - }, - "cleanup": { - "text": [ - "sam delete" - ] - }, - "authors": [ - { - "name": "Yusuf Mayet", - "image": "https://d2908q01vomqb2.cloudfront.net/9e6a55b6b4563e652a23be9d623ca5055c356940/2021/11/24/Yusuf-mayet-aws.jpg", - "bio": "I am a Solutions Architect at AWS, where I help customers realise that true transformation lies at the intersection of Cloud, DevOps cultural practices, Agile principles, modular and scalable architectures, and efficient team structures.", - "linkedin": "yusufmayet" - } - ] -} diff --git a/apigw-lambda-dsql/src/app.py b/apigw-lambda-dsql/src/app.py index 9ea06f251d..bf4220609d 100644 --- a/apigw-lambda-dsql/src/app.py +++ b/apigw-lambda-dsql/src/app.py @@ -1,36 +1,22 @@ -#Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + #Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. #SPDX-License-Identifier: MIT-0 import json -import boto3 -import psycopg2 -import psycopg2.extensions +import aurora_dsql_psycopg2 as dsql import os cluster_endpoint = os.environ['cluster_endpoint'] region = os.environ['AWS_REGION'] -client = boto3.client("dsql", region_name=region) - def lambda_handler(event, context): - # Generate a fresh password token for each connection, to ensure the token is not expired when the connection is established - password_token = client.generate_db_connect_admin_auth_token(cluster_endpoint, region) - - conn_params = { - "dbname": "postgres", - "user": "admin", - "host": cluster_endpoint, - "port": "5432", - "sslmode": "require", - "password": password_token + config = { + 'host': cluster_endpoint, + 'region': region, + 'user': "admin", } - # Use the more efficient connection method if it's supported. - if psycopg2.extensions.libpq_version() >= 170000: - conn_params["sslnegotiation"] = "direct" - - # Make a connection to the cluster - conn = psycopg2.connect(**conn_params) + # Make a connection to the cluster + conn = dsql.connect(**config) try: with conn.cursor() as cur: diff --git a/apigw-lambda-dsql/src/requirements.txt b/apigw-lambda-dsql/src/requirements.txt index e0b71201f2..e7dd90c449 100644 --- a/apigw-lambda-dsql/src/requirements.txt +++ b/apigw-lambda-dsql/src/requirements.txt @@ -1,3 +1,3 @@ psycopg2-binary>=2.9 botocore>=1.35.74 -boto3>=1.35.74 \ No newline at end of file +aurora-dsql-python-connector \ No newline at end of file diff --git a/apigw-lambda-dsql/template.yaml b/apigw-lambda-dsql/template.yaml index 88d2af22fd..97fab524b7 100644 --- a/apigw-lambda-dsql/template.yaml +++ b/apigw-lambda-dsql/template.yaml @@ -27,7 +27,7 @@ Resources: - Statement: - Effect: Allow Action: - - dsql:DbConnectAdmin + - dsql:DbConnectAdmin #IAM action dsql:DbConnectAdmin required to connect to the cluster. Resource: - !Sub arn:${AWS::Partition}:dsql:${AWS::Region}:${AWS::AccountId}:cluster/${DSQL} Environment: # Function environment variables @@ -42,12 +42,12 @@ Resources: Method: get DSQL: - Type: AWS::DSQL::Cluster + Type: AWS::DSQL::Cluster #Creates a single-Region DSQL cluster Properties: DeletionProtectionEnabled: false Tags: - Key: project - Value: "apigw-lambda-dsql" + Value: "apigw-lambda-dsql" Outputs: # ServerlessRestApi is an implicit API created out of Events key under Serverless::Function