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 { Volume } from "@alchemy.run/aws/EC2"; |
| 3 | import { Alias, Key, type AliasName } from "@alchemy.run/aws/KMS"; |
| 4 | import { expect } from "@alchemy.run/test"; |
| 5 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 6 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 7 | import * as kms from "@distilled.cloud/aws/kms"; |
| 8 | import * as Drift from "alchemy/Drift"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import { State } from "alchemy/State/State"; |
| 11 | import * as Data from "effect/Data"; |
| 12 | import * as Effect from "effect/Effect"; |
| 13 | import { MinimumLogLevel } from "effect/References"; |
| 14 | import * as Schedule from "effect/Schedule"; |
| 15 | |
| 16 | const { test } = Test.make({ providers: AWS.providers() }); |
| 17 | |
| 18 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 19 | |
| 20 | // The testing account is in us-west-2. |
| 21 | const AZ = "us-west-2a"; |
| 22 | |
| 23 | test.provider( |
| 24 | "create, verify, and delete a gp3 volume", |
| 25 | (stack) => |
| 26 | Effect.gen(function* () { |
| 27 | yield* stack.destroy(); |
| 28 | |
| 29 | const volume = yield* stack.deploy( |
| 30 | Effect.gen(function* () { |
| 31 | return yield* Volume("TestGp3Volume", { |
| 32 | availabilityZone: AZ, |
| 33 | size: 1, |
| 34 | volumeType: "gp3", |
| 35 | }); |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | expect(volume.volumeId).toMatch(/^vol-/); |
| 40 | expect(volume.availabilityZone).toBe(AZ); |
| 41 | expect(volume.volumeType).toBe("gp3"); |
| 42 | expect(volume.size).toBe(1); |
| 43 | |
| 44 | // Out-of-band verification. |
| 45 | const observed = yield* EC2.describeVolumes({ |
| 46 | VolumeIds: [volume.volumeId], |
| 47 | }); |
| 48 | const v = observed.Volumes?.[0]; |
| 49 | expect(v?.VolumeType).toBe("gp3"); |
| 50 | expect(v?.Size).toBe(1); |
| 51 | expect(v?.AvailabilityZone).toBe(AZ); |
| 52 | expect(v?.State === "available" || v?.State === "creating").toBe(true); |
| 53 | expect(v?.Tags?.find((tag) => tag.Key === "alchemy::instance")?.Value).toBeTruthy(); |
| 54 | |
| 55 | // list() enumerates the deployed volume. |
| 56 | const provider = yield* Provider.findProvider(Volume); |
| 57 | const all = yield* provider.list(); |
| 58 | expect(all.some((x) => x.volumeId === volume.volumeId)).toBe(true); |
| 59 | |
| 60 | yield* stack.destroy(); |
| 61 | yield* assertVolumeDeleted(volume.volumeId); |
| 62 | }).pipe(logLevel), |
| 63 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 120_000 }, |
| 64 | ); |
| 65 | |
| 66 | class AliasStillExists extends Data.TaggedError("AliasStillExists") {} |
| 67 | class KeyNotPendingDeletion extends Data.TaggedError("KeyNotPendingDeletion") {} |
| 68 | |
| 69 | const kmsObservationSchedule = Schedule.max([Schedule.fixed("1 second"), Schedule.recurs(30)]); |
| 70 | |
| 71 | /** |
| 72 | * Idempotent out-of-band backstop for the stack-managed fixture. KMS keys |
| 73 | * cannot be hard-deleted, so the clean terminal state is alias NotFound plus |
| 74 | * key PendingDeletion. The stack provider remains the owner; this finalizer |
| 75 | * closes failure/timeout gaps and observes both terminal states before return. |
| 76 | */ |
| 77 | const releaseManagedKey = (aliasName: AliasName | undefined, keyId: string | undefined) => |
| 78 | Effect.gen(function* () { |
| 79 | let targetKeyId = keyId; |
| 80 | if (aliasName !== undefined) { |
| 81 | const viaAlias = yield* kms.describeKey({ KeyId: aliasName }).pipe( |
| 82 | Effect.map((response) => response.KeyMetadata), |
| 83 | Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)), |
| 84 | ); |
| 85 | targetKeyId ??= viaAlias?.KeyId; |
| 86 | |
| 87 | yield* kms.deleteAlias({ AliasName: aliasName }).pipe( |
| 88 | Effect.retry({ |
| 89 | while: (error) => |
| 90 | error._tag === "DependencyTimeoutException" || error._tag === "KMSInternalException", |
| 91 | schedule: Schedule.max([Schedule.fixed("500 millis"), Schedule.recurs(8)]), |
| 92 | }), |
| 93 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 94 | ); |
| 95 | |
| 96 | yield* kms.describeKey({ KeyId: aliasName }).pipe( |
| 97 | Effect.flatMap(() => Effect.fail(new AliasStillExists())), |
| 98 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 99 | Effect.retry({ |
| 100 | while: (error) => error._tag === "AliasStillExists", |
| 101 | schedule: kmsObservationSchedule, |
| 102 | }), |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | if (targetKeyId !== undefined) { |
| 107 | const metadata = yield* kms.describeKey({ KeyId: targetKeyId }).pipe( |
| 108 | Effect.map((response) => response.KeyMetadata), |
| 109 | Effect.catchTag("NotFoundException", () => Effect.succeed(undefined)), |
| 110 | ); |
| 111 | if (metadata && metadata.KeyState !== "PendingDeletion") { |
| 112 | yield* kms |
| 113 | .scheduleKeyDeletion({ |
| 114 | KeyId: targetKeyId, |
| 115 | PendingWindowInDays: 7, |
| 116 | }) |
| 117 | .pipe( |
| 118 | Effect.retry({ |
| 119 | while: (error) => |
| 120 | error._tag === "DependencyTimeoutException" || |
| 121 | error._tag === "KMSInternalException", |
| 122 | schedule: Schedule.max([Schedule.fixed("500 millis"), Schedule.recurs(8)]), |
| 123 | }), |
| 124 | Effect.catchTag(["KMSInvalidStateException", "NotFoundException"], () => Effect.void), |
| 125 | ); |
| 126 | } |
| 127 | |
| 128 | yield* kms.describeKey({ KeyId: targetKeyId }).pipe( |
| 129 | Effect.flatMap((response) => |
| 130 | response.KeyMetadata?.KeyState === "PendingDeletion" |
| 131 | ? Effect.void |
| 132 | : Effect.fail(new KeyNotPendingDeletion()), |
| 133 | ), |
| 134 | Effect.catchTag("NotFoundException", () => Effect.void), |
| 135 | Effect.retry({ |
| 136 | while: (error) => error._tag === "KeyNotPendingDeletion", |
| 137 | schedule: kmsObservationSchedule, |
| 138 | }), |
| 139 | ); |
| 140 | } |
| 141 | }); |
| 142 | |
| 143 | test.provider( |
| 144 | "create an encrypted volume with a managed KMS key and alias", |
| 145 | (stack) => { |
| 146 | let fixtureAliasName: AliasName | undefined; |
| 147 | let fixtureKeyId: string | undefined; |
| 148 | return Effect.gen(function* () { |
| 149 | yield* stack.destroy(); |
| 150 | |
| 151 | const deployed = yield* stack.deploy( |
| 152 | Effect.gen(function* () { |
| 153 | const key = yield* Key("EncryptedVolumeKey", { |
| 154 | description: "Alchemy EC2 encrypted-volume test key", |
| 155 | deletionWindow: "7 days", |
| 156 | tags: { fixture: "ec2-volume" }, |
| 157 | }); |
| 158 | const alias = yield* Alias("EncryptedVolumeAlias", { |
| 159 | targetKeyId: key.keyId, |
| 160 | }); |
| 161 | const volume = yield* Volume("TestEncryptedVolume", { |
| 162 | availabilityZone: AZ, |
| 163 | size: 1, |
| 164 | volumeType: "gp3", |
| 165 | encrypted: true, |
| 166 | kmsKeyId: alias.aliasName, |
| 167 | }); |
| 168 | return { alias, key, volume }; |
| 169 | }), |
| 170 | ); |
| 171 | fixtureAliasName = deployed.alias.aliasName; |
| 172 | fixtureKeyId = deployed.key.keyId; |
| 173 | |
| 174 | expect(deployed.volume.encrypted).toBe(true); |
| 175 | |
| 176 | const observed = yield* EC2.describeVolumes({ |
| 177 | VolumeIds: [deployed.volume.volumeId], |
| 178 | }); |
| 179 | const v = observed.Volumes?.[0]; |
| 180 | expect(v?.Encrypted).toBe(true); |
| 181 | expect(v?.KmsKeyId).toBe(deployed.key.keyArn); |
| 182 | |
| 183 | yield* stack.destroy(); |
| 184 | yield* assertVolumeDeleted(deployed.volume.volumeId); |
| 185 | yield* releaseManagedKey(fixtureAliasName, fixtureKeyId); |
| 186 | }).pipe( |
| 187 | logLevel, |
| 188 | // Always run both the state-owned destroy and the direct idempotent |
| 189 | // backstop. This covers assertions, failures, and runner interruption |
| 190 | // while preserving dependency order (Volume -> Alias -> Key). |
| 191 | Effect.ensuring( |
| 192 | Effect.gen(function* () { |
| 193 | yield* stack.destroy().pipe(Effect.ignore); |
| 194 | // Finalizers require a `never` error channel. Escalate a cleanup |
| 195 | // failure to a visible defect instead of swallowing it. |
| 196 | yield* releaseManagedKey(fixtureAliasName, fixtureKeyId).pipe(Effect.orDie); |
| 197 | }).pipe(logLevel), |
| 198 | ), |
| 199 | ); |
| 200 | }, |
| 201 | { |
| 202 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:kms", "live"], |
| 203 | timeout: 120_000, |
| 204 | }, |
| 205 | ); |
| 206 | |
| 207 | // A create whose reply was lost leaves no saved output, and the tag lookup |
| 208 | // can miss a volume EC2 has not indexed yet. The ClientToken alchemy derives |
| 209 | // from the resource's generation makes the retried CreateVolume return the |
| 210 | // first volume instead of a second one. |
| 211 | test.provider( |
| 212 | "a create retried after a lost reply returns the same volume", |
| 213 | (stack) => |
| 214 | Effect.gen(function* () { |
| 215 | yield* stack.destroy(); |
| 216 | |
| 217 | const deployWith = (tags: Record<string, string>) => |
| 218 | stack.deploy( |
| 219 | Effect.gen(function* () { |
| 220 | return yield* Volume("ClientTokenVolume", { |
| 221 | availabilityZone: AZ, |
| 222 | size: 1, |
| 223 | volumeType: "gp3", |
| 224 | tags, |
| 225 | }); |
| 226 | }), |
| 227 | ); |
| 228 | const key = { stack: stack.name, stage: stack.stage, fqn: "ClientTokenVolume" }; |
| 229 | |
| 230 | // Simulate a lost CreateVolume reply: drop the saved output and the |
| 231 | // generation tag the recovery lookup finds the volume by. |
| 232 | const loseReply = Effect.fn(function* (volumeId: string) { |
| 233 | const state = yield* yield* State; |
| 234 | const row = yield* state.get(key); |
| 235 | if (row?.status !== "created") { |
| 236 | return yield* Effect.die(new Error("Expected a created volume row")); |
| 237 | } |
| 238 | const { attr: _attr, ...rest } = row; |
| 239 | yield* state.set({ ...key, value: { ...rest, status: "creating" } }); |
| 240 | yield* EC2.deleteTags({ |
| 241 | Resources: [volumeId], |
| 242 | Tags: [{ Key: "alchemy::instance" }], |
| 243 | DryRun: false, |
| 244 | }); |
| 245 | return row.instanceId; |
| 246 | }); |
| 247 | |
| 248 | const ownedVolumes = EC2.describeVolumes({ |
| 249 | Filters: [ |
| 250 | { Name: "tag:alchemy::stack", Values: [stack.name] }, |
| 251 | { Name: "tag:alchemy::stage", Values: [stack.stage] }, |
| 252 | { Name: "tag:alchemy::id", Values: ["ClientTokenVolume"] }, |
| 253 | ], |
| 254 | }).pipe(Effect.map((owned) => owned.Volumes ?? [])); |
| 255 | |
| 256 | const first = yield* deployWith({ v: "1" }); |
| 257 | const generation = yield* loseReply(first.volumeId); |
| 258 | |
| 259 | const retried = yield* deployWith({ v: "1" }); |
| 260 | expect(retried.volumeId).toBe(first.volumeId); |
| 261 | |
| 262 | // Only one volume carries this resource's ownership tags, and the sync |
| 263 | // put the generation tag back. |
| 264 | const volumes = yield* ownedVolumes; |
| 265 | expect(volumes.map((v) => v.VolumeId)).toEqual([first.volumeId]); |
| 266 | expect(volumes[0]?.Tags?.find((tag) => tag.Key === "alchemy::instance")?.Value).toBe( |
| 267 | generation, |
| 268 | ); |
| 269 | |
| 270 | // Lose the reply again, then delete the volume outside the stack. EC2 |
| 271 | // refuses the retried token for a deleted volume, so the create must |
| 272 | // fall back to a new volume rather than fail. |
| 273 | yield* loseReply(first.volumeId); |
| 274 | yield* EC2.deleteVolume({ VolumeId: first.volumeId, DryRun: false }); |
| 275 | yield* assertVolumeDeleted(first.volumeId); |
| 276 | |
| 277 | const recreated = yield* deployWith({ v: "1" }); |
| 278 | expect(recreated.volumeId).not.toBe(first.volumeId); |
| 279 | expect((yield* ownedVolumes).map((v) => v.VolumeId)).toEqual([recreated.volumeId]); |
| 280 | |
| 281 | // Delete the volume outside the stack and change a tag. The update |
| 282 | // finds the saved volume gone and creates under a token keyed on it. |
| 283 | // Then lose that reply: state still holds the deleted volume and the |
| 284 | // recovery lookup misses, so the retry must get the same new volume. |
| 285 | const state = yield* yield* State; |
| 286 | const beforeRecreate = yield* state.get(key); |
| 287 | yield* EC2.deleteVolume({ VolumeId: recreated.volumeId, DryRun: false }); |
| 288 | yield* assertVolumeDeleted(recreated.volumeId); |
| 289 | const updated = yield* deployWith({ v: "2" }); |
| 290 | expect(updated.volumeId).not.toBe(recreated.volumeId); |
| 291 | |
| 292 | yield* state.set({ ...key, value: beforeRecreate! }); |
| 293 | yield* EC2.deleteTags({ |
| 294 | Resources: [updated.volumeId], |
| 295 | Tags: [{ Key: "alchemy::instance" }], |
| 296 | DryRun: false, |
| 297 | }); |
| 298 | const retriedUpdate = yield* deployWith({ v: "2" }); |
| 299 | expect(retriedUpdate.volumeId).toBe(updated.volumeId); |
| 300 | expect((yield* ownedVolumes).map((v) => v.VolumeId)).toEqual([updated.volumeId]); |
| 301 | |
| 302 | yield* stack.destroy(); |
| 303 | yield* assertVolumeDeleted(updated.volumeId); |
| 304 | }).pipe(logLevel), |
| 305 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 300_000 }, |
| 306 | ); |
| 307 | |
| 308 | // `read` observes the volume, so drift sees it unchanged after a deploy and |
| 309 | // missing once it is deleted outside the stack. Repair recreates it under a |
| 310 | // new ClientToken. |
| 311 | test.provider( |
| 312 | "drift reports a volume deleted outside the stack as missing and repair recreates it", |
| 313 | (stack) => |
| 314 | Effect.gen(function* () { |
| 315 | yield* stack.destroy(); |
| 316 | |
| 317 | const program = Effect.gen(function* () { |
| 318 | return yield* Volume("DriftVolume", { |
| 319 | availabilityZone: AZ, |
| 320 | size: 1, |
| 321 | volumeType: "gp3", |
| 322 | }); |
| 323 | }); |
| 324 | |
| 325 | const volume = yield* stack.deploy(program); |
| 326 | const state = yield* yield* State; |
| 327 | const key = { stack: stack.name, stage: stack.stage, fqn: "DriftVolume" }; |
| 328 | const beforeRepair = yield* state.get(key); |
| 329 | |
| 330 | const detect = Drift.detect({ name: stack.name, stage: stack.stage }).pipe( |
| 331 | Effect.provide(stack.state), |
| 332 | ); |
| 333 | expect((yield* detect).resources.DriftVolume?.action).toBe("unchanged"); |
| 334 | |
| 335 | yield* EC2.deleteVolume({ VolumeId: volume.volumeId, DryRun: false }); |
| 336 | yield* assertVolumeDeleted(volume.volumeId); |
| 337 | expect((yield* detect).resources.DriftVolume?.action).toBe("missing"); |
| 338 | |
| 339 | const repaired = yield* Drift.repair({ name: stack.name, stage: stack.stage }).pipe( |
| 340 | Effect.provide(stack.state), |
| 341 | ); |
| 342 | expect(repaired.resources.DriftVolume?.action).toBe("recreated"); |
| 343 | const recreated = repaired.resources.DriftVolume?.attr as typeof volume; |
| 344 | expect(recreated.volumeId).not.toBe(volume.volumeId); |
| 345 | expect(recreated.state).toBe("available"); |
| 346 | |
| 347 | // Lose the repair's reply: state still holds the deleted volume and the |
| 348 | // recovery lookup misses. Repairing again must return the same volume |
| 349 | // rather than create a second one. |
| 350 | yield* state.set({ ...key, value: beforeRepair! }); |
| 351 | yield* EC2.deleteTags({ |
| 352 | Resources: [recreated.volumeId], |
| 353 | Tags: [{ Key: "alchemy::instance" }], |
| 354 | DryRun: false, |
| 355 | }); |
| 356 | const retried = yield* Drift.repair({ name: stack.name, stage: stack.stage }).pipe( |
| 357 | Effect.provide(stack.state), |
| 358 | ); |
| 359 | expect(retried.resources.DriftVolume?.action).toBe("recreated"); |
| 360 | expect((retried.resources.DriftVolume?.attr as typeof volume).volumeId).toBe( |
| 361 | recreated.volumeId, |
| 362 | ); |
| 363 | const owned = yield* EC2.describeVolumes({ |
| 364 | Filters: [ |
| 365 | { Name: "tag:alchemy::stack", Values: [stack.name] }, |
| 366 | { Name: "tag:alchemy::stage", Values: [stack.stage] }, |
| 367 | { Name: "tag:alchemy::id", Values: ["DriftVolume"] }, |
| 368 | ], |
| 369 | }); |
| 370 | expect((owned.Volumes ?? []).map((v) => v.VolumeId)).toEqual([recreated.volumeId]); |
| 371 | |
| 372 | yield* stack.destroy(); |
| 373 | yield* assertVolumeDeleted(recreated.volumeId); |
| 374 | }).pipe(logLevel), |
| 375 | { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 300_000 }, |
| 376 | ); |
| 377 | |
| 378 | const assertVolumeDeleted = Effect.fn(function* (volumeId: string) { |
| 379 | yield* EC2.describeVolumes({ VolumeIds: [volumeId] }).pipe( |
| 380 | Effect.flatMap((result) => { |
| 381 | const state = result.Volumes?.[0]?.State; |
| 382 | return state === undefined || state === "deleted" |
| 383 | ? Effect.void |
| 384 | : Effect.fail(new VolumeStillExists()); |
| 385 | }), |
| 386 | Effect.retry({ |
| 387 | while: (e) => e._tag === "VolumeStillExists", |
| 388 | schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(8)]), |
| 389 | }), |
| 390 | Effect.catchTag("InvalidVolume.NotFound", () => Effect.void), |
| 391 | ); |
| 392 | }); |
| 393 | |
| 394 | class VolumeStillExists extends Data.TaggedError("VolumeStillExists") {} |