@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
mdx 109 lines 3 KB
Raw
1 ---
2 title: Cloud Functions
3 description: Deploy an Effect fetch handler as a 2nd-gen Cloud Function — built by Google from an uploaded bundle, so no local Docker is needed.
4 ---
5
6 `GCP.CloudFunctions.Function` deploys the same Effect program as
7 [Cloud Run](/gcp/compute/cloud-run), built a different way: Alchemy
8 bundles your code for Node.js, uploads it, and Google builds it. You
9 can deploy from a machine without Docker.
10
11 ## Declare a function
12
13 ```typescript
14 // src/Notes.ts
15 import * as GCP from "@alchemy.run/gcp";
16 import * as Effect from "effect/Effect";
17 import * as HttpServerResponse from "effect/http/HttpServerResponse";
18 import { NotesDb } from "./resources.ts";
19
20 export default class Notes extends GCP.CloudFunctions.Function<Notes>()(
21 "Notes",
22 { main: import.meta.url },
23 Effect.gen(function* () {
24 const db = yield* GCP.Firestore.ReadWriteDatabase(NotesDb);
25
26 return {
27 fetch: Effect.gen(function* () {
28 const { documents } = yield* db.list("notes", { pageSize: 100 });
29 return yield* HttpServerResponse.json({
30 notes: documents.map((doc) => doc.fields),
31 });
32 }).pipe(Effect.orDie),
33 };
34 }).pipe(Effect.provide(GCP.Firestore.ReadWriteDatabaseHttp)),
35 ) {}
36 ```
37
38 The function runs on Node.js 22 and serves `fetch` through Google's
39 Functions Framework.
40
41 ## Add it to the Stack
42
43 ```typescript
44 // alchemy.run.ts
45 Effect.gen(function* () {
46 const notes = yield* Notes;
47 return { url: notes.url };
48 }),
49 ```
50
51 ## Make it public
52
53 A 2nd-gen function is served by a Cloud Run service. Grant
54 `roles/run.invoker` to `allUsers` on that service to open it up:
55
56 ```typescript
57 yield* GCP.IAM.Member("PublicInvoker", {
58 kind: "run.service",
59 name: notes.service.as<string>(),
60 role: "roles/run.invoker",
61 member: "allUsers",
62 });
63 ```
64
65 Without it, callers need a Google identity token.
66
67 ## Consume a topic
68
69 Event sources work as on Cloud Run. Provide the Cloud Functions
70 implementation:
71
72 ```typescript
73 yield* GCP.PubSub.consumeTopicMessages(Orders, (messages) =>
74 messages.pipe(Stream.runForEach(({ message }) => Effect.log(message))),
75 );
76 // …provided with Effect.provide(GCP.CloudFunctions.TopicEventSource)
77 ```
78
79 ## Deploy your own source
80
81 Leave out `main` to deploy an archive you built yourself:
82
83 ```typescript
84 const hello = yield* GCP.CloudFunctions.Function("Hello", {
85 buildConfig: {
86 runtime: "nodejs22",
87 entryPoint: "helloHttp",
88 source: { storageSource: { bucket: "my-source", object: "hello.zip" } },
89 },
90 });
91 ```
92
93 ## Cloud Functions or Cloud Run?
94
95 Both run your program on Cloud Run. Pick Cloud Functions when you
96 can't run Docker where you deploy. Pick a Cloud Run service otherwise:
97 it deploys faster and gives you every revision setting.
98
99 ## Example
100
101 [`examples/gcp-cloud-function`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-cloud-function)
102 is a notes API over Firestore.
103
104 ## Where next
105
106 - [Cloud Run](/gcp/compute/cloud-run) — the same program with a
107 locally built image.
108 - [CloudFunctions reference](/providers/gcp/cloudfunctions#function) —
109 every prop and attribute.