Skip to content
betterstack-alchemyUnofficial

Part 2: Deploy a Service

In Part 1 you deployed a server. Now you put code on it: a Cloud.Service, an Effect program that Alchemy bundles, copies over SSH and runs as a systemd unit. You write no unit file, no Dockerfile and no deploy script.

To let other files use the server, declare it at module scope. Create src/server.ts and move the key, the firewall and the server into it:

src/server.ts
import * as BetterStack from "betterstack-alchemy";
import * as Effect from "effect/Effect";
export const Laptop = BetterStack.Cloud.SshKey("Laptop", {
publicKey:
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a you@laptop",
});
export const Wall = BetterStack.Cloud.Firewall("Wall", {
rules: [
{ direction: "inbound", protocol: "tcp", portFrom: 22, ipAddresses: ["0.0.0.0/0", "::/0"] },
{ direction: "outbound", protocol: "any", portFrom: 0, ipAddresses: ["0.0.0.0/0", "::/0"] },
],
});
export const Box = BetterStack.Cloud.Server(
"Box",
Effect.gen(function* () {
const key = yield* Laptop;
const wall = yield* Wall;
return {
serverType: "AM1",
image: "ubuntu-26-04",
sshKeys: [key.slug],
firewallIds: [wall.id],
};
}),
);

A module-scope declaration is an Effect you can import and yield* anywhere. The server’s props are an Effect too, so they can read the key’s slug and the firewall’s id. The ids Laptop, Wall and Box are unchanged, so Alchemy sees the same resources you deployed in Part 1: moving a declaration to another file recreates nothing.

Import the server and yield it from the Stack:

alchemy.run.ts
import * as Alchemy from "alchemy";
import * as BetterStack from "betterstack-alchemy";
import * as Effect from "effect/Effect";
import { Box } from "./src/server.ts";
export default Alchemy.Stack(
"MyApp",
{ providers: BetterStack.providers(), state: Alchemy.localState() },
Effect.gen(function* () {
const server = yield* Box;
const key = yield* BetterStack.Cloud.SshKey("Laptop", {
publicKey:
"ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEF0uIiLF54lpavoiG7O95cChLJ80vbi0Nh08Kf3+G/a you@laptop",
});
const wall = yield* BetterStack.Cloud.Firewall("Wall", {
rules: [
{ direction: "inbound", protocol: "tcp", portFrom: 22, ipAddresses: ["0.0.0.0/0", "::/0"] },
{ direction: "outbound", protocol: "any", portFrom: 0, ipAddresses: ["0.0.0.0/0", "::/0"] },
],
});
const server = yield* BetterStack.Cloud.Server("Box", {
serverType: "AM1",
image: "ubuntu-26-04",
sshKeys: [key.slug],
firewallIds: [wall.id],
});
return {
ipv4: server.ipv4,
ipv6: server.ipv6,

The key and the firewall are still part of the Stack: yielding Box resolves them first.

A Service is a class with two halves: the infrastructure it needs and the program it runs. Create src/api.ts with the smallest one:

src/api.ts
import * as BetterStack from "betterstack-alchemy";
import * as Effect from "effect/Effect";
import { Box } from "./server.ts";
export default class Api extends BetterStack.Cloud.Service<Api>()(
"Api",
{ server: Box, main: import.meta.url },
Effect.gen(function* () {
return {};
}),
) {}

server: Box is the machine it runs on, and changing it later moves the Service to the new server. main: import.meta.url makes this file the bundle’s entry: on deploy, Alchemy bundles it with Rolldown and ships it to the server.

Return a fetch handler from the program:

src/api.ts
import * as BetterStack from "betterstack-alchemy";
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 },
Effect.gen(function* () {
return {
fetch: Effect.succeed(HttpServerResponse.text("Hello from Better Stack Cloud!")),
};
return {};
}),
) {}

Returning fetch boots an HTTP server. The outer Effect.gen runs at deploy and when the process starts; fetch runs for each request. HttpServerResponse is Effect’s HTTP module, the same one Alchemy’s other runtimes use.

Give the Service a port:

src/api.ts
export default class Api extends BetterStack.Cloud.Service<Api>()(
"Api",
{ server: Box, main: import.meta.url, port: 3000 },
{ server: Box, main: import.meta.url },
Effect.gen(function* () {
return {
fetch: Effect.succeed(HttpServerResponse.text("Hello from Better Stack Cloud!")),

The port is written to the PORT environment variable and builds the Service’s url attribute, http://<server ipv4>:3000.

The firewall from Part 1 only lets SSH in. Add a rule for port 3000:

src/server.ts
export const Wall = BetterStack.Cloud.Firewall("Wall", {
rules: [
{ direction: "inbound", protocol: "tcp", portFrom: 22, ipAddresses: ["0.0.0.0/0", "::/0"] },
{ 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"] },
],
});

The rule reaches every server using the firewall within seconds of the deploy.

A Service with a port must answer /health. The deploy polls http://127.0.0.1:3000/health on the server and fails, printing the unit’s last journal lines, if it never answers 2xx. Add the route:

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";
// …
{ server: Box, main: import.meta.url, port: 3000 },
Effect.gen(function* () {
return {
fetch: Effect.gen(function* () {
const request = yield* HttpServerRequest;
const url = new URL(request.url, "http://service");
if (url.pathname === "/health") {
return yield* HttpServerResponse.json({ ok: true });
}
return HttpServerResponse.text("Hello from Better Stack Cloud!");
}),
fetch: Effect.succeed(HttpServerResponse.text("Hello from Better Stack Cloud!")),
};
}),
) {}

Yield the Service in the Stack and return its URL:

alchemy.run.ts
import * as Alchemy from "alchemy";
import * as BetterStack from "betterstack-alchemy";
import * as Effect from "effect/Effect";
import Api from "./src/api.ts";
import { Box } from "./src/server.ts";
export default Alchemy.Stack(
// …
{ providers: BetterStack.providers(), state: Alchemy.localState() },
Effect.gen(function* () {
const server = yield* Box;
const api = yield* Api;
return {
ipv4: server.ipv4,
url: api.url,
ipv6: server.ipv6,
};
}),
);

The Api class is a typed handle. Yielding it registers the Service and returns its attributes: url, port, unitName and the bundle’s hash.

Terminal window
bun alchemy deploy

The plan updates the firewall and creates Api. Over one SSH session with the server’s deploy key, Alchemy waits for the server’s first-boot setup (which installs Node.js), copies the bundle to /opt/<unitName>/ with an env file, installs a systemd unit with Restart=always, starts it, and waits for /health. The deploy prints url.

Terminal window
curl http://<ipv4>:3000
# → Hello from Better Stack Cloud!
curl http://<ipv4>:3000/health
# → {"ok":true}

Edit the greeting in src/api.ts and run alchemy deploy again. Alchemy compares the bundle’s hash with the deployed one: unchanged code is a no-op with no SSH at all, and changed code is copied up and the unit restarted, in a few seconds. The unit’s logs are in the systemd journal:

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

You now have:

  • An HTTP Service running on your server as a systemd unit that restarts when it exits
  • A public URL, checked healthy before the deploy finished
  • A firewall that admits SSH and port 3000 only
  • An update loop that only touches the server when the code changes

Next, Part 3 attaches a volume and keeps files across deploys.