The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as backup from "@distilled.cloud/aws/backup"; |
| 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 { StartRestoreJob, type StartRestoreJobRequest } from "./StartRestoreJob.ts"; |
| 8 | |
| 9 | export const StartRestoreJobHttp = Layer.effect( |
| 10 | StartRestoreJob, |
| 11 | Effect.gen(function* () { |
| 12 | const bind = yield* AWSBindings.Bind; |
| 13 | const startRestoreJob = yield* AWSBindings.withCredentials(backup.startRestoreJob); |
| 14 | |
| 15 | return Effect.fn(function* <R extends Role>(restoreRole: R) { |
| 16 | const RoleArn = yield* restoreRole.roleArn; |
| 17 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 18 | yield* bind`AWS.Backup.StartRestoreJob(${restoreRole})`({ |
| 19 | policyStatements: [ |
| 20 | // StartRestoreJob authorizes on the recovery point's |
| 21 | // underlying resource ARN (a snapshot ARN unknowable at |
| 22 | // deploy time), so the grant is on `*`. |
| 23 | { |
| 24 | Effect: "Allow", |
| 25 | Action: ["backup:StartRestoreJob"], |
| 26 | Resource: ["*"], |
| 27 | }, |
| 28 | // CRITICAL: without iam:PassRole on the restore role, |
| 29 | // StartRestoreJob fails only at runtime with an AccessDenied. |
| 30 | { |
| 31 | Effect: "Allow", |
| 32 | Action: ["iam:PassRole"], |
| 33 | Resource: [Output.interpolate`${restoreRole.roleArn}`], |
| 34 | Condition: { |
| 35 | StringEquals: { |
| 36 | "iam:PassedToService": "backup.amazonaws.com", |
| 37 | }, |
| 38 | }, |
| 39 | }, |
| 40 | ], |
| 41 | }); |
| 42 | } |
| 43 | return Effect.fn(`AWS.Backup.StartRestoreJob(${restoreRole.LogicalId})`)(function* ( |
| 44 | request: StartRestoreJobRequest, |
| 45 | ) { |
| 46 | return yield* startRestoreJob({ |
| 47 | ...request, |
| 48 | IamRoleArn: request.IamRoleArn ?? (yield* RoleArn), |
| 49 | }); |
| 50 | }); |
| 51 | }); |
| 52 | }), |
| 53 | ); |