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 235
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
630 additions
and
8 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,3 @@ | ||
|
||
# sst | ||
.sst |
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,10 @@ | ||
FROM node:18-bullseye-slim | ||
WORKDIR /app/ | ||
|
||
COPY package.json /app | ||
RUN npm install | ||
|
||
COPY service.mjs /app | ||
COPY common.mjs /app | ||
|
||
ENTRYPOINT ["node", "service.mjs"] |
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,20 @@ | ||
import { readFile, writeFile } from "fs/promises"; | ||
|
||
export async function increment() { | ||
// read the counter file from EFS | ||
let oldValue = 0; | ||
try { | ||
oldValue = parseInt(await readFile("/mnt/efs/counter", "utf8")); | ||
oldValue = isNaN(oldValue) ? 0 : oldValue; | ||
} catch (e) { | ||
// file doesn't exist | ||
} | ||
|
||
// increment the counter | ||
const newValue = oldValue + 1; | ||
|
||
// write the counter file to EFS | ||
await writeFile("/mnt/efs/counter", newValue.toString()); | ||
|
||
return newValue; | ||
} |
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,12 @@ | ||
import { increment } from "./common.mjs"; | ||
|
||
export const handler = async () => { | ||
const counter = await increment(); | ||
console.log("COUNTER", counter); | ||
return { | ||
statusCode: 200, | ||
body: JSON.stringify({ | ||
counter, | ||
}), | ||
}; | ||
}; |
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,12 @@ | ||
{ | ||
"name": "aws-efs", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"express": "^4.19.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,18 @@ | ||
import express from "express"; | ||
import { increment } from "./common.mjs"; | ||
|
||
const PORT = 80; | ||
|
||
const app = express(); | ||
|
||
app.get("/", async (req, res) => { | ||
res.send( | ||
JSON.stringify({ | ||
counter: await increment(), | ||
}) | ||
); | ||
}); | ||
|
||
app.listen(PORT, () => { | ||
console.log(`Server is running on http://localhost:${PORT}`); | ||
}); |
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,22 @@ | ||
/* This file is auto-generated by SST. Do not edit. */ | ||
/* tslint:disable */ | ||
/* eslint-disable */ | ||
import "sst" | ||
export {} | ||
declare module "sst" { | ||
export interface Resource { | ||
"MyFunction": { | ||
"name": string | ||
"type": "sst.aws.Function" | ||
"url": string | ||
} | ||
"MyService": { | ||
"service": string | ||
"type": "sst.aws.Service" | ||
"url": string | ||
} | ||
"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,43 @@ | ||
/// <reference path="./.sst/platform/config.d.ts" /> | ||
|
||
export default $config({ | ||
app(input) { | ||
return { | ||
name: "aws-efs", | ||
removal: input?.stage === "production" ? "retain" : "remove", | ||
home: "aws", | ||
}; | ||
}, | ||
async run() { | ||
// NAT Gateways are required for Lambda functions | ||
const vpc = new sst.aws.Vpc("MyVpc", { nat: "managed" }); | ||
|
||
// Create an EFS file system to store a counter | ||
const efs = new sst.aws.Efs("MyEfs", { vpc }); | ||
|
||
// Create a Lambda function that increments the counter | ||
new sst.aws.Function("MyFunction", { | ||
handler: "lambda.handler", | ||
url: true, | ||
vpc, | ||
volume: { | ||
efs, | ||
path: "/mnt/efs", | ||
}, | ||
}); | ||
|
||
// Create a service that increments the same counter | ||
const cluster = new sst.aws.Cluster("MyCluster", { vpc }); | ||
cluster.addService("MyService", { | ||
public: { | ||
ports: [{ listen: "80/http" }], | ||
}, | ||
volumes: [ | ||
{ | ||
efs, | ||
path: "/mnt/efs", | ||
}, | ||
], | ||
}); | ||
}, | ||
}); |
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,27 @@ | ||
import { stat, readFile, writeFile } from "fs/promises"; | ||
|
||
export const handler = async () => { | ||
const dir = await stat("/mnt/efs"); | ||
console.log("DIR STAT", dir); | ||
if (!dir.isDirectory()) { | ||
return { | ||
statusCode: 500, | ||
body: "/mnt/efs not mounted", | ||
}; | ||
} | ||
|
||
let value; | ||
try { | ||
const content = await readFile("/mnt/efs/counter", "utf8"); | ||
value = parseInt(content) + 1; | ||
} catch (e) { | ||
console.log("READ ERROR", e); | ||
value = 1; | ||
} | ||
await writeFile("/mnt/efs/counter", value.toString()); | ||
|
||
return { | ||
statusCode: 200, | ||
body: value.toString(), | ||
}; | ||
}; |
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
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
Oops, something went wrong.