forked from aws-samples/aurora-dsql-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_with_connection_pool.py
More file actions
64 lines (48 loc) · 1.73 KB
/
example_with_connection_pool.py
File metadata and controls
64 lines (48 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: MIT-0
"""
import asyncio
import os
import aurora_dsql_asyncpg as dsql
async def connect_with_pool(cluster_user, cluster_endpoint, region):
ssl_cert_path = "./root.pem"
if not os.path.isfile(ssl_cert_path):
raise FileNotFoundError(f"SSL certificate file not found: {ssl_cert_path}")
pool_params = {
"database": "postgres",
"user": cluster_user,
"host": cluster_endpoint,
"port": 5432,
"region": region,
"ssl": "verify-full",
"sslrootcert": ssl_cert_path,
"min_size": 2,
"max_size": 5,
}
pool = await dsql.create_pool(**pool_params)
try:
async with pool.acquire() as conn:
result = await conn.fetchval("SELECT 1")
assert result == 1
finally:
await pool.close()
async def main():
try:
cluster_user = os.environ.get("CLUSTER_USER", None)
assert cluster_user is not None, "CLUSTER_USER environment variable is not set"
cluster_endpoint = os.environ.get("CLUSTER_ENDPOINT", None)
assert (
cluster_endpoint is not None
), "CLUSTER_ENDPOINT environment variable is not set"
region = os.environ.get("REGION", None)
assert region is not None, "REGION environment variable is not set"
ssl_cert_path = "./root.pem"
if not os.path.isfile(ssl_cert_path):
raise FileNotFoundError(f"SSL certificate file not found: {ssl_cert_path}")
await connect_with_pool(cluster_user, cluster_endpoint, region)
finally:
pass
print("Pool exercised successfully")
if __name__ == "__main__":
asyncio.run(main())