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 { Subnet, Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import { LogGroup } from "@alchemy.run/aws/Logs"; |
| 4 | import { Firewall, FirewallPolicy, LoggingConfiguration } from "@alchemy.run/aws/NetworkFirewall"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 7 | import * as nfw from "@distilled.cloud/aws/network-firewall"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Schedule from "effect/Schedule"; |
| 10 | |
| 11 | const { test } = Test.make({ providers: AWS.providers() }); |
| 12 | |
| 13 | // Ungated typed-error probes: prove the distilled error union carries the |
| 14 | // not-found tags this provider's read/delete paths depend on. |
| 15 | test.provider( |
| 16 | "describeFirewall on a nonexistent firewall fails with ResourceNotFoundException", |
| 17 | () => |
| 18 | Effect.gen(function* () { |
| 19 | const error = yield* Effect.flip( |
| 20 | nfw.describeFirewall({ |
| 21 | FirewallName: "alchemy-nonexistent-nfw-firewall-probe", |
| 22 | }), |
| 23 | ); |
| 24 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 25 | }), |
| 26 | { tags: ["provider:aws", "provider:aws:networkfirewall", "live"] }, |
| 27 | ); |
| 28 | |
| 29 | test.provider( |
| 30 | "describeLoggingConfiguration on a nonexistent firewall fails with ResourceNotFoundException", |
| 31 | () => |
| 32 | Effect.gen(function* () { |
| 33 | const error = yield* Effect.flip( |
| 34 | nfw.describeLoggingConfiguration({ |
| 35 | FirewallName: "alchemy-nonexistent-nfw-firewall-probe", |
| 36 | }), |
| 37 | ); |
| 38 | expect(error._tag).toBe("ResourceNotFoundException"); |
| 39 | }), |
| 40 | { tags: ["provider:aws", "provider:aws:networkfirewall", "live"] }, |
| 41 | ); |
| 42 | |
| 43 | const assertFirewallGone = (name: string) => |
| 44 | Effect.gen(function* () { |
| 45 | const error = yield* Effect.flip(nfw.describeFirewall({ FirewallName: name })); |
| 46 | if (error._tag !== "ResourceNotFoundException") { |
| 47 | return yield* Effect.fail(new Error(`firewall '${name}' still exists (${error._tag})`)); |
| 48 | } |
| 49 | }).pipe( |
| 50 | Effect.retry({ |
| 51 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(18)]), |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | // A firewall takes ~5-10 minutes to provision its endpoints and a similar |
| 56 | // window to deprovision on delete. The full lifecycle is gated behind |
| 57 | // AWS_TEST_NETWORKFIREWALL=1 and always destroys what it created. |
| 58 | test.provider.skipIf(!process.env.AWS_TEST_NETWORKFIREWALL)( |
| 59 | "create firewall with logging configuration, verify, destroy", |
| 60 | (stack) => |
| 61 | Effect.gen(function* () { |
| 62 | yield* stack.destroy(); |
| 63 | |
| 64 | const { firewall, logging, policy } = yield* stack.deploy( |
| 65 | Effect.gen(function* () { |
| 66 | const vpc = yield* Vpc("Vpc", { |
| 67 | cidrBlock: "10.77.0.0/16", |
| 68 | tags: { fixture: "nfw-firewall" }, |
| 69 | }); |
| 70 | const subnet = yield* Subnet("FirewallSubnet", { |
| 71 | vpcId: vpc.vpcId, |
| 72 | cidrBlock: "10.77.1.0/24", |
| 73 | tags: { fixture: "nfw-firewall" }, |
| 74 | }); |
| 75 | const policy = yield* FirewallPolicy("Policy", { |
| 76 | firewallPolicy: { |
| 77 | StatelessDefaultActions: ["aws:pass"], |
| 78 | StatelessFragmentDefaultActions: ["aws:pass"], |
| 79 | }, |
| 80 | }); |
| 81 | const firewall = yield* Firewall("Firewall", { |
| 82 | firewallPolicyArn: policy.firewallPolicyArn, |
| 83 | vpcId: vpc.vpcId, |
| 84 | subnetMappings: [{ SubnetId: subnet.subnetId }], |
| 85 | description: "alchemy network firewall test", |
| 86 | tags: { fixture: "nfw-firewall" }, |
| 87 | }); |
| 88 | const logGroup = yield* LogGroup("FlowLogs", { |
| 89 | retention: "1 day", |
| 90 | }); |
| 91 | const logging = yield* LoggingConfiguration("Logging", { |
| 92 | firewallArn: firewall.firewallArn, |
| 93 | logDestinationConfigs: [ |
| 94 | { |
| 95 | LogType: "FLOW", |
| 96 | LogDestinationType: "CloudWatchLogs", |
| 97 | LogDestination: { logGroup: logGroup.logGroupName }, |
| 98 | }, |
| 99 | ], |
| 100 | }); |
| 101 | return { firewall, logging, policy }; |
| 102 | }), |
| 103 | ); |
| 104 | |
| 105 | expect(firewall.firewallArn).toContain(":firewall/"); |
| 106 | expect(firewall.firewallId).toBeDefined(); |
| 107 | expect(firewall.endpointIds.length).toBe(1); |
| 108 | expect(firewall.endpointIds[0]).toMatch(/^vpce-/); |
| 109 | expect(logging.firewallArn).toBe(firewall.firewallArn); |
| 110 | |
| 111 | // Out-of-band verification via distilled. |
| 112 | const observed = yield* nfw.describeFirewall({ |
| 113 | FirewallName: firewall.firewallName, |
| 114 | }); |
| 115 | expect(observed.FirewallStatus?.Status).toBe("READY"); |
| 116 | expect(observed.Firewall?.FirewallPolicyArn).toBe(policy.firewallPolicyArn); |
| 117 | |
| 118 | const observedLogging = yield* nfw.describeLoggingConfiguration({ |
| 119 | FirewallArn: firewall.firewallArn, |
| 120 | }); |
| 121 | expect(observedLogging.LoggingConfiguration?.LogDestinationConfigs?.[0]?.LogType).toBe( |
| 122 | "FLOW", |
| 123 | ); |
| 124 | |
| 125 | // Destroy immediately (delete waits for endpoint deprovisioning) and |
| 126 | // verify the firewall is gone out-of-band. |
| 127 | yield* stack.destroy(); |
| 128 | yield* assertFirewallGone(firewall.firewallName); |
| 129 | }), |
| 130 | // firewall create (~10 min) + delete (~10 min), one test. |
| 131 | { |
| 132 | tags: [ |
| 133 | "provider:aws", |
| 134 | "provider:aws:ec2", |
| 135 | "provider:aws:logs", |
| 136 | "provider:aws:networkfirewall", |
| 137 | "live", |
| 138 | ], |
| 139 | timeout: 2_400_000, |
| 140 | }, |
| 141 | ); |