Skip to content
betterstack-alchemyUnofficial

Networks, firewalls & load balancers

Around its servers, Better Stack Cloud offers what they need to talk to each other and to the internet: private networks that connect servers on internal addresses, firewalls that decide which traffic reaches them, reserved IPs that stay the same when the server behind them changes, and load balancers that spread traffic over servers and terminate TLS with certificates. Reserved and server IPv4 addresses bill (IPv6 is free) per Better Stack’s Cloud pricing, and load balancers bill.

us-west-2aavailability zone
edgeload balancer · LB1
443
https → 3000
privatenetwork · 10.0.0.0/24
webserver · 10.0.0.5
firewall
web-fw
  • edgeroutes →web
The load balancer and the server share a private network, so the load balancer reaches the server on its private address. The server's firewall filters what reaches its public addresses.

A Network is an IP range. Servers attached to it get private addresses from it and reach each other without the public internet:

alchemy.run.ts
const network = yield* BetterStack.Cloud.Network("private", {
ipPrefix: "10.0.0.0",
ipPrefixLength: 24,
});
const server = yield* BetterStack.Cloud.Server("web", {
serverType: "AM1",
image: "ubuntu-26-04",
networkIds: [network.id],
});

Only name changes in place; the range and the zone replace the network. Attaching a server is its networkIds prop; see Servers.

ServerPrivateIp pins a server’s address in a network it is attached to. Leave ipAddress out to get the next free one:

alchemy.run.ts
yield* BetterStack.Cloud.ServerPrivateIp("web-ip", {
serverId: server.id,
networkId: network.id,
ipAddress: "10.0.0.5",
});

There is no update: changing anything replaces the address. It goes away with its server.

A Firewall is a named set of rules you attach to servers. Rules only allow traffic; whatever no rule matches is dropped, in both directions. Firewalls are stateful, so replies to an allowed connection pass.

alchemy.run.ts
const firewall = yield* BetterStack.Cloud.Firewall("web-fw", {
rules: [
{ direction: "inbound", protocol: "tcp", portFrom: 22, ipAddresses: ["0.0.0.0/0", "::/0"] },
{
direction: "inbound",
protocol: "tcp",
portFrom: 443,
ipAddresses: ["0.0.0.0/0", "::/0"],
},
{ direction: "outbound", protocol: "any", portFrom: 0, ipAddresses: ["0.0.0.0/0", "::/0"] },
],
serverIds: [server.id],
});

A firewall created through the API starts with one rule that allows all outbound traffic and no inbound rule. Add at least an SSH rule before attaching it to a server you need to reach: Services deploy over SSH. A server has at most one firewall.

The inline rules list is the whole set: a deploy that changes the firewall’s props makes the remote rules match it, and alchemy drift finds rules changed outside the stack (see How deploys behave). To add rules one by one instead, use FirewallRule:

alchemy.run.ts
const app = yield* BetterStack.Cloud.Firewall("app-fw", { name: "app" });
yield* BetterStack.Cloud.FirewallRule("app-ports", {
firewallId: app.id,
direction: "inbound",
protocol: "tcp",
portFrom: 8000,
portTo: 8099,
ipAddresses: ["10.0.0.0/8"],
});

Better Stack can’t update a rule, so changing one creates the new rule first and then deletes the old one: the allowed traffic never has a gap. Use inline rules or FirewallRule on a firewall, not both.

A ReservedIp is a public IPv4 or IPv6 address that belongs to the team rather than a server. Assign it to a server, and move it by changing serverId:

alchemy.run.ts
const ip = yield* BetterStack.Cloud.ReservedIp("ingress", {
type: "ipv4",
serverId: server.id,
});

It stays when the server is deleted. Outbound connections still leave from the server’s own address. Deleting a reserved IP releases the address for good. A reserved IPv4 address bills whether it is assigned or not, so the resource is tested against the offline mock only; see Compatibility and Better Stack’s pricing.

A LoadBalancer spreads TCP, UDP, HTTP and HTTPS traffic over servers. Its services listen for traffic and its targets receive it, and both are their own resources that take the load balancer’s id:

alchemy.run.ts
const lb = yield* BetterStack.Cloud.LoadBalancer("edge", {
type: "LB1",
networkIds: [network.id],
});
yield* BetterStack.Cloud.LoadBalancerService("http", {
loadBalancerId: lb.id,
protocol: "http",
listenPort: 80,
destinationPort: 3000,
healthCheck: { protocol: "http", path: "/health" },
});
yield* BetterStack.Cloud.LoadBalancerTarget("web-target", {
loadBalancerId: lb.id,
type: "server",
serverId: server.id,
usePrivateIp: true,
});

A LoadBalancerService listens on listenPort and forwards to destinationPort on the healthy targets, checking their health on its own. A LoadBalancerTarget is one server, here reached over its private IP. Creating a load balancer waits until it is running, for up to 20 minutes. Its type rescales in place.

Coming from Hetzner: a Hetzner load balancer’s services[] and targets[] are separate resources here, and networks is networkIds.

A target can select servers by their metadata instead of naming one:

alchemy.run.ts
yield* BetterStack.Cloud.LoadBalancerTarget("workers", {
loadBalancerId: lb.id,
type: "label_selector",
selector: { role: "web" },
});

A target has no update: changing it replaces it.

A Certificate is either uploaded (certificateType: "uploaded", with a Redacted privateKey) or issued and renewed by Better Stack with Let’s Encrypt ("managed", with domainNames, no wildcards). An https service terminates TLS with it:

alchemy.run.ts
const certificate = yield* BetterStack.Cloud.Certificate("site", {
certificateType: "managed",
domainNames: ["example.com", "www.example.com"],
});
yield* BetterStack.Cloud.LoadBalancerService("https", {
loadBalancerId: lb.id,
protocol: "https",
listenPort: 443,
destinationPort: 3000,
certificateIds: [certificate.id],
redirectHttp: true,
});

A managed certificate starts pending and becomes issued once Let’s Encrypt validates the domains, so point the domains at the load balancer first; the deploy doesn’t wait for it. redirectHttp: true redirects plain HTTP to HTTPS. Better Stack hosts no DNS, so the records live with your DNS provider; see DNS.

Load balancers bill, so they are tested against the offline mock only; see Compatibility.

Load balancers and reserved IPs this stack creates carry an alchemy entry in their metadata, which takes one of the 10 metadata slots, so a lost create is found again and alchemy nuke lists only Alchemy-created ones. Networks and firewalls have no metadata: their generated name identifies them. See the LoadBalancer and ReservedIp references.