| 1 | import * as autoscaling from "@distilled.cloud/aws/auto-scaling"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Layer from "effect/Layer"; |
| 4 | import * as Binding from "../../Binding.ts"; |
| 5 | import { isInstance } from "../EC2/Instance.ts"; |
| 6 | import { isBindingHost } from "../Lambda/Function.ts"; |
| 7 | import type { AutoScalingGroup } from "./AutoScalingGroup.ts"; |
| 8 | import { |
| 9 | CompleteLifecycleAction, |
| 10 | type CompleteLifecycleActionRequest, |
| 11 | type RecordLifecycleActionHeartbeatRequest, |
| 12 | } from "./CompleteLifecycleAction.ts"; |
| 13 | |
| 14 | export const CompleteLifecycleActionHttp = Layer.effect( |
| 15 | CompleteLifecycleAction, |
| 16 | Effect.gen(function* () { |
| 17 | const complete = yield* autoscaling.completeLifecycleAction; |
| 18 | const heartbeat = yield* autoscaling.recordLifecycleActionHeartbeat; |
| 19 | |
| 20 | return Effect.fn(function* (group: AutoScalingGroup) { |
| 21 | const AutoScalingGroupName = yield* group.autoScalingGroupName; |
| 22 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 23 | const host = yield* Binding.Host; |
| 24 | if (isBindingHost(host) || isInstance(host)) { |
| 25 | yield* host.bind`Allow(${host}, AWS.AutoScaling.CompleteLifecycleAction(${group}))`( |
| 26 | { |
| 27 | policyStatements: [ |
| 28 | { |
| 29 | Effect: "Allow", |
| 30 | Action: [ |
| 31 | "autoscaling:CompleteLifecycleAction", |
| 32 | "autoscaling:RecordLifecycleActionHeartbeat", |
| 33 | ], |
| 34 | Resource: [group.autoScalingGroupArn], |
| 35 | }, |
| 36 | ], |
| 37 | }, |
| 38 | ); |
| 39 | } |
| 40 | } |
| 41 | return { |
| 42 | complete: Effect.fn( |
| 43 | `AWS.AutoScaling.CompleteLifecycleAction(${group.LogicalId})`, |
| 44 | )(function* (request: CompleteLifecycleActionRequest) { |
| 45 | return yield* complete({ |
| 46 | ...request, |
| 47 | AutoScalingGroupName: yield* AutoScalingGroupName, |
| 48 | }); |
| 49 | }), |
| 50 | heartbeat: Effect.fn( |
| 51 | `AWS.AutoScaling.RecordLifecycleActionHeartbeat(${group.LogicalId})`, |
| 52 | )(function* (request: RecordLifecycleActionHeartbeatRequest) { |
| 53 | return yield* heartbeat({ |
| 54 | ...request, |
| 55 | AutoScalingGroupName: yield* AutoScalingGroupName, |
| 56 | }); |
| 57 | }), |
| 58 | }; |
| 59 | }); |
| 60 | }), |
| 61 | ); |