Skip to content
betterstack-alchemyUnofficial

Volumes

A Volume is an NVMe block volume attached to one server at a time, from 1 to 3072 GiB, billed per allocated GiB whether it is attached or not. It outlives the server, so data on it survives a server being replaced, and it can move to another server in its zone.

BoxCloud.Server
ApiService
WorkerService
Datavolume · 10 GiB · ext4
  • Apimounts /data →Data
  • Workermounts /data →Data
A volume is attached to one server. Two Services on that server mounting it at the same path share one attach and one mount.

A volume is created attached to serverId, and the deploy waits until it is. Give it a format so it holds a filesystem:

src/volume.ts
export const Data = BetterStack.Cloud.Volume(
"Data",
Effect.gen(function* () {
const server = yield* Box;
return { serverId: server.id, sizeGb: 10, format: "ext4" as const };
}),
);

format (ext4 or xfs), autoMount, volumeType and the zone are fixed: changing one replaces the volume, and its data with it. name and metadata change in place.

Bind the volume with MountVolume inside a Service’s implementation and use the path as a directory:

src/api.ts
import * as BetterStack from "betterstack-alchemy";
import * as Effect from "effect/Effect";
import * as FileSystem from "effect/FileSystem";
import * as HttpServerResponse from "effect/http/HttpServerResponse";
import { Box } from "./server.ts";
import { Data } from "./volume.ts";
export default class Api extends BetterStack.Cloud.Service<Api>()(
"Api",
{ server: Box, main: import.meta.url, port: 3000 },
Effect.gen(function* () {
const mount = yield* BetterStack.Cloud.MountVolume(Data, { path: "/data" });
return {
fetch: Effect.gen(function* () {
const fs = yield* FileSystem.FileSystem;
const text = yield* fs.readFileString(`${mount.path}/hello.txt`);
return HttpServerResponse.text(text);
}).pipe(Effect.orDie),
};
}).pipe(Effect.provide(BetterStack.Cloud.MountVolumeLive)),
) {}

On deploy the Service attaches the volume to its server, then over SSH creates the directory, mounts the device and adds a nofail line to /etc/fstab, so the mount survives reboots. The unit gets RequiresMountsFor=<path>. When no Service on the server binds the path any more, it is unmounted and its /etc/fstab line removed; the volume stays attached.

An unformatted volume fails the deploy with MountVolumeError. Give the volume the Service’s server as serverId (as above), so the two don’t fight over the attachment.

Without a Service, autoMount: true has the server mount a formatted volume under /mnt:

alchemy.run.ts
const volume = yield* BetterStack.Cloud.Volume("data", {
serverId: server.id,
sizeGb: 10,
format: "ext4",
autoMount: true,
});

mountPath is where it went, and devicePath is the stable device to use in /etc/fstab.

sizeGb grows in place, and serverId moves the volume to another server:

alchemy.run.ts
yield* BetterStack.Cloud.Volume("data", {
serverId: other.id,
sizeGb: 20,
format: "ext4",
autoMount: true,
});

A volume never shrinks: a smaller sizeGb fails with VolumeShrinkRefused before anything changes. serverId: null detaches it:

alchemy.run.ts
yield* BetterStack.Cloud.Volume("data", {
serverId: null,
sizeGb: 20,
format: "ext4",
autoMount: true,
});

Deleting a volume detaches it first and waits until it is gone.

A ScheduledVolumeSnapshot snapshots a volume hourly, daily, weekly or monthly:

src/volume.ts
export const WeeklySnapshots = BetterStack.Cloud.ScheduledVolumeSnapshot(
"Weekly",
Effect.gen(function* () {
const volume = yield* Data;
return { volumeId: volume.id, frequency: "weekly" as const };
}),
);

Volume snapshots are stored off the volume’s machine, billed by the volume’s allocated size, and stay after the schedule is deleted. See Better Stack’s volume snapshots docs.

For one snapshot taken when it is created, such as before a risky change, declare a VolumeSnapshot with the volume’s volumeId; it is deleted with the stack. Volume snapshots are tested against the offline mock only.

Every volume this stack creates carries an alchemy entry in its metadata, which takes one of its 10 metadata slots, so a deploy that lost track of one finds it again and alchemy nuke lists only Alchemy-created volumes. See the Volume reference.