Services
A Service is your code on a
Server: an Effect program that Alchemy bundles with
Rolldown, copies over SSH, and runs under systemd with Restart=always.
You write no unit file, no Dockerfile and no deploy script, and several
Services can share one server. It is the same Service as
@alchemy.run/hetzner’s: moving between the two is changing the import
and the server.
- restart
- always
- restart
- always
Declare a Service
Section titled “Declare a Service”A Service is a class with its infrastructure props and its program. The server is a module-scope declaration:
import * as BetterStack from "betterstack-alchemy";
export const Box = BetterStack.Cloud.Server("Box", { serverType: "AM1", image: "ubuntu-26-04",});The Service points at it:
import * as BetterStack from "betterstack-alchemy";import * as Effect from "effect/Effect";import { HttpServerRequest } from "effect/http/HttpServerRequest";import * as HttpServerResponse from "effect/http/HttpServerResponse";import { Box } from "./server.ts";
export default class Api extends BetterStack.Cloud.Service<Api>()( "Api", { server: Box, main: import.meta.url, port: 3000 }, Effect.gen(function* () { return { fetch: Effect.gen(function* () { const request = yield* HttpServerRequest; if (new URL(request.url, "http://service").pathname === "/health") { return yield* HttpServerResponse.json({ ok: true }); } return HttpServerResponse.text("hello from Better Stack Cloud"); }), }; }),) {}server is the machine to run on; changing it replaces the Service.
main: import.meta.url makes this file the bundle’s entry. port is
written to PORT, defaults to 3000, and builds the url attribute,
http://<server ipv4>:<port>. Returning fetch boots an HTTP server on
that port. Leave it out for a background Service.
The Stack deploys it:
import * as Alchemy from "alchemy";import * as BetterStack from "betterstack-alchemy";import * as Effect from "effect/Effect";import Api from "./api.ts";import Worker from "./worker.ts";
export default Alchemy.Stack( "MyApp", { providers: BetterStack.providers(), state: Alchemy.localState() }, Effect.gen(function* () { const api = yield* Api; const worker = yield* Worker; return { url: api.url, apiUnit: api.unitName, workerUnit: worker.unitName }; }),);The resolved Service exposes url, port, unitName, serverId,
ipv4, ipv6 and the bundle’s code.hash.
What a deploy does
Section titled “What a deploy does”One SSH session as root, with the server’s Alchemy-managed deploy key:
-
Bundle
mainwith Rolldown into one ESM file,index.mjs. A generated entry wires up the Node HTTP server, logging, andConfigfrom the environment. -
Wait for SSH and for cloud-init, then install Node.js 26 from NodeSource if it is missing or older.
-
Attach and mount any bound volumes.
-
Copy the bundle, the env file and the unit into
/opt/<unitName>/, thensystemctl enable --nowand restart. The unit:[Service]Type=simpleWorkingDirectory=/opt/<unitName>EnvironmentFile=-/opt/<unitName>/envEnvironment=HOST=0.0.0.0ExecStart=/usr/bin/env node /opt/<unitName>/index.mjsRestart=alwaysRestartSec=5 -
Wait until the unit is active and, when
portis set, untilhttp://127.0.0.1:<port>/healthanswers 2xx. Otherwise the deploy fails withServiceNotHealthy, withsystemctl statusand the unit’s last 80 journal lines in its message.
On later deploys Alchemy compares the bundle’s content hash with the deployed one. Unchanged code, env and bindings are a no-op with no SSH at all; changed code or env re-copies and restarts the unit. Deleting a Service stops and disables its unit and removes its files.
Reachability
Section titled “Reachability”url is the server’s public IP and the Service’s port, with no proxy in
between. A server with a Firewall admits only
what its rules allow, so open the Service’s port, and keep tcp/22 open
to the deploying machine, which the deploy needs:
import * as BetterStack from "betterstack-alchemy";
export const Wall = BetterStack.Cloud.Firewall("Wall", { rules: [ // The deploy reaches the server over SSH. { direction: "inbound", protocol: "tcp", portFrom: 22, ipAddresses: ["0.0.0.0/0", "::/0"] }, // The Service's port. { direction: "inbound", protocol: "tcp", portFrom: 3000, ipAddresses: ["0.0.0.0/0", "::/0"] }, { direction: "outbound", protocol: "any", portFrom: 0, ipAddresses: ["0.0.0.0/0", "::/0"] }, ],});For a stable front door with health checks and TLS, put a load balancer in front and allow the port from the private network only.
Environment and Config
Section titled “Environment and Config”Pass values with env and read them through Effect’s Config:
import * as BetterStack from "betterstack-alchemy";import * as Config from "effect/Config";import * as Effect from "effect/Effect";import * as HttpServerResponse from "effect/http/HttpServerResponse";import { Box } from "./server.ts";
export default class Api extends BetterStack.Cloud.Service<Api>()( "Api", { server: Box, main: import.meta.url, port: 3000, env: { GREETING: "hello" }, }, Effect.gen(function* () { return { fetch: Effect.gen(function* () { const greeting = yield* Config.String("GREETING"); return HttpServerResponse.text(greeting); }).pipe(Effect.orDie), }; }),) {}Alchemy also sets PORT (with port), HOST, ALCHEMY_STACK_NAME,
ALCHEMY_STAGE and ALCHEMY_PHASE. Your env is merged last, so it wins.
Redacted values, like a source token or an error-tracking DSN, are
written in clear text only to the unit’s env file on the server
(/opt/<unitName>/env, mode 0600), never to the deploy’s logs or the
Service’s attributes. See Secrets &
Config for where secret values
come from.
Background services
Section titled “Background services”Leave out port and fetch, and register a long-running loop with
ServerHost.run: a worker, a queue consumer, a scheduler.
import { ServerHost } from "alchemy/Runtime/Process";import * as BetterStack from "betterstack-alchemy";import * as Effect from "effect/Effect";import * as Schedule from "effect/Schedule";import { Box } from "./server.ts";
export default class Worker extends BetterStack.Cloud.Service<Worker>()( "Worker", { server: Box, main: import.meta.url }, Effect.gen(function* () { const host = yield* ServerHost; yield* host.run( Effect.gen(function* () { yield* Effect.log("tick"); }).pipe(Effect.repeat(Schedule.spaced("1 minute")), Effect.asVoid), ); }),) {}systemd supervises the process: when the loop exits or crashes, the unit restarts after 5 seconds.
Several Services on one server
Section titled “Several Services on one server”Each Service is its own unit with its own bundle, env file and lifecycle.
Point several at the same server, as Api and Worker do above. Two
Services mounting the same volume at the same path share one attach and
one mount.
Plain handlers, without Effect
Section titled “Plain handlers, without Effect”Call Service with main pointing at a file and no implementation. The
file is bundled with a small node:http adapter and no Effect runtime:
import * as BetterStack from "betterstack-alchemy";import { Box } from "./server.ts";
export const Ping = BetterStack.Cloud.Service("Ping", { server: Box, main: new URL("./ping.ts", import.meta.url).href, port: 3001, env: { GREETING: "pong" },});
export type PingEnv = BetterStack.Cloud.InferEnv<typeof Ping>;A default export with fetch(request, env) is served on PORT, and
InferEnv types its env:
import type { PingEnv } from "./ping-service.ts";
export default { async fetch(request: Request, env: PingEnv) { if (new URL(request.url).pathname === "/health") return new Response("ok"); return new Response(env.GREETING); },};Any other module simply runs, as a plain Node program.
Your own container image
Section titled “Your own container image”Set context instead of main to run a Dockerfile build:
const worker = yield* BetterStack.Cloud.Service("Worker", { server: Box, context: "./worker", env: { QUEUE: "jobs" },});The image is built with your local Docker CLI for platform (default
linux/amd64), copied over SSH, loaded into Docker on the server
(installed if missing), and run under systemd with --network host, the
env, and any bound volumes.
A Service logs to the systemd journal. Read it over SSH, with the
Service’s unitName:
ssh root@<sshHost> journalctl -u <unitName> -fA failed deploy already prints the last 80 journal lines. To keep logs in Better Stack, send them to a Telemetry source; see Watching your services.
Where next
Section titled “Where next”- Tutorial part 2: build up a Service step by step.
- Volumes: persistent storage through
MountVolume. - Networking: firewalls and load balancers in front of a Service.
- Watching your services: logs, errors, heartbeats and uptime monitors for the Service.
Servicereference: every prop and attribute.