Skip to content
25K
Console

AWS Aurora Postgres

In this example, we deploy a Aurora Postgres database.

sst.config.ts
const postgres = new sst.aws.Aurora("MyDatabase", {
engine: "postgres",
vpc,
});

And link it to a Lambda function.

sst.config.ts
new sst.aws.Function("MyApp", {
handler: "index.handler",
link: [postgres],
url: true,
vpc,
});

In the function we use the postgres package.

index.ts
import postgres from "postgres";
import { Resource } from "sst";
const sql = postgres({
username: Resource.MyDatabase.username,
password: Resource.MyDatabase.password,
database: Resource.MyDatabase.database,
host: Resource.MyDatabase.host,
port: Resource.MyDatabase.port,
});

We also enable the bastion option for the VPC. This allows us to connect to the database from our local machine with the sst tunnel CLI.

Terminal window
sudo npx sst tunnel install

This needs sudo to create a network interface on your machine. You’ll only need to do this once on your machine.

Now you can run npx sst dev and you can connect to the database from your local machine.

sst.config.ts
const vpc = new sst.aws.Vpc("MyVpc", {
nat: "ec2",
bastion: true,
});
const postgres = new sst.aws.Aurora("MyDatabase", {
engine: "postgres",
vpc,
});
new sst.aws.Function("MyApp", {
handler: "index.handler",
link: [postgres],
url: true,
vpc,
});

View the full example.