This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 237
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
example: Surrealdb in AWS [Fargate with EFS] (#1268)
* example: Surrealdb in AWS [Fargate with EFS] * fix typo * sync --------- Co-authored-by: Frank <frank@sst.dev>
- Loading branch information
Showing
6 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
# sst | ||
.sst | ||
|
||
.envrc | ||
shell.nix | ||
pnpm-* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}); | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |