The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import { SecurityGroup, Subnet, Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import { Role } from "@alchemy.run/aws/IAM"; |
| 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 lambdacore from "@distilled.cloud/aws/lambda-core"; |
| 8 | import * as Output from "alchemy/Output"; |
| 9 | import * as Provider from "alchemy/Provider"; |
| 10 | import * as Effect from "effect/Effect"; |
| 11 | |
| 12 | const { test } = Test.make({ providers: AWS.providers() }); |
| 13 | |
| 14 | // Lambda MicroVM network connectors are a preview feature. Creation provisions |
| 15 | // ENIs in the VPC and can take several minutes to reach ACTIVE (we've observed |
| 16 | // the full lifecycle blowing a 25-minute budget and leaking its VPC — which |
| 17 | // then starves the account's 5-VPC quota and fails unrelated EC2 suites), and |
| 18 | // the API requires an account that is onboarded to the preview. Gate the live |
| 19 | // lifecycle behind LAMBDA_TEST_NETWORK_CONNECTOR=1 so an entitled account runs |
| 20 | // it unchanged. |
| 21 | test.provider.skipIf(!process.env.LAMBDA_TEST_NETWORK_CONNECTOR)( |
| 22 | "create, update, list, delete network connector", |
| 23 | (stack) => |
| 24 | Effect.gen(function* () { |
| 25 | yield* stack.destroy(); |
| 26 | |
| 27 | const azResult = yield* EC2.describeAvailabilityZones({}); |
| 28 | const az = azResult.AvailabilityZones?.find((z) => z.State === "available")?.ZoneName!; |
| 29 | |
| 30 | const infra = (networkProtocol: lambdacore.NetworkProtocol) => |
| 31 | Effect.gen(function* () { |
| 32 | // The DualStack update step requires the subnet to carry an IPv6 |
| 33 | // CIDR, so provision the VPC with an Amazon-provided /56 and carve |
| 34 | // the subnet the first /64 out of it. |
| 35 | const vpc = yield* Vpc("ConnectorVpc", { |
| 36 | cidrBlock: "10.0.0.0/16", |
| 37 | amazonProvidedIpv6CidrBlock: true, |
| 38 | }); |
| 39 | const subnet = yield* Subnet("ConnectorSubnet", { |
| 40 | vpcId: vpc.vpcId, |
| 41 | cidrBlock: "10.0.1.0/24", |
| 42 | availabilityZone: az, |
| 43 | ipv6CidrBlock: vpc.ipv6CidrBlockAssociationSet.pipe( |
| 44 | Output.map((set) => set![0]!.ipv6CidrBlock.replace("/56", "/64")), |
| 45 | ), |
| 46 | }); |
| 47 | const sg = yield* SecurityGroup("ConnectorSg", { |
| 48 | vpcId: vpc.vpcId, |
| 49 | description: "MicroVM egress", |
| 50 | }); |
| 51 | const role = yield* Role("ConnectorOperator", { |
| 52 | assumeRolePolicyDocument: { |
| 53 | Version: "2012-10-17", |
| 54 | Statement: [ |
| 55 | { |
| 56 | Effect: "Allow", |
| 57 | Principal: { Service: "lambda.amazonaws.com" }, |
| 58 | Action: ["sts:AssumeRole"], |
| 59 | }, |
| 60 | ], |
| 61 | }, |
| 62 | // The connector service assumes this role to manage ENIs in the VPC. |
| 63 | inlinePolicies: { |
| 64 | "eni-management": { |
| 65 | Version: "2012-10-17", |
| 66 | Statement: [ |
| 67 | { |
| 68 | Effect: "Allow", |
| 69 | Action: [ |
| 70 | "ec2:CreateNetworkInterface", |
| 71 | "ec2:DescribeNetworkInterfaces", |
| 72 | "ec2:DeleteNetworkInterface", |
| 73 | "ec2:DescribeSubnets", |
| 74 | "ec2:DescribeSecurityGroups", |
| 75 | "ec2:DescribeVpcs", |
| 76 | "ec2:AssignPrivateIpAddresses", |
| 77 | "ec2:UnassignPrivateIpAddresses", |
| 78 | "ec2:CreateTags", |
| 79 | ], |
| 80 | Resource: "*", |
| 81 | }, |
| 82 | ], |
| 83 | }, |
| 84 | }, |
| 85 | }); |
| 86 | const connector = yield* AWS.Lambda.NetworkConnector("Connector", { |
| 87 | subnetIds: [subnet.subnetId], |
| 88 | securityGroupIds: [sg.groupId], |
| 89 | networkProtocol, |
| 90 | operatorRole: role.roleArn, |
| 91 | }); |
| 92 | return { connector }; |
| 93 | }); |
| 94 | |
| 95 | // --- create --- |
| 96 | const created = yield* stack.deploy(infra("IPv4")); |
| 97 | expect(created.connector.state).toBe("ACTIVE"); |
| 98 | expect(created.connector.networkConnectorArn).toContain("network-connector"); |
| 99 | |
| 100 | const fetched = yield* lambdacore.getNetworkConnector({ |
| 101 | Identifier: created.connector.networkConnectorId, |
| 102 | }); |
| 103 | expect(fetched.State).toBe("ACTIVE"); |
| 104 | |
| 105 | // --- update (network protocol) --- |
| 106 | const updated = yield* stack.deploy(infra("DualStack")); |
| 107 | expect(updated.connector.networkConnectorArn).toBe(created.connector.networkConnectorArn); |
| 108 | expect(updated.connector.networkProtocol).toBe("DualStack"); |
| 109 | |
| 110 | // --- list --- |
| 111 | const provider = yield* Provider.findProvider(AWS.Lambda.NetworkConnector); |
| 112 | const all = yield* provider.list(); |
| 113 | expect(all.some((c) => c.networkConnectorId === created.connector.networkConnectorId)).toBe( |
| 114 | true, |
| 115 | ); |
| 116 | |
| 117 | // --- delete --- |
| 118 | yield* stack.destroy(); |
| 119 | const afterDestroy = yield* lambdacore |
| 120 | .getNetworkConnector({ |
| 121 | Identifier: created.connector.networkConnectorId, |
| 122 | }) |
| 123 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(undefined))); |
| 124 | expect(afterDestroy).toBeUndefined(); |
| 125 | }).pipe( |
| 126 | Effect.tap(() => stack.destroy()), |
| 127 | Effect.onError(() => stack.destroy().pipe(Effect.ignore)), |
| 128 | ), |
| 129 | { |
| 130 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:iam", "provider:aws:lambda", "live"], |
| 131 | timeout: 1_500_000, |
| 132 | }, |
| 133 | ); |