Skip to content
GitHub Discord

AWS Aurora DSQL with Drizzle

In this example, we use Drizzle ORM with an Aurora DSQL cluster.

sst.config.ts
const cluster = new sst.aws.Dsql("MyCluster");

And link it to a Lambda function.

sst.config.ts
new sst.aws.Function("MyApi", {
handler: "src/api.handler",
link: [cluster],
url: true,
});

Push the Drizzle schema to the database.

Terminal window
sst shell -- bun run push.ts

Now in the function we can connect to the cluster using Drizzle with the DSQL connector. Learn more about DSQL Node.js connectors.

src/drizzle.ts
import { drizzle } from "drizzle-orm/node-postgres";
import { AuroraDSQLPool } from "@aws/aurora-dsql-node-postgres-connector";
import { Resource } from "sst";
const pool = new AuroraDSQLPool({
host: Resource.MyCluster.endpoint,
user: "admin",
});
export const db = drizzle(pool, { schema });
sst.config.ts
const cluster = new sst.aws.Dsql("MyCluster");
new sst.aws.Function("MyApi", {
handler: "src/api.handler",
link: [cluster],
url: true,
});
src/api.ts
import { db } from "./drizzle";
import { todo } from "./todo.sql";
import { APIGatewayProxyEventV2 } from "aws-lambda";
export const handler = async (evt: APIGatewayProxyEventV2) => {
if (evt.requestContext.http.method === "GET") {
const result = await db.select().from(todo).execute();
return {
statusCode: 200,
body: JSON.stringify(result, null, 2),
};
}
if (evt.requestContext.http.method === "POST") {
const result = await db
.insert(todo)
.values({ title: "Todo", description: crypto.randomUUID() })
.returning()
.execute();
return {
statusCode: 200,
body: JSON.stringify(result),
};
}
};

View the full example.