Infrastructure as effects
Sam relates runtime clients, business logic, infrastructure policies, and deployed resources in one typed program27. The compiler can check these relationships before deployment.¶
Contract → surfaces
A contract21 defines shared input, output, failures, and metadata. Projections turn registered capabilities into command line, HTTP, MCP tools, browser RPC, and code mode23 surfaces. Each surface26 derives its input and failure definitions from the same schemas.¶
Code → bindings → resources
A client operation can imply a policy requirement supplied when the function is built. Sam describes bindings25 as permissions and environment variables together at 37:09. An Alchemy Layer can declare the cloud resources behind a service. The Stack8 declares cloud resources.¶
Worked example: a D1 Layer
In the database cartridge38, makeD1Vendor declares SQLite migrations and a D1 database.
The database declaration receives those migrations through its migrations option.¶
const makeD1Vendor = Effect.fn("makeD1Vendor")(function* makeD1Vendor({
id,
}: D1Options) {
const migrations = yield* Drizzle.Schema(`${id}-d1-schema`, {
dialect: "sqlite",
out: databaseMigrationDirectory("d1"),
schema: databaseSchemaFile("d1"),
});
const database = yield* Cloudflare.D1.Database(`${id}-d1`, { migrations });
return { _tag: "D1" as const, database };
});D1(options) builds the Layer below.
It combines RunLogLayer(vendor) with the DatabaseVendor service and provides QueryDatabaseBinding.
The package keeps its database declaration and service construction30 together.¶
export const D1 = (options: D1Options) =>
Layer.unwrap(
Effect.gen(function* buildD1Layer() {
const vendor = yield* makeD1Vendor(options);
return Layer.mergeAll(
RunLogLayer(vendor),
Layer.succeed(DatabaseVendor, vendor)
);
})
).pipe(Layer.provide(Cloudflare.D1.QueryDatabaseBinding));Trace the declaration first, then the returned Layer. Read Database11 for its current callers and local verification commands.¶
Plan → review → deploy
A plan model8 describes infrastructure changes before they apply. Sam connects Effect purity with a reviewable plan and a concrete deployment result at 12:22. Tests using real infrastructure check behavior after deployment at 16:01.¶
The compiler checks the joins
Effect9 tracks required services in its type. Effects and Layers let the type system track the resource providers a Stack needs at 07:01. Sam describes a type error when code still requires a removed binding25 at 15:25. The compiler can check these relationships before deployment: Happy Path 116, 28:00.¶
Cartridges
A cartridge38 is a package that owns one job and the resources it needs. Replacing its Layer changes the implementation and dependencies without changing callers.¶
Lifecycles
Use an XState machine27 for named states, retries, cancellation, or a lifecycle to resume or inspect. Model the states and the events that move between them.¶
The fence
The fence50 is the compiler checks, lint rules, tests, and hooks that enforce project rules. These rules cannot establish whole-program behavior.¶
Cloud as a library
Sam’s phrase is that the whole cloud becomes a library6, at 29:50. He argues that infrastructure tools can retain application-code properties such as debugging and tests at 03:50.¶
The joins at a glance
- Contract → surfaces:
- What the program checks: Shared input and failure schemas¶
- Where to read more: One capability, every surface21¶
- Code → bindings → resources:
- What the program checks: Required services and resource providers¶
- Where to read more: Bindings25 · Infrastructure as Effects, Office Hours 278¶
- Plan → review → deploy:
- What the program checks: Changes before application; real-infrastructure behavior after deployment¶
- Where to read more: Plan model8¶
- Compiler → joins:
- What the program checks: Relationships before deployment; code that requires a removed binding¶
- Where to read more: Happy Path 1168¶
- Cartridges38:
- What the program checks: Removal leaves no loose resources or broken imports¶
- Where to read more: Cartridges38¶
- Lifecycles:
- What the program checks: Named states and the events between them¶
- Where to read more: Lifecycles are machines27¶
- Fence50:
- What the program checks: Project rules; whole-program behavior remains outside these checks¶
- Where to read more: The fence50¶
- Cloud as a library6:
- What the program checks: Debugging and tests for infrastructure code¶
- Where to read more: Cloud as a library6¶
Sources
- #116 Infrastructure as Effects with Sam Goodwin - YouTube. Sam Goodwin. Client and policy discussion; used for resource requirements implied by runtime operations. Accessed 2026-10-01.
- Alchemy, Infrastructure as Effects | Office Hours 27 🔥 - YouTube. Sam Goodwin. Bundle separation discussion; used for keeping providers out of runtime code. Accessed 2026-10-01.
- Sam Goodwin - Alchemy - devtools.fm. devtools.fm. Plan model and cloud as a library discussions. Accessed 2026-10-11.
- rat-stack D1 resource and service Layer.. rat-stack. The D1 constructor declares migrations and a database, combines service Layers, and supplies the query binding. Accessed 2026-11-08.
Last changed 2026-11-08 in 9196534. Source on GitHub. Change log.
Linked from
- Agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
Alchemy: GitHub describes alchemy-run/alchemy as “ Infrastructure as Effects”.
- Agent rules for rat-stack (AGENTS.md). What you may change, which commands to run, and which changes need approval.
Link context
run/sql providers ( Infrastructure as Effects) for every cloud resource; declared in apps/infra/alchemy.run.ts, authenticated through Alchemy profiles, never through env vars in this repo.
- CAFE names the Cloudflare, Alchemy, Foldkit and Effect stack. CAFE is a named stack: Cloudflare runs the app, Alchemy declares it, Foldkit draws the UI, and Effect does the typed work.
See one capability, every surface and infrastructure as effects.¶
- Cartridges. A cartridge owns one job and brings the infrastructure its service needs.
ts declares the D1 schema and database.¶
- Change log. What changed in the files served here, newest first.
Infrastructure as effects — new page: Initial commit from joelhooks/rat-stack template
- Context and requirements. Effect requirements make a computation's needed services visible in its type.
See Effect basics for the whole type, and infrastructure as effects for Sam’s infrastructure reading.¶
- Full agent guide. An Effect stack so pure (aspirational) Kit Langton will blush.
- Rat Stack lore | rat-stack. Short, source-grounded notes on the ideas and decisions behind rat-stack.
Infrastructure as effects — Sam Goodwin relates application operations to infrastructure requirements in one typed program.
- Rat's Nest keeps the code public and the inventory private. Joel's purpose-built on-prem cloud uses rat-stack and Alchemy, with private inventory outside the public repo.
It uses rat-stack and Alchemy to declare infrastructure as code.¶
- Source change history (log.md). The generated change log as Markdown.
Infrastructure as effects — new page: Initial commit from joelhooks/rat-stack template