1import * as autoscaling from "@distilled.cloud/aws/auto-scaling";
2import * as Effect from "effect/Effect";
3import * as Layer from "effect/Layer";
4import * as Binding from "../../Binding.ts";
5import { isInstance } from "../EC2/Instance.ts";
6import { isBindingHost } from "../Lambda/Function.ts";
7import type { AutoScalingGroup } from "./AutoScalingGroup.ts";
8import {
9 Standby,
10 type EnterStandbyRequest,
11 type ExitStandbyRequest,
12} from "./Standby.ts";
13
14// Bespoke (not the shared scaffold): a multi-operation client over
15// `enterStandby` + `exitStandby` behind one IAM grant.
16export const StandbyHttp = Layer.effect(
17 Standby,
18 Effect.gen(function* () {
19 const enter = yield* autoscaling.enterStandby;
20 const exit = yield* autoscaling.exitStandby;
21
22 return Effect.fn(function* (group: AutoScalingGroup) {
23 const AutoScalingGroupName = yield* group.autoScalingGroupName;
24 if (!globalThis.__ALCHEMY_RUNTIME__) {
25 const host = yield* Binding.Host;
26 if (isBindingHost(host) || isInstance(host)) {
27 yield* host.bind`Allow(${host}, AWS.AutoScaling.Standby(${group}))`({
28 policyStatements: [
29 {
30 Effect: "Allow",
31 Action: ["autoscaling:EnterStandby", "autoscaling:ExitStandby"],
32 Resource: [group.autoScalingGroupArn],
33 },
34 ],
35 });
36 }
37 }
38 return {
39 enter: Effect.fn(`AWS.AutoScaling.EnterStandby(${group.LogicalId})`)(
40 function* (request: EnterStandbyRequest) {
41 return yield* enter({
42 ...request,
43 AutoScalingGroupName: yield* AutoScalingGroupName,
44 });
45 },
46 ),
47 exit: Effect.fn(`AWS.AutoScaling.ExitStandby(${group.LogicalId})`)(
48 function* (request: ExitStandbyRequest) {
49 return yield* exit({
50 ...request,
51 AutoScalingGroupName: yield* AutoScalingGroupName,
52 });
53 },
54 ),
55 };
56 });
57 }),
58);