AWS Aurora Postgres
In this example, we deploy a Aurora Postgres database.
const postgres = new sst.aws.Aurora("MyDatabase", { engine: "postgres", vpc,});And link it to a Lambda function.
new sst.aws.Function("MyApp", { handler: "index.handler", link: [postgres], url: true, vpc,});In the function we use the postgres package.
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.
sudo npx sst tunnel installThis 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.
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.