Skip to content
betterstack-alchemyUnofficial

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.

BoxCloud.Server · AM1 · ubuntu-26-04
Apisystemd unit · :3000 + /health
restart
always
Workersystemd unit · no port
restart
always
Several Services share one server, each its own systemd unit that restarts when it exits. A Service with a port must answer /health before the deploy succeeds.

A Service is a class with its infrastructure props and its program. The server is a module-scope declaration:

src/server.ts
import * as BetterStack from "betterstack-alchemy";
export const Box = BetterStack.Cloud.Server("Box", {
serverType: "AM1",
image: "ubuntu-26-04",
});

The Service points at it:

src/api.ts
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:

alchemy.run.ts
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.

One SSH session as root, with the server’s Alchemy-managed deploy key:

  1. Bundle main with Rolldown into one ESM file, index.mjs. A generated entry wires up the Node HTTP server, logging, and Config from the environment.

  2. Wait for SSH and for cloud-init, then install Node.js 26 from NodeSource if it is missing or older.

  3. Attach and mount any bound volumes.

  4. Copy the bundle, the env file and the unit into /opt/<unitName>/, then systemctl enable --now and restart. The unit:

    [Service]
    Type=simple
    WorkingDirectory=/opt/<unitName>
    EnvironmentFile=-/opt/<unitName>/env
    Environment=HOST=0.0.0.0
    ExecStart=/usr/bin/env node /opt/<unitName>/index.mjs
    Restart=always
    RestartSec=5
  5. Wait until the unit is active and, when port is set, until http://127.0.0.1:<port>/health answers 2xx. Otherwise the deploy fails with ServiceNotHealthy, with systemctl status and 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.

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:

src/firewall.ts
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.

Pass values with env and read them through Effect’s Config:

src/api.ts
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.

Leave out port and fetch, and register a long-running loop with ServerHost.run: a worker, a queue consumer, a scheduler.

src/worker.ts
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.

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.

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:

src/ping-service.ts
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:

src/ping.ts
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.

Set context instead of main to run a Dockerfile build:

alchemy.run.ts
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:

Terminal window
ssh root@<sshHost> journalctl -u <unitName> -f

A 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.