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 { FlowLog, Vpc } from "@alchemy.run/aws/EC2"; |
| 3 | import { Role } from "@alchemy.run/aws/IAM/Role"; |
| 4 | import { LogGroup } from "@alchemy.run/aws/Logs/LogGroup"; |
| 5 | import { expect } from "@alchemy.run/test"; |
| 6 | import * as EC2 from "@distilled.cloud/aws/ec2"; |
| 7 | import * as Provider from "alchemy/Provider"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import { MinimumLogLevel } from "effect/References"; |
| 11 | import * as Schedule from "effect/Schedule"; |
| 12 | import * as Test from "./VpcTest.ts"; |
| 13 | |
| 14 | const { test } = Test.make({ providers: AWS.providers() }); |
| 15 | |
| 16 | const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info"); |
| 17 | |
| 18 | class FlowLogStillExists extends Data.TaggedError("FlowLogStillExists") {} |
| 19 | |
| 20 | const assertDeleted = Effect.fn(function* (flowLogId: string) { |
| 21 | yield* EC2.describeFlowLogs({ FlowLogIds: [flowLogId] }).pipe( |
| 22 | Effect.flatMap((r) => |
| 23 | (r.FlowLogs?.length ?? 0) === 0 ? Effect.void : Effect.fail(new FlowLogStillExists()), |
| 24 | ), |
| 25 | Effect.retry({ |
| 26 | while: (e) => e instanceof FlowLogStillExists, |
| 27 | schedule: Schedule.max([Schedule.exponential(300), Schedule.recurs(8)]), |
| 28 | }), |
| 29 | Effect.catchTag("InvalidFlowLogId.NotFound", () => Effect.void), |
| 30 | ); |
| 31 | }); |
| 32 | |
| 33 | const flowLogStack = (tags?: Record<string, string>) => |
| 34 | Effect.gen(function* () { |
| 35 | const vpc = yield* Vpc("FlowLogVpc", { cidrBlock: "10.50.0.0/16" }); |
| 36 | const logGroup = yield* LogGroup("FlowLogGroup", {}); |
| 37 | const role = yield* Role("FlowLogRole", { |
| 38 | assumeRolePolicyDocument: { |
| 39 | Version: "2012-10-17", |
| 40 | Statement: [ |
| 41 | { |
| 42 | Effect: "Allow", |
| 43 | Principal: { Service: "vpc-flow-logs.amazonaws.com" }, |
| 44 | Action: ["sts:AssumeRole"], |
| 45 | }, |
| 46 | ], |
| 47 | }, |
| 48 | inlinePolicies: { |
| 49 | deliver: { |
| 50 | Version: "2012-10-17", |
| 51 | Statement: [ |
| 52 | { |
| 53 | Effect: "Allow", |
| 54 | Action: ["logs:CreateLogStream", "logs:PutLogEvents", "logs:DescribeLogStreams"], |
| 55 | Resource: "*", |
| 56 | }, |
| 57 | ], |
| 58 | }, |
| 59 | }, |
| 60 | }); |
| 61 | const flowLog = yield* FlowLog("VpcFlowLog", { |
| 62 | resourceType: "VPC", |
| 63 | resourceId: vpc.vpcId, |
| 64 | trafficType: "ALL", |
| 65 | logGroupName: logGroup.logGroupName, |
| 66 | deliverLogsPermissionArn: role.roleArn, |
| 67 | tags, |
| 68 | }); |
| 69 | return { vpc, flowLog }; |
| 70 | }); |
| 71 | |
| 72 | test.provider( |
| 73 | "create, update tags, delete VPC flow log to CloudWatch Logs", |
| 74 | (stack) => |
| 75 | Effect.gen(function* () { |
| 76 | yield* stack.destroy(); |
| 77 | |
| 78 | const { vpc, flowLog } = yield* stack.deploy(flowLogStack()); |
| 79 | |
| 80 | expect(flowLog.flowLogId).toMatch(/^fl-/); |
| 81 | expect(flowLog.resourceId).toEqual(vpc.vpcId); |
| 82 | expect(flowLog.trafficType).toEqual("ALL"); |
| 83 | expect(flowLog.logDestinationType).toEqual("cloud-watch-logs"); |
| 84 | |
| 85 | // Verify out-of-band. |
| 86 | const described = yield* EC2.describeFlowLogs({ |
| 87 | FlowLogIds: [flowLog.flowLogId], |
| 88 | }); |
| 89 | const fl = described.FlowLogs?.[0]; |
| 90 | expect(fl?.FlowLogId).toEqual(flowLog.flowLogId); |
| 91 | expect(fl?.ResourceId).toEqual(vpc.vpcId); |
| 92 | expect(fl?.TrafficType).toEqual("ALL"); |
| 93 | expect(fl?.LogDestinationType).toEqual("cloud-watch-logs"); |
| 94 | |
| 95 | // Update tags in place (flow log id unchanged). |
| 96 | const { flowLog: updated } = yield* stack.deploy(flowLogStack({ env: "prod" })); |
| 97 | expect(updated.flowLogId).toEqual(flowLog.flowLogId); |
| 98 | |
| 99 | const tags = yield* EC2.describeTags({ |
| 100 | Filters: [ |
| 101 | { Name: "resource-id", Values: [flowLog.flowLogId] }, |
| 102 | { Name: "resource-type", Values: ["vpc-flow-log"] }, |
| 103 | ], |
| 104 | }); |
| 105 | expect(tags.Tags?.some((t) => t.Key === "env" && t.Value === "prod")).toBe(true); |
| 106 | |
| 107 | yield* stack.destroy(); |
| 108 | yield* assertDeleted(flowLog.flowLogId); |
| 109 | }).pipe(logLevel), |
| 110 | { |
| 111 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:iam", "provider:aws:logs", "live"], |
| 112 | timeout: 240_000, |
| 113 | }, |
| 114 | ); |
| 115 | |
| 116 | test.provider( |
| 117 | "list enumerates the deployed flow log", |
| 118 | (stack) => |
| 119 | Effect.gen(function* () { |
| 120 | yield* stack.destroy(); |
| 121 | |
| 122 | const { flowLog } = yield* stack.deploy(flowLogStack()); |
| 123 | |
| 124 | const provider = yield* Provider.findProvider(FlowLog); |
| 125 | const all = yield* provider.list(); |
| 126 | expect(all.some((x) => x.flowLogId === flowLog.flowLogId)).toBe(true); |
| 127 | |
| 128 | yield* stack.destroy(); |
| 129 | yield* assertDeleted(flowLog.flowLogId); |
| 130 | }).pipe(logLevel), |
| 131 | { |
| 132 | tags: ["provider:aws", "provider:aws:ec2", "provider:aws:iam", "provider:aws:logs", "live"], |
| 133 | timeout: 240_000, |
| 134 | }, |
| 135 | ); |