@aws/aurora-dsql-postgresjs-connector
Version:
An AWS Aurora DSQL connector with IAM authentication for Postgres.js
159 lines (111 loc) • 5.87 kB
Markdown
The Aurora DSQL Connector for Postgres.js is a Node.js connector built on [Postgres.js](https://github.com/porsager/postgres)
that integrates IAM Authentication for connecting JavaScript applications to Amazon Aurora DSQL clusters.
The Aurora DSQL Connector for Postgres.js is designed as an authentication plugin that extends the functionality of the
Postgres.js client to enable applications to authenticate with Amazon Aurora DSQL using IAM credentials. The connector
does not connect directly to the database, but provides seamless IAM authentication on top of the underlying Postgres.js driver.
Amazon Aurora DSQL is a distributed SQL database service that provides high availability and scalability for
PostgreSQL-compatible applications. Aurora DSQL requires IAM-based authentication with time-limited tokens that
existing Node.js drivers do not natively support.
The idea behind the Aurora DSQL Connector for Postgres.js is to add an authentication layer on top of the Postgres.js
client that handles IAM token generation, allowing users to connect to Aurora DSQL without changing their existing Postgres.js workflows.
The Aurora DSQL Connector for Postgres.js works with most versions of Postgres.js. Users provide their own version by installing
Postgres.js directly.
- **Automatic IAM Authentication** - Handles DSQL token generation and refresh
- **Built on Postgres.js** - Leverages the fast PostgreSQL client for Node.js
- **Region Auto-Discovery** - Extracts AWS region from DSQL cluster hostname
- **Full TypeScript Support** - Provides full type safety
- **Custom Credentials** - Support for custom AWS credential providers
- Node.js 20+
- AWS credentials configured (via AWS CLI, environment variables, or IAM roles)
- Access to an Aurora DSQL cluster
### Installation
```bash
npm install @aws/aurora-dsql-postgresjs-connector
# Postgres.js is a peer-dependency, so users must install it themselves
npm install postgres
```
### Basic Usage
```typescript
import { auroraDSQLPostgres } from '@aws/aurora-dsql-postgresjs-connector';
const sql = auroraDSQLPostgres({
host: 'your-cluster.dsql.us-east-1.on.aws',
username: 'admin',
});
// Execute queries
const users = await sql`SELECT * FROM users WHERE age > ${25}`;
console.log(users);
// Clean up
await sql.end();
```
```typescript
const sql = auroraDSQLPostgres({
host: 'your-cluster-id',
region: 'us-east-1',
username: 'admin',
});
```
```typescript
const sql = AuroraDSQLPostgres(
'postgres://admin@your-cluster.dsql.us-east-1.on.aws'
);
const result = await sql`SELECT current_timestamp`;
```
```typescript
import { fromNodeProviderChain } from '@aws-sdk/credential-providers';
const sql = AuroraDSQLPostgres({
host: 'your-cluster.dsql.us-east-1.on.aws',
database: 'postgres',
username: 'admin',
customCredentialsProvider: fromNodeProviderChain(), // Optionally provide custom credentials provider
tokenDurationSecs: 3600, // Token expiration (seconds)
// Standard Postgres.js options
max: 20, // Connection pool size
ssl: { rejectUnauthorized: false } // SSL configuration
});
```
| Option | Type | Required | Description |
|-----------------------------|----------------------------------|----------|----------------------------------------------------------|
| `host` | `string` | Yes | DSQL cluster hostname or cluster ID |
| `database` | `string?` | No | Database name |
| `username` | `string?` | No | Database username (uses admin if not provided) |
| `region` | `string?` | No | AWS region (auto-detected from hostname if not provided) |
| `customCredentialsProvider` | `AwsCredentialIdentityProvider?` | No | Custom AWS credentials provider |
| `tokenDurationSecs` | `number?` | No | Token expiration time in seconds |
All standard [Postgres.js options](https://github.com/porsager/postgres?tab=readme-ov-file#connection-details) are also supported.
## Authentication
The connector automatically handles DSQL authentication by generating tokens using the DSQL client token generator. If the
AWS region is not provided, it will be automatically parsed from the hostname provided.
For more information on authentication in Aurora DSQL, see the [user guide](https://docs.aws.amazon.com/aurora-dsql/latest/userguide/authentication-authorization.html).
### Admin vs Regular Users
- Users named `"admin"` automatically use admin authentication tokens
- All other users use regular authentication tokens
- Tokens are generated dynamically for each connection
## Sample usage
An JavaScript example using the Aurora DSQL Connector for Postgres.js is available [here](example).
## Development
```bash
# Install dependencies
npm install
# Build the project
npm run build
# Set a cluster for use in integration tests
export CLUSTER_ENDPOINT=your-cluster.dsql.us-east-1.on.aws
npm run test
npm run test:unit
npm run test:integration
```
This software is released under the Apache 2.0 license.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0