The best way to control Claude Code, Codex and more from any computer or phone

main
ts 161 lines 5.66 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import {
3 amazonLinux2023,
4 Instance,
5 NetworkInterface,
6 NetworkInterfaceAttachment,
7 SecurityGroup,
8 Subnet,
9 Volume,
10 VolumeAttachment,
11 Vpc,
12 } from "@alchemy.run/aws/EC2";
13 import { expect } from "@alchemy.run/test";
14 import * as EC2 from "@distilled.cloud/aws/ec2";
15 import * as Data from "effect/Data";
16 import * as Effect from "effect/Effect";
17 import { MinimumLogLevel } from "effect/References";
18 import * as Schedule from "effect/Schedule";
19 import * as Test from "./VpcTest.ts";
20
21 const { test } = Test.make({ providers: AWS.providers() });
22
23 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
24
25 const AZ = "us-west-2a";
26
27 // A single cheap t3.micro instance hosts both the EBS volume attachment and the
28 // ENI attachment so we pay for exactly one instance launch. The instance,
29 // subnet, volume, and ENI all share one AZ (EBS/ENI attachment requires it).
30 test.provider(
31 "attach a volume and an ENI to a t3.micro instance, then detach and delete",
32 (stack) =>
33 Effect.gen(function* () {
34 yield* stack.destroy();
35
36 const imageId = amazonLinux2023();
37
38 const deployed = yield* stack.deploy(
39 Effect.gen(function* () {
40 const vpc = yield* Vpc("StorageVpc", { cidrBlock: "10.90.0.0/16" });
41 const subnet = yield* Subnet("StorageSubnet", {
42 vpcId: vpc.vpcId,
43 cidrBlock: "10.90.1.0/24",
44 availabilityZone: AZ,
45 mapPublicIpOnLaunch: true,
46 });
47 const sg = yield* SecurityGroup("StorageSg", {
48 vpcId: vpc.vpcId,
49 description: "alchemy storage smoke test",
50 egress: [
51 {
52 ipProtocol: "-1",
53 cidrIpv4: "0.0.0.0/0",
54 description: "all outbound",
55 },
56 ],
57 });
58 const instance = yield* Instance("StorageInstance", {
59 imageId,
60 instanceType: "t3.micro",
61 subnetId: subnet.subnetId,
62 securityGroupIds: [sg.groupId],
63 });
64 const volume = yield* Volume("StorageVolume", {
65 availabilityZone: AZ,
66 size: 1,
67 volumeType: "gp3",
68 });
69 const volumeAttachment = yield* VolumeAttachment("StorageVolumeAttachment", {
70 volumeId: volume.volumeId,
71 instanceId: instance.instanceId,
72 device: "/dev/sdf",
73 });
74 const eni = yield* NetworkInterface("StorageEni", {
75 subnetId: subnet.subnetId,
76 description: "alchemy storage smoke test secondary eni",
77 securityGroupIds: [sg.groupId],
78 });
79 const eniAttachment = yield* NetworkInterfaceAttachment("StorageEniAttachment", {
80 networkInterfaceId: eni.networkInterfaceId,
81 instanceId: instance.instanceId,
82 deviceIndex: 1,
83 });
84 return {
85 instance,
86 volume,
87 volumeAttachment,
88 eni,
89 eniAttachment,
90 };
91 }),
92 );
93
94 const { instance, volume, volumeAttachment, eni, eniAttachment } = deployed;
95
96 // Volume attachment reached 'attached'.
97 expect(volumeAttachment.state).toBe("attached");
98 expect(volumeAttachment.instanceId).toBe(instance.instanceId);
99
100 const observedVolume = yield* EC2.describeVolumes({
101 VolumeIds: [volume.volumeId],
102 });
103 const attachment = observedVolume.Volumes?.[0]?.Attachments?.find(
104 (a) => a.InstanceId === instance.instanceId,
105 );
106 expect(attachment?.State).toBe("attached");
107 expect(attachment?.Device).toBe("/dev/sdf");
108
109 // ENI attachment reached 'attached' on device index 1.
110 expect(eniAttachment.status).toBe("attached");
111 expect(eniAttachment.attachmentId).toMatch(/^eni-attach-/);
112
113 const observedEni = yield* EC2.describeNetworkInterfaces({
114 NetworkInterfaceIds: [eni.networkInterfaceId],
115 });
116 const eniAttach = observedEni.NetworkInterfaces?.[0]?.Attachment;
117 expect(eniAttach?.Status).toBe("attached");
118 expect(eniAttach?.InstanceId).toBe(instance.instanceId);
119 expect(eniAttach?.DeviceIndex).toBe(1);
120
121 // Tear everything down and verify the volume + ENI are gone out-of-band.
122 yield* stack.destroy();
123
124 yield* assertVolumeGone(volume.volumeId);
125 yield* assertEniGone(eni.networkInterfaceId);
126 }).pipe(logLevel),
127 { tags: ["provider:aws", "provider:aws:ec2", "live"], timeout: 240_000 },
128 );
129
130 const assertVolumeGone = Effect.fn(function* (volumeId: string) {
131 yield* EC2.describeVolumes({ VolumeIds: [volumeId] }).pipe(
132 Effect.flatMap((result) => {
133 const state = result.Volumes?.[0]?.State;
134 // `deleting` is terminal — EC2 can keep a deleted volume visible in
135 // this state for many minutes of internal bookkeeping.
136 return state === undefined || state === "deleted" || state === "deleting"
137 ? Effect.void
138 : Effect.fail(new StillExists());
139 }),
140 Effect.retry({
141 while: (e) => e instanceof StillExists,
142 schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(20)]),
143 }),
144 Effect.catchTag("InvalidVolume.NotFound", () => Effect.void),
145 );
146 });
147
148 const assertEniGone = Effect.fn(function* (eniId: string) {
149 yield* EC2.describeNetworkInterfaces({
150 NetworkInterfaceIds: [eniId],
151 }).pipe(
152 Effect.flatMap(() => Effect.fail(new StillExists())),
153 Effect.retry({
154 while: (e) => e instanceof StillExists,
155 schedule: Schedule.max([Schedule.exponential(200), Schedule.recurs(20)]),
156 }),
157 Effect.catchTag("InvalidNetworkInterfaceID.NotFound", () => Effect.void),
158 );
159 });
160
161 class StillExists extends Data.TaggedError("StillExists") {}