Skip to content
GitHub Discord

AWS EFS

Mount an EFS file system to a function and a container.

This allows both your function and the container to access the same file system. Here they both update a counter that’s stored in the file system.

common.mjs
await writeFile("/mnt/efs/counter", newValue.toString());

The file system is mounted to /mnt/efs in both the function and the container.

sst.config.ts
// 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 });
new sst.aws.Service("MyService", {
cluster,
loadBalancer: {
ports: [{ listen: "80/http" }],
},
volumes: [
{
efs,
path: "/mnt/efs",
},
],
});
lambda.ts
import { increment } from "./common.mjs";
export const handler = async () => {
const counter = await increment();
console.log("COUNTER", counter);
return {
statusCode: 200,
body: JSON.stringify({
counter,
}),
};
};

View the full example.