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.
- Apimounts /data →Data
- Workermounts /data →Data
Create a volume
Section titled “Create a volume”A volume is created attached to serverId, and the deploy waits until it
is. Give it a format so it holds a filesystem:
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.
Mount a volume in a Service
Section titled “Mount a volume in a Service”Bind the volume with MountVolume inside
a Service’s implementation and use the path as a directory:
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.
Let the server mount it
Section titled “Let the server mount it”Without a Service, autoMount: true has the server mount a formatted
volume under /mnt:
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.
Grow and move
Section titled “Grow and move”sizeGb grows in place, and serverId moves the volume to another server:
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:
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.
Snapshots
Section titled “Snapshots”A ScheduledVolumeSnapshot
snapshots a volume hourly, daily, weekly or monthly:
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.
Ownership
Section titled “Ownership”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.
Where next
Section titled “Where next”- Services: the programs that mount volumes.
- Managed databases: when you’d rather not run a database on a volume yourself.
Volumereference,MountVolumereference.- Better Stack’s Volumes docs.