Infrastructure as Effects
Infrastructure as Effects is Alchemy’s model for building applications. The code that runs inside a Worker or Lambda Function and the infrastructure it runs on are one Effect program. A Runtime, such as a Worker, is a Resource that carries its own code, and the resources it uses are wired in as typed values:
import * as Cloudflare from "alchemy/Cloudflare";import * as Effect from "effect/Effect";import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
export const Uploads = Cloudflare.R2.Bucket("Uploads");
export default Cloudflare.Worker( "Api", { main: import.meta.url }, Effect.gen(function* () { const bucket = yield* Cloudflare.R2.ReadWriteBucket(Uploads);
return { fetch: Effect.gen(function* () { const obj = yield* bucket.get("hello.txt"); return obj ? HttpServerResponse.text(yield* obj.text()) : HttpServerResponse.text("Not found", { status: 404 }); }), }; }).pipe(Effect.provide(Cloudflare.R2.ReadWriteBucketBinding)),);Traditional IaC splits this in two. One file declares the bucket and the Worker. Another file is the code the Worker runs, reaching for the bucket through its environment. Alchemy supports that shape too:
import * as Cloudflare from "alchemy/Cloudflare";
export const Uploads = Cloudflare.R2.Bucket("Uploads");
export const Api = Cloudflare.Worker("Api", { main: "./src/api.ts", env: { Uploads },});
export type ApiEnv = Cloudflare.InferEnv<typeof Api>;import type { ApiEnv } from "../alchemy.run.ts";
export default { async fetch(request: Request, env: ApiEnv) { const obj = await env.Uploads.get("hello.txt"); return obj ? new Response(await obj.text()) : new Response("Not found", { status: 404 }); },};Two files, held together by the name Uploads. The Effect form above
is those two files as one. bucket is a value the handler closes over,
and the code that uses the bucket sits next to the line that declares
it.
Everything in this section is in that one Worker. Here is the map.
Runtimes carry their code
Section titled “Runtimes carry their code”Cloudflare.Worker is a Runtime. Its props are the cloud
configuration, and main: import.meta.url tells Alchemy to bundle
this file’s default export as the Worker’s code. The Effect is that
code, and it always has the same shape:
Effect.gen(function* () { // bind what you need const bucket = yield* Cloudflare.R2.ReadWriteBucket(Uploads);
// return what you expose return { fetch: /* ... */ };});Runtime covers that shape, the
fetch and RPC interface it returns, and the three ways to declare
one.
Bindings wire resources in
Section titled “Bindings wire resources in”yield* Cloudflare.R2.ReadWriteBucket(Uploads) is a Binding. It
hands back a typed client and generates whatever the client needs to
work: a native binding on Cloudflare, an IAM statement and an
environment variable on AWS:
const getItem = yield* AWS.DynamoDB.GetItem(Jobs);// → { Action: ["dynamodb:GetItem"], Resource: [Jobs.tableArn] }// → Jobs_tableName=Jobs-a1b2c3There is no env.Uploads to reach for. The binding is the SDK. See
Bindings for how they work.
An Event Source is a
Binding that runs an Effect or Stream in the background, whenever
something happens on the resource. A
Sink is the converse, a Binding
you sink a Stream into:
const outbound = yield* AWS.SQS.QueueSink(Outbound);
yield* AWS.SQS.consumeQueueMessages(Inbound, (records) => records.pipe( Stream.map((record) => record.body.toUpperCase()), Stream.run(outbound), ),);Layers package it all
Section titled “Layers package it all”A Binding is a contract plus a Layer that implements it, which is why
ReadWriteBucketBinding can be swapped for ReadWriteBucketHttp
without touching the handler. The same split works for services of
your own. Declare a Context.Service, implement it with a Layer that
carries its own resources and bindings, and the handler depends on the
service alone:
Effect.gen(function* () { const jobs = yield* JobService;
return { fetch: Effect.gen(function* () { return HttpServerResponse.json(yield* jobs.getJob("job-1")); }), };}).pipe(Effect.provide(JobServiceKV))Provide JobServiceKV and a KV namespace is created and bound. Provide
JobServiceDynamo and a DynamoDB table is instead. The handler doesn’t
change. Layers builds one from
scratch, and Circular Bindings
uses the split to let two Workers call each other.
Two phases, one program
Section titled “Two phases, one program”The Worker’s Effect runs in two phases. The outer Effect is the
Construction phase. It runs at deploy time, where the binding
records what it needs, and again at cold start, where the same line
builds the live client. The inner fetch Effect is the Runtime
phase. It runs per request, only inside the deployed Worker:
Effect.gen(function* () { // Construction: deploy time and cold start const bucket = yield* Cloudflare.R2.ReadWriteBucket(Uploads);
return { // Runtime: per request, inside the deployed Worker fetch: Effect.gen(function* () { const obj = yield* bucket.get("hello.txt"); }), };});Both phases are one program, so bucket is a value the handler closes
over rather than something looked up at request time.
Phases explains exactly what runs
when.
Built into every Runtime
Section titled “Built into every Runtime”Telemetry is a Layer too. Provide an exporter alongside your bindings and every request, queue batch, and cron fire exports the traces and logs Effect already emits. Custom Runtime brings the whole model to a compute target Alchemy doesn’t ship.
Where next
Section titled “Where next”- Runtime — Resources that carry the code they run.
- Bindings — what one
yield*generates. - Layers — encapsulating infrastructure behind services.
- Event Sources — resources that trigger your Function, as Effect
Streams. - Sinks — resources you write to, as Effect
Sinks. - Phases — plantime vs runtime in depth.
- Circular Bindings — two services that reference each other.
- Telemetry — exporters as Layers.
- Custom Runtime — bring the model to a new compute target.