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
When you connect an Aurora DSQL integration, Amplify sets the following environment variables:
| Variable | Value |
|---|---|
|
|
The cluster endpoint, |
|
|
|
|
|
|
|
|
The database user your app connects as. The default is |
|
|
|
|
|
The cluster’s AWS Region |
|
|
|
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
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.
-
Find the environment’s role ARN. It’s the
EnvironmentRoleArnvalue in the output of the following command.aws amplifygen3 env get --env preview --output json -
Connect to the cluster as
admin. The following SQL command creates the database user if it doesn’t exist.CREATE ROLE app_user WITH LOGIN; -
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_userwith your database user,arn:aws:iam::111122223333:role/my-environment-rolewith the role ARN from step 1, andorderswith your table.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 in the Amazon Aurora DSQL User Guide.
Create an Aurora DSQL integration
Create an Aurora DSQL integration in the console
-
Sign in to the Amplify console at https://us-east-2.console.aws.amazon.com/amplifygen3/home.
-
For Integrations, choose Add.
-
For Select integration, select Amazon Aurora DSQL, and then choose Add.
-
For Integration name, enter a unique integration name.
-
For Cluster ARN, enter the ARN of your Aurora DSQL cluster.
-
(Optional) Choose Connect to app, select your app, and then choose the environments to connect the integration to.
-
Choose Add.
Create an Aurora DSQL integration using the CLI
The following command creates an Aurora DSQL integration for an existing cluster and connects it to the preview environment.
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
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.
npm install @aws/aurora-dsql-node-postgres-connector drizzle-orm pg
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
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
-
Sign in to the Amplify console at https://us-east-2.console.aws.amazon.com/amplifygen3/home.
-
For Integrations, choose your Aurora DSQL integration.
-
Choose Delete.
-
Confirm your choice and choose Delete.
Delete an Aurora DSQL integration using the CLI
The following commands disconnect the integration from the preview environment and then delete it.
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.
aws dsql delete-cluster --identifier <cluster-id>