Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
example: Surrealdb in AWS [Fargate with EFS] (#1268)
Browse files Browse the repository at this point in the history
* example: Surrealdb in AWS [Fargate with EFS]

* fix typo

* sync

---------

Co-authored-by: Frank <frank@sst.dev>
  • Loading branch information
zvictor and fwang authored Oct 18, 2024
1 parent d2d1445 commit 03b7bc2
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/aws-efs-surrealdb/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

# sst
.sst

.envrc
shell.nix
pnpm-*
32 changes: 32 additions & 0 deletions examples/aws-efs-surrealdb/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import Surreal from "surrealdb";
import { Resource } from "sst";

export const handler = async () => {
const endpoint = `http://${Resource.MyConfig.host}:${Resource.MyConfig.port}`;

console.log(`Connecting to`, endpoint);

const db = new Surreal();
await db.connect(endpoint);

await db.use({
namespace: Resource.MyConfig.namespace,
database: Resource.MyConfig.database,
});

await db.signin({
username: Resource.MyConfig.username,
password: Resource.MyConfig.password,
});

await db.query(`INSERT INTO visits { when: time::now() }`);

const visits = await db.query(
`SELECT * FROM visits ORDER BY when DESC LIMIT 10`
);

return {
statusCode: 200,
body: JSON.stringify({ "10 Most recent visits": visits[0] }, null, 2),
};
};
16 changes: 16 additions & 0 deletions examples/aws-efs-surrealdb/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "aws-efs-surrealdb",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"sst": "latest",
"surrealdb": "^1.0.1"
}
}
30 changes: 30 additions & 0 deletions examples/aws-efs-surrealdb/sst-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* This file is auto-generated by SST. Do not edit. */
/* tslint:disable */
/* eslint-disable */
import "sst"
export {}
declare module "sst" {
export interface Resource {
"MyApp": {
"name": string
"type": "sst.aws.Function"
"url": string
}
"MyConfig": {
"database": string
"host": string
"namespace": string
"password": string
"port": number
"type": "sst.sst.Linkable"
"username": string
}
"MyService": {
"service": string
"type": "sst.aws.Service"
}
"MyVpc": {
"type": "sst.aws.Vpc"
}
}
}
74 changes: 74 additions & 0 deletions examples/aws-efs-surrealdb/sst.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/// <reference path="./.sst/platform/config.d.ts" />
export default $config({
app(input) {
return {
name: "aws-efs-surrealdb",
removal: input?.stage === "production" ? "retain" : "remove",
home: "aws",
providers: { "@pulumi/random": "4.16.7" },
};
},
async run() {
const { RandomPassword } = await import("@pulumi/random");

// SurrealDB Credentials
const PORT = 8080;
const NAMESPACE = "test";
const DATABASE = "test";
const USERNAME = "root";
const PASSWORD = new RandomPassword("Password", {
length: 32,
}).result;

// NAT Gateways are required for Lambda functions
const vpc = new sst.aws.Vpc("MyVpc", { nat: "managed" });

// Store SurrealDB data in EFS
const efs = new sst.aws.Efs("MyEfs2", { vpc });

// Run SurrealDB server in a container
const cluster = new sst.aws.Cluster("MyCluster", { vpc });
const server = cluster.addService("MyService", {
architecture: "arm64",
image: "surrealdb/surrealdb:v2.0.2",
command: [
"start",
"--bind",
$interpolate`0.0.0.0:${PORT}`,
"--log",
"info",
"--user",
USERNAME,
"--pass",
PASSWORD,
"surrealkv://data/data.skv",
"--allow-scripting",
],
volumes: [
{
efs,
path: "/data",
},
],
});

// Lambda client to connect to SurrealDB
const config = new sst.Linkable("MyConfig", {
properties: {
username: USERNAME,
password: PASSWORD,
namespace: NAMESPACE,
database: DATABASE,
port: PORT,
host: server.service,
},
});

new sst.aws.Function("MyApp", {
handler: "index.handler",
link: [config],
url: true,
vpc,
});
},
});
1 change: 1 addition & 0 deletions examples/aws-efs-surrealdb/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

0 comments on commit 03b7bc2

Please sign in to comment.