Skip to content

Commit 29fb073

Browse files
authored
Update go-pgx to add connection pool with token refresh. (#145)
* Initial start to integrate sdk * Updated example to add pgxpool and token refresh logic and tests. * Update to add verbose output to test in github actions * Clean up code remove unnecessary comments. * Update crypto version to latest. * Update example and README.md
1 parent f0fd702 commit 29fb073

6 files changed

Lines changed: 1238 additions & 96 deletions

File tree

.github/workflows/go-pgx-integ-tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,4 @@ jobs:
5252
REGION: ${{ secrets.GO_PGX_CLUSTER_REGION }}
5353
run: |
5454
go env -w GOPROXY=direct
55-
go test
55+
go test -v

go/pgx/README.md

Lines changed: 77 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,83 @@
1-
# Aurora DSQL pgx code examples
1+
# PGX with Aurora DSQL
22

33
## Overview
44

5-
The code examples in this topic show you how to use DSQL with Go pgx.
5+
This code example demonstrates how to use the `pgx` driver with Amazon Aurora DSQL. The example shows you how to connect to an Aurora DSQL cluster and perform database operations using IAM authentication.
6+
7+
Aurora DSQL is a distributed SQL database service that provides high availability and scalability for your PostgreSQL-compatible applications. `pgx` is a pure Go driver and toolkit for PostgreSQL that offers robust features including automatic connection pool management and authentication token refresh.
8+
9+
10+
## About the code example
11+
12+
The example demonstrates a flexible connection approach using IAM authentication:
13+
14+
- Implements automatic token refresh mechanism to maintain continuous database connectivity
15+
- Handles secure IAM-based authentication token generation
16+
- Provides connection pooling and management
17+
- Demonstrates best practices for Aurora DSQL connectivity in Go applications
18+
19+
## ⚠️ Important
20+
21+
- Running this code might result in charges to your AWS account.
22+
- We recommend that you grant your code least privilege. At most, grant only the
23+
minimum permissions required to perform the task. For more information, see
24+
Grant least privilege in the AWS IAM User Guide.
25+
- This code is not tested in every AWS Region. For more information, see
26+
AWS Regional Services.
27+
28+
## Configuration
29+
30+
The following environment variables can be used to configure the connection parameter in this example:
31+
32+
- `CLUSTER_ENDPOINT`: Your Aurora cluster endpoint (required)
33+
- `REGION`: AWS region where your cluster is located (required)
34+
- `CLUSTER_USER`: Database user (defaults to "admin")
35+
- `DB_HOST`: Database host (defaults to CLUSTER_ENDPOINT)
36+
- `DB_PORT`: Database port (defaults to 5432)
37+
- `DB_USER`: Database user (defaults to CLUSTER_USER)
38+
- `DB_NAME`: Database name (defaults to "postgres")
39+
- `DB_USE_IAM`: Whether to use IAM authentication (defaults to false)
40+
- `DB_REFRESH_TOKEN`: Whether to enable token refresh (defaults to true)
41+
- `TOKEN_REFRESH_INTERVAL`: Token refresh interval in seconds (defaults to 900 seconds / 15 minutes)
642

743
## Run the examples
844

945
### Prerequisites
1046

11-
* Go version >= 1.21
12-
* AWS credentials file is configured
13-
47+
- You must have an AWS account, and have your default credentials and AWS Region
48+
configured as described in the
49+
[Globally configuring AWS SDKs and tools](https://docs.aws.amazon.com/credref/latest/refdocs/creds-config-files.html)
50+
guide.
51+
-You must have an Aurora DSQL cluster. For information about creating an Aurora DSQL cluster, see the
52+
[Getting started with Aurora DSQL](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/getting-started.html)
53+
guide.
54+
- If connecting as a non-admin user, ensure the user is linked to an IAM role and is granted access to the `myschema`
55+
schema. See the
56+
[Using database roles with IAM roles](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/using-database-and-iam-roles.html)
57+
guide.
58+
- Go version >= 1.21
1459

1560
### Setup test running environment
1661

1762
Ensure you are authenticated with AWS credentials. No other setup is needed besides having Go installed.
1863

64+
### Environment Variables
65+
Set the following required environment variables:
66+
67+
```shell
68+
# Your cluster endpoint (e.g., "cluster-name.cluster-xxx.region.rds.amazonaws.com")
69+
export CLUSTER_ENDPOINT="<your cluster endpoint>"
70+
71+
# Your AWS region (e.g., "us-east-1")
72+
export REGION="<your cluster region>"
73+
```
74+
1975
### Run the example tests
2076

2177
In a terminal run the following commands:
2278

2379
```sh
2480
# Use the account credentials dedicated for golang
25-
export CLUSTER_ENDPOINT="<your cluster endpoint>"
26-
export REGION="<your cluster region>"
2781
go env -w GOPROXY=direct
2882
go test
2983

@@ -32,6 +86,22 @@ go build
3286
./example
3387
```
3488

89+
## Token Refresh
90+
91+
Token Refresh
92+
The implementation includes an automatic token refresh mechanism that:
93+
94+
1. Creates a new token before the current one expires (default: every 15 minutes)
95+
96+
1. Ensures continuous database connectivity
97+
98+
1. Handles token generation and rotation securelye fresh connections with valid authentication tokens.
99+
100+
## Additional resources
101+
102+
- [Amazon Aurora DSQL Documentation](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/what-is-aurora-dsql.html)
103+
- [AWS SDK for Go Documentation](https://docs.aws.amazon.com/sdk-for-go/)
104+
35105
---
36106

37107
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

0 commit comments

Comments
 (0)