From 03b7bc29747b29635a1bc6435c50a31061e7417b Mon Sep 17 00:00:00 2001 From: Victor Duarte Date: Fri, 18 Oct 2024 20:18:03 +0200 Subject: [PATCH] example: Surrealdb in AWS [Fargate with EFS] (#1268) * example: Surrealdb in AWS [Fargate with EFS] * fix typo * sync --------- Co-authored-by: Frank --- examples/aws-efs-surrealdb/.gitignore | 7 +++ examples/aws-efs-surrealdb/index.ts | 32 ++++++++++ examples/aws-efs-surrealdb/package.json | 16 +++++ examples/aws-efs-surrealdb/sst-env.d.ts | 30 ++++++++++ examples/aws-efs-surrealdb/sst.config.ts | 74 ++++++++++++++++++++++++ examples/aws-efs-surrealdb/tsconfig.json | 1 + 6 files changed, 160 insertions(+) create mode 100644 examples/aws-efs-surrealdb/.gitignore create mode 100644 examples/aws-efs-surrealdb/index.ts create mode 100644 examples/aws-efs-surrealdb/package.json create mode 100644 examples/aws-efs-surrealdb/sst-env.d.ts create mode 100644 examples/aws-efs-surrealdb/sst.config.ts create mode 100644 examples/aws-efs-surrealdb/tsconfig.json diff --git a/examples/aws-efs-surrealdb/.gitignore b/examples/aws-efs-surrealdb/.gitignore new file mode 100644 index 000000000..f3c8b5fb2 --- /dev/null +++ b/examples/aws-efs-surrealdb/.gitignore @@ -0,0 +1,7 @@ + +# sst +.sst + +.envrc +shell.nix +pnpm-* \ No newline at end of file diff --git a/examples/aws-efs-surrealdb/index.ts b/examples/aws-efs-surrealdb/index.ts new file mode 100644 index 000000000..fe2079159 --- /dev/null +++ b/examples/aws-efs-surrealdb/index.ts @@ -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), + }; +}; diff --git a/examples/aws-efs-surrealdb/package.json b/examples/aws-efs-surrealdb/package.json new file mode 100644 index 000000000..4171ff61f --- /dev/null +++ b/examples/aws-efs-surrealdb/package.json @@ -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" + } +} diff --git a/examples/aws-efs-surrealdb/sst-env.d.ts b/examples/aws-efs-surrealdb/sst-env.d.ts new file mode 100644 index 000000000..fe9d6e973 --- /dev/null +++ b/examples/aws-efs-surrealdb/sst-env.d.ts @@ -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" + } + } +} diff --git a/examples/aws-efs-surrealdb/sst.config.ts b/examples/aws-efs-surrealdb/sst.config.ts new file mode 100644 index 000000000..20e8d9cb6 --- /dev/null +++ b/examples/aws-efs-surrealdb/sst.config.ts @@ -0,0 +1,74 @@ +/// +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, + }); + }, +}); diff --git a/examples/aws-efs-surrealdb/tsconfig.json b/examples/aws-efs-surrealdb/tsconfig.json new file mode 100644 index 000000000..9e26dfeeb --- /dev/null +++ b/examples/aws-efs-surrealdb/tsconfig.json @@ -0,0 +1 @@ +{} \ No newline at end of file