Skip to content
25K
Console

AWS Aurora MySQL

In this example, we deploy a Aurora MySQL database.

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

And link it to a Lambda function.

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

Now in the function we can access the database.

index.ts
const connection = await mysql.createConnection({
database: Resource.MyDatabase.database,
host: Resource.MyDatabase.host,
port: Resource.MyDatabase.port,
user: Resource.MyDatabase.username,
password: Resource.MyDatabase.password,
});

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 mysql = new sst.aws.Aurora("MyDatabase", {
engine: "mysql",
vpc,
});
new sst.aws.Function("MyApp", {
handler: "index.handler",
link: [mysql],
url: true,
vpc,
});

View the full example.