The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { ScalableTarget, ScalingPolicy } from "@alchemy.run/aws/ApplicationAutoScaling"; |
| 3 | import { Cluster } from "@alchemy.run/aws/ECS/Cluster"; |
| 4 | import { Service } from "@alchemy.run/aws/ECS/Service"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as aas from "@distilled.cloud/aws/application-auto-scaling"; |
| 8 | import * as ecs from "@distilled.cloud/aws/ecs"; |
| 9 | import * as Output from "alchemy/Output"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import { reclaimTaskDefinitionFamily } from "../ECS/reclaimTaskDefinitionFamily.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | const clusterName = "alchemy-test-aas-ecs"; |
| 17 | |
| 18 | // The flagship ECS-service scaling flow: register the service's DesiredCount |
| 19 | // dimension (min 1 / max 3), attach a CPU target-tracking policy, verify both |
| 20 | // out-of-band, update the policy target in place, and destroy in dependency |
| 21 | // order (policy -> target -> service -> cluster). |
| 22 | // |
| 23 | // To stay inside the speed budget we avoid building/pushing a Docker image |
| 24 | // and register a minimal task definition against a public image out of band |
| 25 | // (the same pattern as test/AWS/ECS/Service.test.ts). The Service provider |
| 26 | // waits for the deployment to stabilize, so the task must actually be able |
| 27 | // to start: run in the account's default VPC (public subnets + |
| 28 | // `assignPublicIp`, the provider's fallback) so Fargate can pull the public |
| 29 | // nginx image without a NAT. |
| 30 | test.provider( |
| 31 | "ecs service DesiredCount target + CPU target tracking policy", |
| 32 | (stack) => |
| 33 | Effect.gen(function* () { |
| 34 | yield* stack.destroy(); |
| 35 | |
| 36 | // Pre-clean: reclaim revisions a previously killed run may have left, |
| 37 | // then register this run's revision. |
| 38 | yield* reclaimTaskDefinitionFamily(clusterName); |
| 39 | |
| 40 | const registered = yield* ecs.registerTaskDefinition({ |
| 41 | family: "alchemy-test-aas-ecs", |
| 42 | networkMode: "awsvpc", |
| 43 | requiresCompatibilities: ["FARGATE"], |
| 44 | cpu: "256", |
| 45 | memory: "512", |
| 46 | containerDefinitions: [ |
| 47 | { |
| 48 | name: "app", |
| 49 | image: "public.ecr.aws/nginx/nginx:stable", |
| 50 | essential: true, |
| 51 | portMappings: [{ containerPort: 80, protocol: "tcp" }], |
| 52 | }, |
| 53 | ], |
| 54 | }); |
| 55 | const taskDefinitionArn = registered.taskDefinition?.taskDefinitionArn!; |
| 56 | // Safety net: fully reclaim the out-of-band family on scope close even |
| 57 | // if the body fails — deregister + hard-delete so no INACTIVE revision |
| 58 | // survives the run. |
| 59 | yield* Effect.addFinalizer(() => |
| 60 | reclaimTaskDefinitionFamily(clusterName).pipe(Effect.ignore), |
| 61 | ); |
| 62 | |
| 63 | const deploy = (targetValue: number) => |
| 64 | stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const cluster = yield* Cluster("AasEcsCluster", { clusterName }); |
| 67 | // No vpcId/subnets: fall back to the default VPC's public |
| 68 | // subnets with assignPublicIp so the task can pull its image. |
| 69 | const service = yield* Service("AasEcsService", { |
| 70 | cluster, |
| 71 | task: { taskDefinitionArn, containerName: "app", port: 80 }, |
| 72 | desiredCount: 1, |
| 73 | }); |
| 74 | const target = yield* ScalableTarget("AasEcsTarget", { |
| 75 | serviceNamespace: "ecs", |
| 76 | resourceId: Output.interpolate`service/${clusterName}/${service.serviceName}`, |
| 77 | scalableDimension: "ecs:service:DesiredCount", |
| 78 | minCapacity: 1, |
| 79 | maxCapacity: 3, |
| 80 | }); |
| 81 | const policy = yield* ScalingPolicy("AasEcsCpuPolicy", { |
| 82 | serviceNamespace: target.serviceNamespace, |
| 83 | resourceId: target.resourceId, |
| 84 | scalableDimension: target.scalableDimension, |
| 85 | targetTracking: { |
| 86 | TargetValue: targetValue, |
| 87 | PredefinedMetricSpecification: { |
| 88 | PredefinedMetricType: "ECSServiceAverageCPUUtilization", |
| 89 | }, |
| 90 | ScaleOutCooldown: 60, |
| 91 | ScaleInCooldown: 60, |
| 92 | }, |
| 93 | }); |
| 94 | // ECS also supports StepScaling — cover that policy branch here |
| 95 | // (DynamoDB targets reject it, so the DynamoDB suites cannot). |
| 96 | const stepPolicy = yield* ScalingPolicy("AasEcsStepPolicy", { |
| 97 | serviceNamespace: target.serviceNamespace, |
| 98 | resourceId: target.resourceId, |
| 99 | scalableDimension: target.scalableDimension, |
| 100 | stepScaling: { |
| 101 | AdjustmentType: "ChangeInCapacity", |
| 102 | Cooldown: 60, |
| 103 | MetricAggregationType: "Average", |
| 104 | StepAdjustments: [{ MetricIntervalLowerBound: 0, ScalingAdjustment: 1 }], |
| 105 | }, |
| 106 | }); |
| 107 | return { |
| 108 | serviceName: service.serviceName.as<string>(), |
| 109 | resourceId: target.resourceId.as<string>(), |
| 110 | scalableTargetArn: target.scalableTargetArn.as<string>(), |
| 111 | policyName: policy.policyName.as<string>(), |
| 112 | policyArn: policy.policyArn.as<string>(), |
| 113 | stepPolicyName: stepPolicy.policyName.as<string>(), |
| 114 | stepPolicyType: stepPolicy.policyType.as<string>(), |
| 115 | }; |
| 116 | }), |
| 117 | ); |
| 118 | |
| 119 | const created = yield* deploy(50); |
| 120 | expect(created.resourceId).toEqual(`service/${clusterName}/${created.serviceName}`); |
| 121 | expect(created.scalableTargetArn).toContain("scalable-target/"); |
| 122 | |
| 123 | // Out-of-band: the DesiredCount dimension is registered min 1 / max 3. |
| 124 | const targets = yield* aas.describeScalableTargets({ |
| 125 | ServiceNamespace: "ecs", |
| 126 | ResourceIds: [created.resourceId], |
| 127 | ScalableDimension: "ecs:service:DesiredCount", |
| 128 | }); |
| 129 | const observedTarget = targets.ScalableTargets?.[0]; |
| 130 | expect(observedTarget?.MinCapacity).toBe(1); |
| 131 | expect(observedTarget?.MaxCapacity).toBe(3); |
| 132 | |
| 133 | // Out-of-band: the tracking policy exists with its managed alarms. |
| 134 | const policies = yield* aas.describeScalingPolicies({ |
| 135 | ServiceNamespace: "ecs", |
| 136 | PolicyNames: [created.policyName], |
| 137 | ResourceId: created.resourceId, |
| 138 | }); |
| 139 | const observedPolicy = policies.ScalingPolicies?.[0]; |
| 140 | expect(observedPolicy?.PolicyType).toBe("TargetTrackingScaling"); |
| 141 | expect(observedPolicy?.TargetTrackingScalingPolicyConfiguration?.TargetValue).toBe(50); |
| 142 | expect((observedPolicy?.Alarms ?? []).length).toBeGreaterThan(0); |
| 143 | |
| 144 | // Out-of-band: the step scaling policy exists with its adjustments. |
| 145 | expect(created.stepPolicyType).toBe("StepScaling"); |
| 146 | const stepPolicies = yield* aas.describeScalingPolicies({ |
| 147 | ServiceNamespace: "ecs", |
| 148 | PolicyNames: [created.stepPolicyName], |
| 149 | ResourceId: created.resourceId, |
| 150 | }); |
| 151 | const observedStepPolicy = stepPolicies.ScalingPolicies?.[0]; |
| 152 | expect(observedStepPolicy?.PolicyType).toBe("StepScaling"); |
| 153 | expect(observedStepPolicy?.StepScalingPolicyConfiguration?.StepAdjustments).toEqual([ |
| 154 | { MetricIntervalLowerBound: 0, ScalingAdjustment: 1 }, |
| 155 | ]); |
| 156 | |
| 157 | // Update the policy target in place — same policy ARN. |
| 158 | const updated = yield* deploy(30); |
| 159 | expect(updated.policyArn).toEqual(created.policyArn); |
| 160 | const policiesAfterUpdate = yield* aas.describeScalingPolicies({ |
| 161 | ServiceNamespace: "ecs", |
| 162 | PolicyNames: [created.policyName], |
| 163 | ResourceId: created.resourceId, |
| 164 | }); |
| 165 | expect( |
| 166 | policiesAfterUpdate.ScalingPolicies?.[0]?.TargetTrackingScalingPolicyConfiguration |
| 167 | ?.TargetValue, |
| 168 | ).toBe(30); |
| 169 | |
| 170 | // Destroy in dependency order and verify deregistration out-of-band. |
| 171 | yield* stack.destroy(); |
| 172 | const targetsAfterDestroy = yield* aas |
| 173 | .describeScalableTargets({ |
| 174 | ServiceNamespace: "ecs", |
| 175 | ResourceIds: [created.resourceId], |
| 176 | ScalableDimension: "ecs:service:DesiredCount", |
| 177 | }) |
| 178 | .pipe( |
| 179 | Effect.repeat({ |
| 180 | schedule: Schedule.spaced("2 seconds"), |
| 181 | until: (res) => (res.ScalableTargets ?? []).length === 0, |
| 182 | times: 10, |
| 183 | }), |
| 184 | ); |
| 185 | expect(targetsAfterDestroy.ScalableTargets ?? []).toHaveLength(0); |
| 186 | const policiesAfterDestroy = yield* aas.describeScalingPolicies({ |
| 187 | ServiceNamespace: "ecs", |
| 188 | PolicyNames: [created.policyName], |
| 189 | ResourceId: created.resourceId, |
| 190 | }); |
| 191 | expect(policiesAfterDestroy.ScalingPolicies ?? []).toHaveLength(0); |
| 192 | |
| 193 | yield* reclaimTaskDefinitionFamily(clusterName); |
| 194 | }), |
| 195 | { |
| 196 | tags: ["provider:aws", "provider:aws:applicationautoscaling", "provider:aws:ecs", "live"], |
| 197 | timeout: 240_000, |
| 198 | }, |
| 199 | ); |