

# Amazon Aurora DSQL

Amazon Aurora DSQL is a serverless, distributed relational database service optimized for transactional workloads. Aurora DSQL offers virtually unlimited scale and doesn’t require you to manage infrastructure. Aurora DSQL is PostgreSQL-compatible, so you can use familiar drivers, object-relational mappings (ORMs), frameworks, and SQL features.

To integrate Aurora DSQL, use an existing Aurora DSQL cluster in the same AWS account as your app.

## Environment variables for Aurora DSQL
<a name="integrations-dsql-env-vars"></a>

When you connect an Aurora DSQL integration, Amplify sets the following environment variables:


| Variable | Value | 
| --- | --- | 
|  `PGHOST`  | The cluster endpoint, `<cluster-id>.dsql.<region>.on.aws`  | 
|  `PGPORT`  |  `5432`  | 
|  `PGDATABASE`  |  `postgres`  | 
|  `PGUSER`  | The database user your app connects as. The default is `admin`. | 
|  `PGSSLMODE`  |  `require`  | 
|  `DSQL_REGION`  | The cluster’s AWS Region | 
|  `DSQL_ADMIN_USER`  |  `admin`  | 

There is no password variable. Aurora DSQL uses AWS Identity and Access Management (IAM) authentication: your app generates a short-lived authentication token for each connection, using the permissions that the integration grants.

## Permissions for Aurora DSQL
<a name="integrations-dsql-permissions"></a>

The integration grants your app `dsql:DbConnectAdmin` on the cluster when it connects as `admin`, or `dsql:DbConnect` when it connects as another database user.

To connect as a database user other than `admin`, map that user to your environment’s role before you deploy. Each environment has its own role, so repeat these steps for each environment that you connect the integration to.

1. Find the environment’s role ARN. It’s the `EnvironmentRoleArn` value in the output of the following command.

   ```bash
   aws amplifygen3 env get --env preview --output json
   ```

1. Connect to the cluster as `admin`. The following SQL command creates the database user if it doesn’t exist.

   ```sql
   CREATE ROLE app_user WITH LOGIN;
   ```

1. Map the database user to the environment’s role, and grant the user only the database permissions that your app needs. In the following SQL example, replace `app_user` with your database user, `arn:aws:iam::111122223333:role/my-environment-role` with the role ARN from step 1, and `orders` with your table.

   ```sql
   AWS IAM GRANT app_user TO 'arn:aws:iam::111122223333:role/my-environment-role';
   GRANT USAGE ON SCHEMA public TO app_user;
   GRANT SELECT, INSERT ON public.orders TO app_user;
   ```

For more information, see [Using database roles and IAM authentication](aurora-dsql/latest/userguide/using-database-and-iam-roles.html) in the *Amazon Aurora DSQL User Guide*.

## Create an Aurora DSQL integration
<a name="integrations-dsql-create"></a>

### Create an Aurora DSQL integration in the console
<a name="_create_an_aurora_dsql_integration_in_the_console"></a>

1. Sign in to the Amplify console at https://us-east-2.console.aws.amazon.com/amplifygen3/home.

1. For **Integrations**, choose **Add**.

1. For **Select integration**, select **Amazon Aurora DSQL**, and then choose **Add**.

1. For **Integration name**, enter a unique integration name.

1. For **Cluster ARN**, enter the ARN of your Aurora DSQL cluster.

1. (Optional) Choose **Connect to app**, select your app, and then choose the environments to connect the integration to.

1. Choose **Add**.

### Create an Aurora DSQL integration using the CLI
<a name="_create_an_aurora_dsql_integration_using_the_cli"></a>

The following command creates an Aurora DSQL integration for an existing cluster and connects it to the `preview` environment.

```bash
aws amplifygen3 integration connect dsql --name my-database --arn arn:aws:dsql:us-east-1:111122223333:cluster/abcdefghij0123456789klmnop --env preview
```

The cluster can be active, idle, or inactive. Aurora DSQL reactivates an idle or inactive cluster on the next connection.

To connect as a database user other than `admin`, add `--database-user` followed by the name of the database user. You can’t change the cluster or the database user after you create the integration.

## Use Aurora DSQL in your code
<a name="integrations-dsql-code"></a>

Use the Aurora DSQL connector for node-postgres. It generates and refreshes the authentication token for you. The connector doesn’t read `PGHOST` or `PGUSER` itself, so pass them from the environment variables, as the following TypeScript example does. The example queries the `users` table.

```bash
npm install @aws/aurora-dsql-node-postgres-connector drizzle-orm pg
```

```typescript
import { AuroraDSQLPool } from "@aws/aurora-dsql-node-postgres-connector";
import { drizzle } from "drizzle-orm/node-postgres";
import { sql } from "drizzle-orm";

const pool = new AuroraDSQLPool({
  host: process.env.PGHOST,
  user: process.env.PGUSER,
});

const db = drizzle({ client: pool });
const { rows } = await db.execute(sql`SELECT * FROM users`);
```

To run your app locally against the same cluster, download the environment’s variables with `aws amplifygen3 var download .env.local --env preview`. Your local AWS credentials must be allowed to connect to the cluster.

## Delete an Aurora DSQL integration
<a name="integrations-dsql-delete"></a>

Before you delete an integration, disconnect it from every environment, and then redeploy those environments so that they stop using it. Deleting an integration doesn’t delete the cluster, which continues to incur charges. To avoid ongoing charges, delete the cluster separately if you no longer need it.

### Delete an Aurora DSQL integration in the console
<a name="_delete_an_aurora_dsql_integration_in_the_console"></a>

1. Sign in to the Amplify console at https://us-east-2.console.aws.amazon.com/amplifygen3/home.

1. For **Integrations**, choose your Aurora DSQL integration.

1. Choose **Delete**.

1. Confirm your choice and choose **Delete**.

### Delete an Aurora DSQL integration using the CLI
<a name="_delete_an_aurora_dsql_integration_using_the_cli"></a>

The following commands disconnect the integration from the `preview` environment and then delete it.

```bash
aws amplifygen3 integration disconnect --name my-database --env preview
aws amplifygen3 integration delete --name my-database
```

The `delete` command asks for confirmation. In a non-interactive session such as CI, add `--yes`.

If you no longer need the cluster, the following command deletes it. Replace `<cluster-id>` with the ID of your cluster. If deletion protection is turned on for the cluster, turn it off first. Deleting a cluster deletes its data, and you can’t undo it.

```bash
aws dsql delete-cluster --identifier <cluster-id>
```