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 { VpcOrigin } from "@alchemy.run/aws/CloudFront"; |
| 3 | import { Network } from "@alchemy.run/aws/EC2/Network"; |
| 4 | import { SecurityGroup } from "@alchemy.run/aws/EC2/SecurityGroup"; |
| 5 | import { LoadBalancer } from "@alchemy.run/aws/ELBv2/LoadBalancer"; |
| 6 | import { describe, expect } from "@alchemy.run/test"; |
| 7 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 8 | import * as cloudfront from "@distilled.cloud/aws/cloudfront"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | |
| 13 | const { test } = Test.make({ providers: AWS.providers() }); |
| 14 | |
| 15 | // The full lifecycle provisions a real internal ALB and a CloudFront VPC |
| 16 | // origin. CloudFront VPC-origin deploy + delete is very slow (global |
| 17 | // propagation, ~20-25 min for create alone), so the whole create -> Deployed |
| 18 | // -> delete -> gone cycle runs ~35-40 min end to end. It is gated behind an |
| 19 | // env var and given a generous timeout; the probe + list below run cheaply and |
| 20 | // cover the wiring + typed-error surface in CI. |
| 21 | const runLifecycle = process.env.CLOUDFRONT_TEST_VPC_ORIGIN === "1"; |
| 22 | |
| 23 | describe( |
| 24 | "AWS.CloudFront.VpcOrigin", |
| 25 | { tags: ["provider:aws", "provider:aws:cloudfront", "live"] }, |
| 26 | () => { |
| 27 | // Fast probe (no deploy): creating a VPC origin against a bogus ARN must |
| 28 | // surface a typed `InvalidArgument` (or `EntityNotFound`), proving the error |
| 29 | // typing for the create op without provisioning any infrastructure. |
| 30 | test.provider("createVpcOrigin rejects a bogus ARN with a typed error", () => |
| 31 | Effect.gen(function* () { |
| 32 | const result = yield* cloudfront |
| 33 | .createVpcOrigin({ |
| 34 | VpcOriginEndpointConfig: { |
| 35 | Name: "alchemy-vpc-origin-probe", |
| 36 | Arn: "arn:aws:elasticloadbalancing:us-east-1:000000000000:loadbalancer/app/does-not-exist/0000000000000000", |
| 37 | HTTPPort: 80, |
| 38 | HTTPSPort: 443, |
| 39 | OriginProtocolPolicy: "https-only", |
| 40 | }, |
| 41 | }) |
| 42 | .pipe(Effect.flip); |
| 43 | |
| 44 | expect(["InvalidArgument", "EntityNotFound", "AccessDenied"]).toContain(result._tag); |
| 45 | }), |
| 46 | ); |
| 47 | |
| 48 | test.provider.skipIf(!runLifecycle)( |
| 49 | "create, update, and delete a VPC origin for an internal ALB", |
| 50 | (stack) => |
| 51 | Effect.gen(function* () { |
| 52 | yield* stack.destroy(); |
| 53 | |
| 54 | // CloudFront VPC origins require the target's VPC to have an internet |
| 55 | // gateway attached. `Network` provisions a production-shaped VPC (VPC + |
| 56 | // attached IGW + public/private subnets across 2 AZs + route tables), so |
| 57 | // the networking + ALB is deployed in a first phase, then the VPC origin |
| 58 | // in a second — the IGW must be attached before `createVpcOrigin` runs. |
| 59 | const network = Effect.gen(function* () { |
| 60 | const net = yield* Network("VpcOriginNet", { cidrBlock: "10.40.0.0/16" }); |
| 61 | const sg = yield* SecurityGroup("VpcOriginSg", { |
| 62 | vpcId: net.vpcId, |
| 63 | description: "alchemy vpc origin alb", |
| 64 | ingress: [{ ipProtocol: "tcp", fromPort: 80, toPort: 80, cidrIpv4: "0.0.0.0/0" }], |
| 65 | }); |
| 66 | const alb = yield* LoadBalancer("VpcOriginAlb", { |
| 67 | scheme: "internal", |
| 68 | type: "application", |
| 69 | subnets: net.publicSubnetIds, |
| 70 | securityGroups: [sg.groupId], |
| 71 | }); |
| 72 | return { albArn: alb.loadBalancerArn }; |
| 73 | }); |
| 74 | |
| 75 | // Phase 1: networking (incl. attached IGW) + ALB. |
| 76 | yield* stack.deploy(network); |
| 77 | |
| 78 | // Phase 2: the VPC origin, now that the IGW is attached. |
| 79 | const deployed = yield* stack.deploy( |
| 80 | Effect.gen(function* () { |
| 81 | const { albArn } = yield* network; |
| 82 | const vpcOrigin = yield* VpcOrigin("AppVpcOrigin", { |
| 83 | arn: albArn, |
| 84 | httpPort: 80, |
| 85 | originProtocolPolicy: "http-only", |
| 86 | }); |
| 87 | return { vpcOrigin }; |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | // Out-of-band: confirm it deployed. |
| 92 | const got = yield* cloudfront.getVpcOrigin({ Id: deployed.vpcOrigin.vpcOriginId }); |
| 93 | expect(got.VpcOrigin?.Status).toEqual("Deployed"); |
| 94 | expect(got.VpcOrigin?.VpcOriginEndpointConfig.HTTPPort).toEqual(80); |
| 95 | expect(got.VpcOrigin?.VpcOriginEndpointConfig.OriginProtocolPolicy).toEqual("http-only"); |
| 96 | |
| 97 | yield* stack.destroy(); |
| 98 | yield* assertVpcOriginDeleted(deployed.vpcOrigin.vpcOriginId); |
| 99 | }), |
| 100 | // CloudFront VPC origin deploy + delete each take many minutes (global |
| 101 | // propagation), on top of the ALB/VPC provisioning and teardown — budget |
| 102 | // 45 min for the full create -> Deployed -> delete -> gone cycle. |
| 103 | { tags: ["provider:aws:ec2", "provider:aws:elbv2"], timeout: 2_700_000 }, |
| 104 | ); |
| 105 | |
| 106 | test.provider.skipIf(!runLifecycle)("list enumerates account VPC origins", () => |
| 107 | Effect.gen(function* () { |
| 108 | const provider = yield* Provider.findProvider(VpcOrigin); |
| 109 | const all = yield* provider.list(); |
| 110 | expect(Array.isArray(all)).toBe(true); |
| 111 | for (const item of all) { |
| 112 | expect(item.vpcOriginId).toBeDefined(); |
| 113 | expect(item.vpcOriginArn).toBeDefined(); |
| 114 | } |
| 115 | }), |
| 116 | ); |
| 117 | }, |
| 118 | ); |
| 119 | |
| 120 | const assertVpcOriginDeleted = (id: string) => |
| 121 | cloudfront.getVpcOrigin({ Id: id }).pipe( |
| 122 | Effect.flatMap((result) => |
| 123 | result.VpcOrigin ? Effect.fail(new Error("VpcOriginStillExists")) : Effect.void, |
| 124 | ), |
| 125 | Effect.catchTag("EntityNotFound", () => Effect.void), |
| 126 | Effect.retry({ |
| 127 | while: (error) => error instanceof Error && error.message === "VpcOriginStillExists", |
| 128 | schedule: Schedule.max([Schedule.fixed("10 seconds"), Schedule.recurs(30)]), |
| 129 | }), |
| 130 | ); |