Skip to content

Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.

3.6 KB
Raw Blame
---
title: Infrastructure as Effects
description: One Effect program models both your runtime code and the infrastructure it runs on — Functions carry code, Bindings wire resources into them, Phases split deploy from runtime, Layers package it all behind typed services.
---

Infrastructure as Effects extends Infrastructure as Code by
combining application code and infrastructure configuration into a
single type-safe program built from **Effects** and **Layers**.

There is no separate "infra" project plus "handler" project — a
Worker or Lambda Function is a
[Resource](/infrastructure-as-code/resource) that ships its runtime
code, with the resources it uses wired in as Bindings.

## The whole model in one Worker

```typescript
import * as Cloudflare from "alchemy/Cloudflare";
import * as Effect from "effect/Effect";
import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
import { Bucket } from "./bucket.ts";

export default Cloudflare.Worker(
  "Api",
  { main: import.meta.url },
  Effect.gen(function* () {
    // Init — runs at deploy time (records the binding) and at cold start (builds the live client)
    const bucket = yield* Cloudflare.R2.ReadWriteBucket(Bucket);

    return {
      // Runtime — runs per request, inside the deployed Worker
      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)),
);
```

- **[Functions & Servers](/infrastructure-as-effects/functions-and-servers)** — the Worker is a Resource that carries its handler; config and code deploy together.
- **[Bindings](/infrastructure-as-effects/binding)** — `yield* Cloudflare.R2.ReadWriteBucket(Bucket)` declares the capability; the provided `ReadWriteBucketBinding` Layer is what wires it up — here as a native Worker binding — and hands back the typed client. The binding **is** the SDK; there is no `env.BUCKET`.
- **[Phases](/infrastructure-as-effects/phases)** — the outer Effect runs at plantime to record bindings and again at cold start; the inner Effect runs per request.
- **[Layers](/infrastructure-as-effects/layers)** — resources + bindings + runtime glue package into an Effect Layer behind a typed service, so implementations swap without touching consumers.

Effect style is optional — bindings also work as plain props on an
`async fetch` handler with a typed env via `InferEnv`; see
[Effect handlers vs async handlers](/infrastructure-as-effects/functions-and-servers#effect-handlers-vs-async-handlers).

## Where next

1. [Functions & Servers](/infrastructure-as-effects/functions-and-servers) — Resources that carry runtime code.
2. [Bindings](/infrastructure-as-effects/binding) — what `bind()` generates.
3. [Event Sources](/infrastructure-as-effects/event-sources) — resources that trigger your Function, as Effect `Stream`s.
4. [Sinks](/infrastructure-as-effects/sinks) — resources you write to, as Effect `Sink`s.
5. [APIs](/apis) — schemaless RPC between your Functions and Servers, plus schema'd surfaces for trust boundaries.
6. [Phases](/infrastructure-as-effects/phases) — plantime vs runtime in depth.
7. [Layers](/infrastructure-as-effects/layers) — encapsulating infrastructure behind services.
8. [Circular Bindings](/infrastructure-as-effects/circular-bindings) — two services that reference each other.
9. [Custom Runtime](/infrastructure-as-effects/custom-runtime) — bring the model to a new compute target.