Cloud Functions
GCP.CloudFunctions.Function deploys the same Effect program as
Cloud Run, built a different way: Alchemy
bundles your code for Node.js, uploads it, and Google builds it. You
can deploy from a machine without Docker.
Declare a function
Section titled “Declare a function”import * as GCP from "@alchemy.run/gcp";import * as Effect from "effect/Effect";import * as HttpServerResponse from "effect/http/HttpServerResponse";import { NotesDb } from "./resources.ts";
export default class Notes extends GCP.CloudFunctions.Function<Notes>()( "Notes", { main: import.meta.url }, Effect.gen(function* () { const db = yield* GCP.Firestore.ReadWriteDatabase(NotesDb);
return { fetch: Effect.gen(function* () { const { documents } = yield* db.list("notes", { pageSize: 100 }); return yield* HttpServerResponse.json({ notes: documents.map((doc) => doc.fields), }); }).pipe(Effect.orDie), }; }).pipe(Effect.provide(GCP.Firestore.ReadWriteDatabaseHttp)),) {}The function runs on Node.js 22 and serves fetch through Google’s
Functions Framework.
Add it to the Stack
Section titled “Add it to the Stack”Effect.gen(function* () { const notes = yield* Notes; return { url: notes.url };}),Make it public
Section titled “Make it public”A 2nd-gen function is served by a Cloud Run service. Grant
roles/run.invoker to allUsers on that service to open it up:
yield* GCP.IAM.Member("PublicInvoker", { kind: "run.service", name: notes.service.as<string>(), role: "roles/run.invoker", member: "allUsers",});Without it, callers need a Google identity token.
Consume a topic
Section titled “Consume a topic”Event sources work as on Cloud Run. Provide the Cloud Functions implementation:
yield* GCP.PubSub.consumeTopicMessages(Orders, (messages) => messages.pipe(Stream.runForEach(({ message }) => Effect.log(message))),);// …provided with Effect.provide(GCP.CloudFunctions.TopicEventSource)Deploy your own source
Section titled “Deploy your own source”Leave out main to deploy an archive you built yourself:
const hello = yield* GCP.CloudFunctions.Function("Hello", { buildConfig: { runtime: "nodejs22", entryPoint: "helloHttp", source: { storageSource: { bucket: "my-source", object: "hello.zip" } }, },});Cloud Functions or Cloud Run?
Section titled “Cloud Functions or Cloud Run?”Both run your program on Cloud Run. Pick Cloud Functions when you can’t run Docker where you deploy. Pick a Cloud Run service otherwise: it deploys faster and gives you every revision setting.
Example
Section titled “Example”examples/gcp-cloud-function
is a notes API over Firestore.
Where next
Section titled “Where next”- Cloud Run — the same program with a locally built image.
- CloudFunctions reference — every prop and attribute.