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

main
mdx 127 lines 4.34 KB
Raw
1 ---
2 title: Federated identity
3 description: How a Cloud Run service reaches AWS, and a Lambda reaches GCP, 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 Cloud Run
7 service sending to an SQS queue, or a Lambda publishing to a Pub/Sub
8 topic. When the runtime already runs as an identity of its own cloud,
9 Alchemy lets the other cloud trust that identity. There is no access
10 key or service account 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 AWS from Cloud Run
16
17 A Cloud Run service runs as a Google service account. Bind an SQS queue
18 to it like you would on a Lambda:
19
20 ```typescript
21 // src/api.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 Jobs = AWS.SQS.Queue("Jobs");
28
29 export default class Api extends GCP.Run.Service<Api>()(
30 "Api",
31 { main: import.meta.url },
32 Effect.gen(function* () {
33 const send = yield* AWS.SQS.SendMessage(Jobs);
34 return {
35 fetch: send({ MessageBody: "hello" }).pipe(
36 Effect.as(HttpServerResponse.text("sent")),
37 Effect.orDie,
38 ),
39 };
40 }).pipe(Effect.provide(AWS.SQS.SendMessageHttp)),
41 ) {}
42 ```
43
44 The binding creates an IAM role that trusts Google's ID tokens for that
45 one service account, and puts `sqs:SendMessage` on the role. The trust
46 policy matches the account's unique ID, which the service exposes as
47 `serviceAccountUniqueId`:
48
49 ```json
50 {
51 "Effect": "Allow",
52 "Principal": { "Federated": "accounts.google.com" },
53 "Action": "sts:AssumeRoleWithWebIdentity",
54 "Condition": {
55 "StringEquals": {
56 "accounts.google.com:sub": "104729384756102938475",
57 "accounts.google.com:aud": "104729384756102938475",
58 "accounts.google.com:oaud": "sts.amazonaws.com"
59 }
60 }
61 }
62 ```
63
64 At runtime the service asks its metadata server for an ID token and
65 trades it for the role's credentials with `AssumeRoleWithWebIdentity`.
66 The credentials last an hour and are assumed again, with a fresh token,
67 shortly before they expire.
68
69 ## Call GCP from a Lambda
70
71 A Lambda runs as an IAM role. Bind a Pub/Sub topic to it:
72
73 ```typescript
74 // src/worker.ts
75 import * as AWS from "@alchemy.run/aws";
76 import * as GCP from "@alchemy.run/gcp";
77 import * as Effect from "effect/Effect";
78 import * as HttpServerResponse from "effect/http/HttpServerResponse";
79
80 export const Events = GCP.PubSub.Topic("Events", {});
81
82 export default class Worker extends AWS.Lambda.Function<Worker>()(
83 "Worker",
84 { main: import.meta.url, functionUrl: true },
85 Effect.gen(function* () {
86 const publish = yield* GCP.PubSub.Publish(yield* Events);
87 return {
88 fetch: publish({ body: { messages: [{ data: btoa("hello") }] } }).pipe(
89 Effect.as(HttpServerResponse.text("published")),
90 Effect.orDie,
91 ),
92 };
93 }).pipe(Effect.provide(GCP.PubSub.PublishHttp)),
94 ) {}
95 ```
96
97 The binding creates a service account that holds the publisher role,
98 and lets the Lambda's role impersonate it. The trust goes through a
99 workload identity pool with an AWS provider for your account. The stack
100 shares one pool, so every AWS runtime in the stack uses it.
101
102 At runtime the Lambda signs an STS `GetCallerIdentity` request with its
103 own role credentials and sends it to Google STS. Google replays the
104 request against AWS to learn which role is calling, and returns a token
105 for the service account. The token lasts an hour and is exchanged again
106 before it expires.
107
108 ## Which runtimes federate
109
110 Lambda functions, ECS tasks and services, EC2 instances and launch
111 templates, and App Runner services reach GCP this way. Cloud Run
112 services, jobs, and worker pools and Cloud Functions reach AWS this
113 way.
114
115 Runtimes without an identity the other cloud can trust still get a key:
116 a Cloudflare Worker, an EKS or GKE pod, and an AWS Batch job. The
117 binding code is the same either way.
118
119 ## New grants take a few minutes
120
121 A freshly created role or federated grant is not usable right away. AWS
122 usually accepts a new role's trust within seconds. A GCP grant to a
123 federated principal can take a few minutes. Until it lands, calls fail
124 with a permission error, so retry the first call after a deploy.
125
126 See [IAM & bindings](/gcp/security/iam) for the roles each GCP binding
127 grants.