The best way to control Claude Code, Codex and more from any computer or phone
| 1 | --- |
| 2 | title: Private services |
| 3 | description: Keep a Cloud Run service private and call it from another service with GCP.Run.InvokeService and Google-signed identity tokens. |
| 4 | --- |
| 5 | |
| 6 | A Cloud Run service is private unless you set `invokerIamDisabled`. |
| 7 | Callers need a Google identity token from an account that holds |
| 8 | `roles/run.invoker` on it. `InvokeService` sets that up for you. |
| 9 | |
| 10 | ## A private backend |
| 11 | |
| 12 | Leave `invokerIamDisabled` off: |
| 13 | |
| 14 | ```typescript |
| 15 | // src/Quotes.ts |
| 16 | import * as GCP from "@alchemy.run/gcp"; |
| 17 | import * as Effect from "effect/Effect"; |
| 18 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 19 | |
| 20 | export default class Quotes extends GCP.Function<Quotes>()( |
| 21 | "Quotes", |
| 22 | { main: import.meta.url }, |
| 23 | Effect.gen(function* () { |
| 24 | return { |
| 25 | fetch: HttpServerResponse.json({ quote: "Make it work, make it right." }), |
| 26 | }; |
| 27 | }), |
| 28 | ) {} |
| 29 | ``` |
| 30 | |
| 31 | Requests without a valid token get a 403 before they reach your code. |
| 32 | |
| 33 | ## Call it from another service |
| 34 | |
| 35 | ```typescript |
| 36 | // src/Gateway.ts |
| 37 | import * as GCP from "@alchemy.run/gcp"; |
| 38 | import * as Effect from "effect/Effect"; |
| 39 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 40 | import Quotes from "./Quotes.ts"; |
| 41 | |
| 42 | export default class Gateway extends GCP.Function<Gateway>()( |
| 43 | "Gateway", |
| 44 | { main: import.meta.url, invokerIamDisabled: true }, |
| 45 | Effect.gen(function* () { |
| 46 | const quotes = yield* GCP.Run.InvokeService(Quotes); |
| 47 | |
| 48 | return { |
| 49 | fetch: Effect.gen(function* () { |
| 50 | const response = yield* quotes.fetch("/quote"); |
| 51 | return HttpServerResponse.text(yield* response.text, { |
| 52 | status: response.status, |
| 53 | contentType: "application/json", |
| 54 | }); |
| 55 | }).pipe(Effect.orDie), |
| 56 | }; |
| 57 | }).pipe(Effect.provide(GCP.Run.InvokeServiceHttp)), |
| 58 | ) {} |
| 59 | ``` |
| 60 | |
| 61 | On deploy, `InvokeService` grants the Gateway `roles/run.invoker` on |
| 62 | Quotes and nothing else, and gives the Gateway Quotes' URL. Each call |
| 63 | attaches a fresh identity token. You pass a path, not a URL. |
| 64 | |
| 65 | ## Send a body |
| 66 | |
| 67 | ```typescript |
| 68 | const response = yield* orders.fetch("/orders", { |
| 69 | method: "POST", |
| 70 | headers: { "content-type": "application/json" }, |
| 71 | body: JSON.stringify({ sku: "abc", quantity: 1 }), |
| 72 | }); |
| 73 | const order = yield* response.json; |
| 74 | ``` |
| 75 | |
| 76 | ## Handle an unreachable backend |
| 77 | |
| 78 | A failed call fails with `GCP.Run.InvokeServiceError`. Turn it into a |
| 79 | 502: |
| 80 | |
| 81 | ```typescript |
| 82 | const response = yield* quotes.fetch("/quote").pipe( |
| 83 | Effect.catchTag("GCP.Run.InvokeServiceError", (error) => |
| 84 | Effect.succeed({ |
| 85 | status: 502, |
| 86 | text: Effect.succeed(JSON.stringify({ error: error.message })), |
| 87 | }), |
| 88 | ), |
| 89 | ); |
| 90 | ``` |
| 91 | |
| 92 | ## Example |
| 93 | |
| 94 | [`examples/gcp-service-to-service`](https://github.com/alchemy-run/alchemy/tree/main/examples/gcp-service-to-service) |
| 95 | is a public gateway in front of a private backend. |
| 96 | |
| 97 | ## Where next |
| 98 | |
| 99 | - [VPC & private networking](/gcp/networking/vpc) — reach private IPs |
| 100 | from Cloud Run. |
| 101 | - [IAM & bindings](/gcp/security/iam) — how the invoker grant is |
| 102 | scoped. |