The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as ec2 from "@distilled.cloud/aws/ec2"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | |
| 4 | /** |
| 5 | * The identifiers of the account's default VPC and its intrinsic furniture, |
| 6 | * resolved once per `list()` call so census/nuke never enumerate what AWS |
| 7 | * itself provisions (and what `test/AWS/DefaultVpc.ts` deliberately |
| 8 | * recreates as standing infrastructure). |
| 9 | * |
| 10 | * All fields are `undefined` when the account has no default VPC — callers |
| 11 | * must then exclude nothing that depends on these ids. |
| 12 | * |
| 13 | * Internal scaffolding — NOT exported from the EC2 service `index.ts`. |
| 14 | */ |
| 15 | export interface DefaultVpcScope { |
| 16 | /** The default VPC's id, if the account has one. */ |
| 17 | readonly vpcId: string | undefined; |
| 18 | /** The DhcpOptions set the default VPC references (the account default). */ |
| 19 | readonly dhcpOptionsId: string | undefined; |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * Resolve the default VPC (if any) for the ambient account/region. |
| 24 | */ |
| 25 | export const getDefaultVpcScope = ec2 |
| 26 | .describeVpcs({ Filters: [{ Name: "isDefault", Values: ["true"] }] }) |
| 27 | .pipe( |
| 28 | Effect.map((r): DefaultVpcScope => { |
| 29 | const vpc = (r.Vpcs ?? []).find((v) => v.IsDefault); |
| 30 | return { vpcId: vpc?.VpcId, dhcpOptionsId: vpc?.DhcpOptionsId }; |
| 31 | }), |
| 32 | ); |
| 33 | |
| 34 | /** |
| 35 | * The GroupId of the default VPC's "default" security group, or `undefined` |
| 36 | * when the account has no default VPC. Rules on this group are AWS-provisioned |
| 37 | * furniture; rules on user-created groups inside the default VPC are not. |
| 38 | */ |
| 39 | export const getDefaultVpcDefaultSecurityGroupId = (vpcId: string | undefined) => |
| 40 | vpcId === undefined |
| 41 | ? Effect.undefined |
| 42 | : ec2 |
| 43 | .describeSecurityGroups({ |
| 44 | Filters: [ |
| 45 | { Name: "vpc-id", Values: [vpcId] }, |
| 46 | { Name: "group-name", Values: ["default"] }, |
| 47 | ], |
| 48 | }) |
| 49 | .pipe(Effect.map((r) => r.SecurityGroups?.[0]?.GroupId)); |
| 50 | |
| 51 | /** |
| 52 | * The GroupIds of the EMR-managed default security groups |
| 53 | * (`ElasticMapReduce-master` / `ElasticMapReduce-slave`, tagged |
| 54 | * `for-use-with-amazon-emr-managed-policies`) that Amazon EMR auto-creates in |
| 55 | * the default VPC the first time a cluster launches there without explicit |
| 56 | * security groups. EMR owns and reuses them for every later cluster in the |
| 57 | * VPC (like Glue's shared `/aws-glue/*` log groups), and their mutual |
| 58 | * references make them undeletable piecemeal — they are default-VPC |
| 59 | * furniture, not leaks. Empty when the account has no default VPC. |
| 60 | */ |
| 61 | export const getDefaultVpcEmrManagedSecurityGroupIds = (vpcId: string | undefined) => |
| 62 | vpcId === undefined |
| 63 | ? Effect.succeed(new Set<string>()) |
| 64 | : ec2 |
| 65 | .describeSecurityGroups({ |
| 66 | Filters: [ |
| 67 | { Name: "vpc-id", Values: [vpcId] }, |
| 68 | { Name: "tag-key", Values: ["for-use-with-amazon-emr-managed-policies"] }, |
| 69 | ], |
| 70 | }) |
| 71 | .pipe( |
| 72 | Effect.map( |
| 73 | (r) => |
| 74 | new Set( |
| 75 | (r.SecurityGroups ?? []).flatMap((sg) => |
| 76 | sg.GroupId !== undefined && sg.GroupName?.startsWith("ElasticMapReduce-") |
| 77 | ? [sg.GroupId] |
| 78 | : [], |
| 79 | ), |
| 80 | ), |
| 81 | ), |
| 82 | ); |