@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, 06:00 UTC

main
mdx 118 lines 4.27 KB
Raw
1 ---
2 title: Federated identity
3 description: How a Lambda reaches GCP, and a Cloud Run service reaches AWS, through workload identity federation instead of long-lived keys.
4 ---
5
6 A runtime on one cloud often needs a resource on the other: a Lambda
7 publishing to a Pub/Sub topic, or a Cloud Run service sending to an SQS
8 queue. When the runtime already runs as an identity of its own cloud,
9 Alchemy lets the other cloud trust that identity. There is no service
10 account key or access key to create, store, or rotate.
11
12 You write the binding the same way as on a native runtime. Federation is
13 what Alchemy picks for you.
14
15 ## Call GCP from a Lambda
16
17 A Lambda runs as an IAM role. Bind a Pub/Sub topic to it like you would
18 on Cloud Run:
19
20 ```typescript
21 // src/worker.ts
22 import * as AWS from "@alchemy.run/aws";
23 import * as GCP from "@alchemy.run/gcp";
24 import * as Effect from "effect/Effect";
25 import * as HttpServerResponse from "effect/http/HttpServerResponse";
26
27 export const Events = GCP.PubSub.Topic("Events", {});
28
29 export default class Worker extends AWS.Lambda.Function<Worker>()(
30 "Worker",
31 { main: import.meta.url, functionUrl: true },
32 Effect.gen(function* () {
33 const publish = yield* GCP.PubSub.Publish(yield* Events);
34 return {
35 fetch: publish({ body: { messages: [{ data: btoa("hello") }] } }).pipe(
36 Effect.as(HttpServerResponse.text("published")),
37 Effect.orDie,
38 ),
39 };
40 }).pipe(Effect.provide(GCP.PubSub.PublishHttp)),
41 ) {}
42 ```
43
44 The binding creates a service account for the Lambda and grants it the
45 publisher role on the topic, as it would for any runtime. Instead of a
46 key, the Lambda's role may impersonate that account.
47
48 The trust goes through a workload identity pool with an AWS provider
49 for your account. The stack shares one pool, so every AWS runtime in the
50 stack uses it. The provider maps each caller to the role it assumed, and
51 the account grants `roles/iam.workloadIdentityUser` to that one role:
52
53 ```
54 principalSet://iam.googleapis.com/projects/123456789/locations/global/workloadIdentityPools/<pool>/attribute.aws_role/arn:aws:sts::111122223333:assumed-role/<role>
55 ```
56
57 At runtime the Lambda signs an STS `GetCallerIdentity` request with its
58 own role credentials and sends it to Google STS. Google replays the
59 request against AWS to learn which role is calling, and returns a
60 federated token. The Lambda trades that for the service account's token,
61 which lasts an hour and is exchanged again before it expires.
62
63 ## Call AWS from Cloud Run
64
65 A Cloud Run service runs as a Google service account. Bind an SQS queue
66 to it:
67
68 ```typescript
69 // src/api.ts
70 import * as AWS from "@alchemy.run/aws";
71 import * as GCP from "@alchemy.run/gcp";
72 import * as Effect from "effect/Effect";
73 import * as HttpServerResponse from "effect/http/HttpServerResponse";
74
75 export const Jobs = AWS.SQS.Queue("Jobs");
76
77 export default class Api extends GCP.Run.Service<Api>()(
78 "Api",
79 { main: import.meta.url },
80 Effect.gen(function* () {
81 const send = yield* AWS.SQS.SendMessage(Jobs);
82 return {
83 fetch: send({ MessageBody: "hello" }).pipe(
84 Effect.as(HttpServerResponse.text("sent")),
85 Effect.orDie,
86 ),
87 };
88 }).pipe(Effect.provide(AWS.SQS.SendMessageHttp)),
89 ) {}
90 ```
91
92 The binding creates an IAM role that trusts Google's ID tokens for the
93 service's runtime account, matched by the account's unique ID. At
94 runtime the service asks its metadata server for an ID token and trades
95 it for the role's credentials with `AssumeRoleWithWebIdentity`. The
96 credentials last an hour and are assumed again, with a fresh token,
97 shortly before they expire.
98
99 ## Which runtimes federate
100
101 Lambda functions, ECS tasks and services, EC2 instances and launch
102 templates, and App Runner services reach GCP this way. Cloud Run
103 services, jobs, and worker pools and Cloud Functions reach AWS this
104 way.
105
106 Runtimes without an identity the other cloud can trust still get a key:
107 a Cloudflare Worker, an EKS or GKE pod, and an AWS Batch job. The
108 binding code is the same either way.
109
110 ## New grants take a few minutes
111
112 A federated grant is not usable right away. A grant to a workload
113 identity principal can take a few minutes to land, and until it does
114 the Lambda's calls fail with `iam.serviceAccounts.getAccessToken`
115 denied. Retry the first call after a deploy.
116
117 See [IAM & bindings](/gcp/security/iam) for the roles each binding
118 grants.