The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as location from "@distilled.cloud/aws/location"; |
| 2 | import * as Output from "alchemy/Output"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import type { Role } from "../IAM/Role.ts"; |
| 7 | import { StartJob, type StartJobRequest } from "./StartJob.ts"; |
| 8 | |
| 9 | /** |
| 10 | * Bespoke (not via `BindingHttp.ts`): StartJob is the one Location binding |
| 11 | * that injects an identifier from a *foreign* resource (the IAM execution |
| 12 | * role) and needs a second `iam:PassRole` grant alongside the `geo:` action. |
| 13 | */ |
| 14 | export const StartJobHttp = Layer.effect( |
| 15 | StartJob, |
| 16 | Effect.gen(function* () { |
| 17 | const bind = yield* AWSBindings.Bind; |
| 18 | const startJob = yield* AWSBindings.withCredentials(location.startJob); |
| 19 | |
| 20 | return Effect.fn(function* (executionRole: Role) { |
| 21 | const RoleArn = yield* executionRole.roleArn; |
| 22 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 23 | yield* bind`AWS.Location.StartJob(${executionRole})`({ |
| 24 | policyStatements: [ |
| 25 | // Jobs are named at runtime, so their ARNs are unknowable at |
| 26 | // deploy time. |
| 27 | { |
| 28 | Effect: "Allow", |
| 29 | Action: ["geo:StartJob"], |
| 30 | Resource: ["*"], |
| 31 | }, |
| 32 | // CRITICAL: without iam:PassRole on the execution role, |
| 33 | // StartJob fails only at runtime with an AccessDenied. |
| 34 | { |
| 35 | Effect: "Allow", |
| 36 | Action: ["iam:PassRole"], |
| 37 | Resource: [Output.interpolate`${executionRole.roleArn}`], |
| 38 | Condition: { |
| 39 | StringEquals: { |
| 40 | "iam:PassedToService": "location.amazonaws.com", |
| 41 | }, |
| 42 | }, |
| 43 | }, |
| 44 | ], |
| 45 | }); |
| 46 | } |
| 47 | return Effect.fn(`AWS.Location.StartJob(${executionRole.LogicalId})`)(function* ( |
| 48 | request: StartJobRequest, |
| 49 | ) { |
| 50 | return yield* startJob({ |
| 51 | ...request, |
| 52 | ExecutionRoleArn: yield* RoleArn, |
| 53 | }); |
| 54 | }); |
| 55 | }); |
| 56 | }), |
| 57 | ); |