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 type { Input } from "alchemy/Input"; |
| 2 | import { moduleExtension } from "alchemy/Util/Node"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import type { SecurityGroupId } from "../EC2/SecurityGroup.ts"; |
| 5 | import type { SubnetId } from "../EC2/Subnet.ts"; |
| 6 | import { Function as LambdaFunction } from "../Lambda/Function.ts"; |
| 7 | |
| 8 | export interface MigrationRunnerProps { |
| 9 | /** |
| 10 | * Subnets the runner attaches to. They need a route to the database; no |
| 11 | * internet or NAT access is required. |
| 12 | */ |
| 13 | subnetIds: Input<SubnetId[]>; |
| 14 | /** |
| 15 | * Security groups for the runner. The database's security group must |
| 16 | * allow ingress on its port from one of them. |
| 17 | */ |
| 18 | securityGroupIds: Input<SecurityGroupId[]>; |
| 19 | /** |
| 20 | * Longest single SQL operation (one query, or one migration's |
| 21 | * transaction) the runner allows before Lambda stops it. |
| 22 | * @default 15 minutes |
| 23 | */ |
| 24 | timeout?: Duration.Duration; |
| 25 | /** |
| 26 | * Memory for the runner function, in MB. |
| 27 | * @default 512 |
| 28 | */ |
| 29 | memorySize?: number; |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * A Lambda function inside a VPC that applies SQL migrations to RDS |
| 34 | * databases the deploying machine can't reach. Pass it as |
| 35 | * `migrationsTransport` on a `DBInstance` or `Aurora`: during deploy, |
| 36 | * Alchemy invokes the runner once per SQL operation, and the runner |
| 37 | * connects to the database from inside the VPC. |
| 38 | * |
| 39 | * The runner receives the database credentials in its invocation payload, |
| 40 | * so it needs no access to Secrets Manager and no outbound internet. It |
| 41 | * supports PostgreSQL, MySQL and MariaDB engines, including Aurora. The |
| 42 | * `pg` and `mysql2` drivers are installed into the function's artifact at |
| 43 | * deploy time, which requires `npm` on the deploying machine. |
| 44 | * |
| 45 | * ### Migrating a private database |
| 46 | * **Example:** Standalone PostgreSQL in private subnets |
| 47 | * ```typescript |
| 48 | * const runnerSecurityGroup = yield* AWS.EC2.SecurityGroup("RunnerSg", { |
| 49 | * vpcId: network.vpcId, |
| 50 | * }); |
| 51 | * const databaseSecurityGroup = yield* AWS.EC2.SecurityGroup("DatabaseSg", { |
| 52 | * vpcId: network.vpcId, |
| 53 | * ingress: [ |
| 54 | * { |
| 55 | * ipProtocol: "tcp", |
| 56 | * fromPort: 5432, |
| 57 | * toPort: 5432, |
| 58 | * referencedGroupId: runnerSecurityGroup.groupId, |
| 59 | * }, |
| 60 | * ], |
| 61 | * }); |
| 62 | * const runner = yield* AWS.RDS.MigrationRunner("MigrationRunner", { |
| 63 | * subnetIds: network.privateSubnetIds, |
| 64 | * securityGroupIds: [runnerSecurityGroup.groupId], |
| 65 | * }); |
| 66 | * const database = yield* AWS.RDS.DBInstance("Database", { |
| 67 | * engine: "postgres", |
| 68 | * dbInstanceClass: "db.t4g.micro", |
| 69 | * allocatedStorage: 20, |
| 70 | * dbName: "app", |
| 71 | * masterUsername: "app", |
| 72 | * manageMasterUserPassword: true, |
| 73 | * dbSubnetGroupName: subnetGroup.dbSubnetGroupName, |
| 74 | * vpcSecurityGroupIds: [databaseSecurityGroup.groupId], |
| 75 | * migrations: "./migrations", |
| 76 | * migrationsTransport: runner, |
| 77 | * }); |
| 78 | * ``` |
| 79 | * |
| 80 | * @resource |
| 81 | */ |
| 82 | export const MigrationRunner = (id: string, props: MigrationRunnerProps) => |
| 83 | LambdaFunction(id, { |
| 84 | main: new URL(`./MigrationRunnerHandler${moduleExtension(import.meta.url)}`, import.meta.url) |
| 85 | .href, |
| 86 | handler: "handler", |
| 87 | runtime: "nodejs24.x", |
| 88 | vpc: { |
| 89 | subnetIds: props.subnetIds, |
| 90 | securityGroupIds: props.securityGroupIds, |
| 91 | }, |
| 92 | timeout: props.timeout ?? Duration.minutes(15), |
| 93 | memorySize: props.memorySize ?? 512, |
| 94 | // Exact pins: the runner's drivers must not depend on the deploying |
| 95 | // project's lockfile, which usually lists neither package. |
| 96 | build: { install: { pg: "8.23.1", mysql2: "3.24.5" } }, |
| 97 | }); |