@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 2,909 lines 114 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import { Network } from "@alchemy.run/aws/EC2/Network";
3 import { SecurityGroup } from "@alchemy.run/aws/EC2/SecurityGroup";
4 import { AWSEnvironment } from "@alchemy.run/aws/Environment";
5 import { normalizePolicyDocument, type PolicyDocument } from "@alchemy.run/aws/IAM/Policy";
6 import { Key } from "@alchemy.run/aws/KMS/Key";
7 import { DBCluster, DBInstance, MigrationRunner, type DBInstanceProps } from "@alchemy.run/aws/RDS";
8 import { Aurora, type AuroraProps } from "@alchemy.run/aws/RDS/Aurora";
9 import { DBParameterGroup } from "@alchemy.run/aws/RDS/DBParameterGroup";
10 import { DBSubnetGroup } from "@alchemy.run/aws/RDS/DBSubnetGroup";
11 import { describe, expect } from "@alchemy.run/test";
12 import * as Test from "@alchemy.run/test/Alchemy";
13 import * as ec2 from "@distilled.cloud/aws/ec2";
14 import * as Lambda from "@distilled.cloud/aws/lambda";
15 import * as rds from "@distilled.cloud/aws/rds";
16 import * as rdsdata from "@distilled.cloud/aws/rds-data";
17 import * as secretsmanager from "@distilled.cloud/aws/secrets-manager";
18 import * as PgClient from "@effect/sql-pg/PgClient";
19 import * as Drift from "alchemy/Drift";
20 import * as Output from "alchemy/Output";
21 import * as Provider from "alchemy/Provider";
22 import { State } from "alchemy/State";
23 import * as Cause from "effect/Cause";
24 import * as Duration from "effect/Duration";
25 import * as Effect from "effect/Effect";
26 import * as Exit from "effect/Exit";
27 import * as FileSystem from "effect/FileSystem";
28 import * as HttpClient from "effect/http/HttpClient";
29 import * as HttpClientResponse from "effect/http/HttpClientResponse";
30 import * as Path from "effect/Path";
31 import * as Redacted from "effect/Redacted";
32 import * as Result from "effect/Result";
33 import * as Schedule from "effect/Schedule";
34 import * as Stream from "effect/Stream";
35 import { driftOf } from "../drift.ts";
36
37 const { test } = Test.make({ providers: AWS.providers() });
38
39 // Gated lifecycle budget, measured on a live run (us-west-2): a postgres
40 // db.t3.micro creates in 283–343s, a reboot-applied modify takes 137–199s and
41 // a delete 90–210s; an Aurora serverless-v2 writer creates in ~380s and
42 // deletes in ~315s. A create + up to three applied modifies + delete fits in
43 // 30 minutes; cases that apply four or more modifies (or measured >25 min)
44 // get 45.
45 const INSTANCE_TIMEOUT = 1_800_000;
46 const LONG_INSTANCE_TIMEOUT = 2_700_000;
47
48 // Every gated case provisions its own Network + instance and spends 10–25
49 // minutes doing it, so running them one after another takes ~7 hours. They
50 // are registered into lanes instead: lanes run concurrently, the cases within
51 // a lane run sequentially. Nine lanes bound the VPCs and instances alive at
52 // once (well under the account's VPC quota of 20) while the whole file
53 // finishes in about an hour. Cases cannot share instances: RDS allows one
54 // storage modification per instance every six hours, and the others assert
55 // exact request sequences, adoption, or drift on an instance they own.
56 type LifecycleCase = Parameters<ReturnType<typeof test.provider.skipIf>>;
57 const LANES = 9;
58 const lanes: Array<Array<{ enabled: boolean; args: LifecycleCase }>> = Array.from(
59 { length: LANES },
60 () => [],
61 );
62 const lifecycle =
63 (lane: number, gate: string | undefined) =>
64 (...args: LifecycleCase) => {
65 lanes[lane - 1]!.push({ enabled: !!gate, args });
66 };
67
68 // Render a deploy failure (whatever engine wrapper it arrives in) to a string
69 // we can assert AWS's parameter-validation message against.
70 const renderFailure = (attempt: Result.Result<unknown, unknown>): string => {
71 if (!Result.isFailure(attempt)) {
72 return "";
73 }
74 const failure = attempt.failure;
75 const json = (() => {
76 try {
77 return JSON.stringify(failure);
78 } catch {
79 return "";
80 }
81 })();
82 return `${String(failure)} ${json}`;
83 };
84
85 // Live wire probes for this PR's Redacted/Duration prop conversions on
86 // DBInstance (the instance reconcile has its own conversion code, separate
87 // from DBCluster's). Both drive the full engine + provider `reconcile` path
88 // into a real `createDBInstance` call that AWS rejects at
89 // parameter-validation time — nothing is provisioned and the probe completes
90 // in seconds. Probe 1 proves `masterUserPassword: Redacted.Redacted<string>`
91 // serializes to the actual secret characters on the wire; probe 2 proves
92 // `backupRetentionPeriod: Duration.Input` ("60 days") arrives as integer days
93 // (rejected as > the 35-day maximum). These rejected requests do not verify
94 // the RDS_TEST_LIFECYCLE-gated storage lifecycle below.
95 test.provider(
96 "wire probe: Redacted password + Duration retention reach createDBInstance",
97 (stack) =>
98 Effect.gen(function* () {
99 yield* stack.destroy();
100
101 const network = Effect.gen(function* () {
102 const net = yield* Network("ProbeNet", { cidrBlock: "10.47.0.0/16" });
103 return yield* DBSubnetGroup("ProbeSubnetGroup", {
104 description: "RDS serialization probe network",
105 subnetIds: net.privateSubnetIds,
106 });
107 });
108 const badPassword = yield* Effect.result(
109 stack.deploy(
110 Effect.gen(function* () {
111 const subnetGroup = yield* network;
112 return yield* DBInstance("AuditProbeInstance", {
113 dbInstanceIdentifier: "alchemy-audit-probe-instance",
114 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
115 engine: "postgres",
116 dbInstanceClass: "db.t3.micro",
117 allocatedStorage: 20,
118 masterUsername: "alchemy",
119 // '@' and ' ' are forbidden password characters — AWS rejects
120 // the create before provisioning anything.
121 masterUserPassword: Redacted.make("bad@pass word1"),
122 backupRetentionPeriod: "3 days",
123 });
124 }),
125 ),
126 );
127 expect(Result.isFailure(badPassword)).toBe(true);
128 expect(renderFailure(badPassword)).toContain("InvalidParameterValue");
129 expect(renderFailure(badPassword)).toContain("MasterUserPassword");
130
131 const badRetention = yield* Effect.result(
132 stack.deploy(
133 Effect.gen(function* () {
134 const subnetGroup = yield* network;
135 return yield* DBInstance("AuditProbeInstance", {
136 dbInstanceIdentifier: "alchemy-audit-probe-instance",
137 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
138 engine: "postgres",
139 dbInstanceClass: "db.t3.micro",
140 allocatedStorage: 20,
141 masterUsername: "alchemy",
142 masterUserPassword: Redacted.make("ValidPassw0rd"),
143 // 60 days is above the 1-35 day API maximum — AWS can only
144 // reject it if the converted integer arrived on the wire.
145 backupRetentionPeriod: "60 days",
146 });
147 }),
148 ),
149 );
150 expect(Result.isFailure(badRetention)).toBe(true);
151 expect(renderFailure(badRetention)).toContain("InvalidParameterValue");
152 expect(renderFailure(badRetention)).toMatch(/retention/i);
153
154 const invalidFlags = yield* rds
155 .describeDBEngineVersions({
156 Engine: "postgres",
157 IncludeAll: true,
158 DefaultOnly: true,
159 })
160 .pipe(Effect.result);
161 expect(Result.isFailure(invalidFlags)).toBe(true);
162 if (Result.isFailure(invalidFlags)) {
163 expect(invalidFlags.failure._tag).toBe("InvalidParameterCombination");
164 }
165 const emptyGroups = yield* stack
166 .deploy(
167 Effect.gen(function* () {
168 const subnetGroup = yield* network;
169 return yield* DBInstance("AuditProbeInstance", {
170 dbInstanceIdentifier: "alchemy-audit-probe-instance",
171 engine: "postgres",
172 dbInstanceClass: "db.t3.micro",
173 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
174 vpcSecurityGroupIds: [],
175 masterUsername: "alchemy",
176 masterUserPassword: Redacted.make("bad@pass word1"),
177 });
178 }),
179 )
180 .pipe(Effect.result);
181 expect(Result.isFailure(emptyGroups)).toBe(true);
182 expect(renderFailure(emptyGroups)).toContain("InvalidDBInstanceAssociations");
183
184 yield* stack.destroy();
185 }),
186 {
187 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
188 timeout: 120_000,
189 },
190 );
191
192 // Default (read-only) path: an RDS instance takes many minutes to create and
193 // delete — far beyond the 240s test budget — so the canonical `list()` test
194 // here does NOT deploy. It resolves the provider via the typed
195 // `Provider.findProvider(DBInstance)` helper and calls `list()` directly,
196 // asserting it returns a well-typed `DBInstance["Attributes"][]`. On a fresh
197 // account this is typically empty; either way every element must conform to
198 // the exact `read` shape.
199 test.provider(
200 "list returns well-typed DB instance attributes",
201 () =>
202 Effect.gen(function* () {
203 const provider = yield* Provider.findProvider(DBInstance);
204 const all = yield* provider.list();
205
206 expect(Array.isArray(all)).toBe(true);
207
208 // Every element must match the exact `Attributes` shape `read` produces.
209 for (const instance of all) {
210 expect(typeof instance.dbInstanceIdentifier).toBe("string");
211 expect(typeof instance.dbInstanceArn).toBe("string");
212 expect(Array.isArray(instance.dbParameterGroupNames)).toBe(true);
213 expect(typeof instance.tags).toBe("object");
214 }
215 }),
216 { tags: ["provider:aws", "provider:aws:rds", "live"] },
217 );
218
219 // Full lifecycle is gated: provisioning an Aurora cluster + instance and then
220 // tearing it down takes many minutes, exceeding the 240s budget. Set
221 // AWS_TEST_RDS_DBINSTANCE=1 on an account that can afford the wait to run it.
222 // It deploys a serverless-v2 Aurora cluster + instance and asserts the
223 // instance appears in the exhaustively-paginated `list()` result.
224 lifecycle(3, process.env.AWS_TEST_RDS_DBINSTANCE)(
225 "list enumerates the deployed DB instance",
226 (stack) =>
227 Effect.gen(function* () {
228 yield* stack.destroy();
229
230 const { client, requests } = yield* observeInstanceRequests;
231 const program = (round: string) =>
232 Effect.gen(function* () {
233 const network = yield* Network("ListNet", {
234 cidrBlock: "10.43.0.0/16",
235 });
236 const subnetGroup = yield* DBSubnetGroup("ListSubnetGroup", {
237 description: "alchemy instance list lifecycle",
238 subnetIds: network.privateSubnetIds,
239 });
240 const clusterGroup = yield* SecurityGroup("ListClusterGroup", {
241 vpcId: network.vpcId,
242 description: "Aurora cluster group",
243 });
244 const ignoredInstanceGroup = yield* SecurityGroup("ListIgnoredInstanceGroup", {
245 vpcId: network.vpcId,
246 description: "Ignored instance group",
247 });
248 const cluster = yield* DBCluster("ListCluster", {
249 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
250 vpcSecurityGroupIds: [clusterGroup.groupId],
251 engine: "aurora-postgresql",
252 engineMode: "provisioned",
253 port: 5434,
254 serverlessV2ScalingConfiguration: {
255 MinCapacity: 0.5,
256 MaxCapacity: 1,
257 },
258 manageMasterUserPassword: true,
259 masterUsername: "alchemy",
260 });
261
262 return yield* DBInstance("ListInstance", {
263 dbClusterIdentifier: cluster.dbClusterIdentifier,
264 dbInstanceClass: "db.serverless",
265 engine: "aurora-postgresql",
266 port: 5435,
267 vpcSecurityGroupIds: [ignoredInstanceGroup.groupId],
268 tags: { round },
269 });
270 });
271 const instance = yield* stack
272 .deploy(program("created"))
273 .pipe(Effect.provideService(HttpClient.HttpClient, client));
274 expect(instance.endpointPort).toBe(5434);
275 const updated = yield* stack
276 .deploy(program("updated"))
277 .pipe(Effect.provideService(HttpClient.HttpClient, client));
278 expect(updated.endpointPort).toBe(5434);
279 // #2688: drift observes autoMinorVersionUpgrade.
280 const observedInstance = (yield* rds.describeDBInstances({
281 DBInstanceIdentifier: updated.dbInstanceIdentifier,
282 })).DBInstances![0]!;
283 expect((yield* driftOf(stack, "ListInstance"))?.attr?.autoMinorVersionUpgrade).toBe(
284 observedInstance.AutoMinorVersionUpgrade,
285 );
286 expect(requests.filter((request) => request.port !== null)).toEqual([]);
287 expect(requests.filter((request) => request.securityGroups.length > 0)).toEqual([]);
288 const cluster = (yield* rds.describeDBClusters({
289 DBClusterIdentifier: instance.dbClusterIdentifier!,
290 })).DBClusters![0]!;
291 expect([...updated.vpcSecurityGroupIds].sort()).toEqual(
292 cluster.VpcSecurityGroups!.map((group) => group.VpcSecurityGroupId!).sort(),
293 );
294 const family = (yield* rds.describeDBEngineVersions({
295 Engine: "aurora-postgresql",
296 EngineVersion: cluster.EngineVersion,
297 })).DBEngineVersions![0]!.DBParameterGroupFamily!;
298 expect(updated.dbParameterGroupNames).toEqual([`default.${family}`]);
299 expect(
300 requests.filter(
301 (request) => request.action === "ModifyDBInstance" && request.parameterGroup !== null,
302 ),
303 ).toEqual([]);
304 expect(requests.some((request) => request.action === "CreateDBInstance")).toBe(true);
305 yield* assertPort(instance.dbInstanceIdentifier, 5434);
306 expect((yield* stack.plan(program("updated"))).resources.ListInstance).toMatchObject({
307 action: "noop",
308 });
309
310 const provider = yield* Provider.findProvider(DBInstance);
311 const all = yield* provider.list();
312
313 expect(all.some((i) => i.dbInstanceIdentifier === instance.dbInstanceIdentifier)).toBe(true);
314
315 yield* stack.destroy();
316 }),
317 {
318 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
319 timeout: INSTANCE_TIMEOUT,
320 },
321 );
322
323 // RDS provisioning and storage optimization exceed the default test budget.
324 lifecycle(1, process.env.RDS_TEST_LIFECYCLE)(
325 "standalone instance: autoscaling defaults, drift, and allocation floor",
326 (stack) =>
327 Effect.gen(function* () {
328 yield* stack.destroy();
329
330 // The testing account has no default VPC/subnets, so provision a
331 // production-shaped network (VPC + subnets across 2 AZs) and a DB subnet
332 // group for the instance to live in.
333 const network = Effect.gen(function* () {
334 const net = yield* Network("RdsNet", { cidrBlock: "10.41.0.0/16" });
335 // No fixed name — let the engine generate a unique physical name so a
336 // leftover group from an interrupted run can't force a cross-VPC
337 // ModifyDBSubnetGroup ("new Subnets are not in the same Vpc").
338 const subnetGroup = yield* DBSubnetGroup("RdsSubnetGroup", {
339 description: "alchemy standalone instance lifecycle",
340 subnetIds: net.privateSubnetIds,
341 });
342 return { dbSubnetGroupName: subnetGroup.dbSubnetGroupName };
343 });
344
345 const program = (
346 allocatedStorage: number,
347 maxAllocatedStorage?: number,
348 backupRetentionPeriod: "1 day" | "3 days" = "1 day",
349 enablePerformanceInsights = false,
350 ) =>
351 Effect.gen(function* () {
352 const { dbSubnetGroupName } = yield* network;
353 return yield* DBInstance("StandaloneInstance", {
354 engine: "postgres",
355 dbInstanceClass: "db.t3.micro",
356 allocatedStorage,
357 ...(maxAllocatedStorage === undefined ? {} : { maxAllocatedStorage }),
358 storageType: "gp2",
359 masterUsername: "alchemy",
360 manageMasterUserPassword: true,
361 backupRetentionPeriod,
362 enablePerformanceInsights,
363 deletionProtection: false,
364 dbSubnetGroupName,
365 publiclyAccessible: false,
366 });
367 });
368 const created = yield* stack.deploy(program(20));
369 expect(created.allocatedStorage).toBe(20);
370 expect(created.storageType).toBe("gp2");
371 expect(created.backupRetentionPeriod).toBe(1);
372 expect([0, 20]).toContain(created.maxAllocatedStorage ?? 0);
373 const describe = rds.describeDBInstances({
374 DBInstanceIdentifier: created.dbInstanceIdentifier,
375 });
376 expect([0, 20]).toContain((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage ?? 0);
377
378 const enabled = yield* stack.deploy(program(20, 40));
379 expect(enabled.maxAllocatedStorage).toBe(40);
380 expect((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage).toBe(40);
381 // Grow beyond the old ceiling while disabling autoscaling in the same request.
382 const updated = yield* stack.deploy(program(50, undefined, "3 days", true));
383 expect(updated.dbInstanceArn).toBe(created.dbInstanceArn);
384 expect(updated.backupRetentionPeriod).toBe(3);
385 expect(updated.allocatedStorage).toBe(50);
386 expect([0, 50]).toContain(updated.maxAllocatedStorage ?? 0);
387 const grown = (yield* describe).DBInstances?.[0];
388 expect(grown?.AllocatedStorage).toBe(50);
389 expect([0, 50]).toContain(grown?.MaxAllocatedStorage ?? 0);
390 expect(grown?.PendingModifiedValues?.AllocatedStorage).toBeUndefined();
391
392 const explicit = program(20, 100, "3 days", true);
393 const reenabled = yield* stack.deploy(explicit);
394 expect(reenabled.allocatedStorage).toBe(50);
395 expect(reenabled.maxAllocatedStorage).toBe(100);
396 expect((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage).toBe(100);
397
398 const injectCeiling = Effect.fn(function* (ceiling: number) {
399 yield* rds.modifyDBInstance({
400 DBInstanceIdentifier: created.dbInstanceIdentifier,
401 MaxAllocatedStorage: ceiling,
402 ApplyImmediately: true,
403 });
404 const injected = yield* describe.pipe(
405 Effect.repeat({
406 schedule: Schedule.spaced("5 seconds"),
407 times: 8,
408 until: (response) => response.DBInstances?.[0]?.MaxAllocatedStorage === ceiling,
409 }),
410 );
411 expect(injected.DBInstances?.[0]?.MaxAllocatedStorage).toBe(ceiling);
412 });
413 yield* injectCeiling(120);
414 expect((yield* stack.plan(explicit)).resources.StandaloneInstance).toMatchObject({
415 action: "update",
416 });
417 const repairedExplicit = yield* stack.deploy(explicit);
418 expect(repairedExplicit.maxAllocatedStorage).toBe(100);
419 expect((yield* describe).DBInstances?.[0]?.MaxAllocatedStorage).toBe(100);
420
421 // Lowering the minimum cannot shrink capacity, but omission disables autoscaling.
422 const lowerMinimum = program(20);
423 expect((yield* stack.plan(lowerMinimum)).resources.StandaloneInstance).toMatchObject({
424 action: "update",
425 });
426 const preserved = yield* stack.deploy(lowerMinimum);
427 expect(preserved.dbInstanceArn).toBe(created.dbInstanceArn);
428 expect(preserved.allocatedStorage).toBe(50);
429 expect([0, 50]).toContain(preserved.maxAllocatedStorage ?? 0);
430 const observed = (yield* describe).DBInstances?.[0];
431 expect(observed?.AllocatedStorage).toBe(50);
432 expect([0, 50]).toContain(observed?.MaxAllocatedStorage ?? 0);
433 expect(observed?.PendingModifiedValues?.AllocatedStorage).toBeUndefined();
434
435 yield* injectCeiling(80);
436 const drift = yield* Drift.detect({
437 name: stack.name,
438 stage: stack.stage,
439 });
440 expect(drift.resources.StandaloneInstance?.action).toBe("drifted");
441 expect((yield* stack.plan(lowerMinimum)).resources.StandaloneInstance).toMatchObject({
442 action: "update",
443 });
444 const repaired = yield* stack.deploy(lowerMinimum);
445 expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
446 expect(repaired.allocatedStorage).toBe(50);
447 expect([0, 50]).toContain(repaired.maxAllocatedStorage ?? 0);
448 const settled = (yield* describe).DBInstances?.[0];
449 expect([0, 50]).toContain(settled?.MaxAllocatedStorage ?? 0);
450 expect(settled?.AllocatedStorage).toBe(50);
451 expect((yield* stack.plan(lowerMinimum)).resources.StandaloneInstance).toMatchObject({
452 action: "noop",
453 });
454
455 const disabled = program(20, 0);
456 const explicitZero = yield* stack.deploy(disabled);
457 expect([0, 50]).toContain(explicitZero.maxAllocatedStorage ?? 0);
458 expect(explicitZero.allocatedStorage).toBe(50);
459 expect((yield* stack.plan(disabled)).resources.StandaloneInstance).toMatchObject({
460 action: "noop",
461 });
462
463 yield* stack.destroy();
464 const gone = yield* describe.pipe(
465 Effect.as(false),
466 Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(true)),
467 Effect.repeat({
468 schedule: Schedule.spaced("5 seconds"),
469 times: 8,
470 until: (absent) => absent,
471 }),
472 );
473 expect(gone).toBe(true);
474 }),
475 {
476 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
477 timeout: LONG_INSTANCE_TIMEOUT,
478 },
479 );
480
481 type StorageProps = Pick<
482 DBInstanceProps,
483 "allocatedStorage" | "storageType" | "iops" | "storageThroughput" | "maxAllocatedStorage"
484 >;
485
486 type StorageState = readonly [
487 allocatedStorage: number,
488 storageType: string,
489 iops: number,
490 storageThroughput: number,
491 ];
492
493 const storageProgram = (props: StorageProps) =>
494 Effect.gen(function* () {
495 const network = yield* Network("StorageNet", { cidrBlock: "10.44.0.0/16" });
496 const subnetGroup = yield* DBSubnetGroup("StorageSubnetGroup", {
497 description: "alchemy coupled storage lifecycle",
498 subnetIds: network.privateSubnetIds,
499 });
500 return yield* DBInstance("StorageInstance", {
501 engine: "postgres",
502 dbInstanceClass: "db.t3.micro",
503 masterUsername: "alchemy",
504 masterUserPassword: Redacted.make("StorageCouplingPass123"),
505 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
506 backupRetentionPeriod: "0 days",
507 deletionProtection: false,
508 skipFinalSnapshot: true,
509 publiclyAccessible: false,
510 ...props,
511 });
512 });
513
514 const assertStorageState = Effect.fn(function* (
515 identifier: string,
516 expected: StorageState,
517 maximum = 0,
518 ) {
519 const observed = (yield* rds.describeDBInstances({
520 DBInstanceIdentifier: identifier,
521 })).DBInstances?.[0];
522 expect([
523 observed?.AllocatedStorage,
524 observed?.StorageType,
525 observed?.Iops ?? 0,
526 observed?.StorageThroughput ?? 0,
527 ]).toEqual(expected);
528 if (maximum === 0) {
529 expect([0, expected[0]]).toContain(observed?.MaxAllocatedStorage ?? 0);
530 } else {
531 expect(observed?.MaxAllocatedStorage).toBe(maximum);
532 }
533 expect(observed?.PendingModifiedValues?.AllocatedStorage).toBeUndefined();
534 expect(observed?.PendingModifiedValues?.StorageType).toBeUndefined();
535 expect(observed?.PendingModifiedValues?.Iops).toBeUndefined();
536 expect(observed?.PendingModifiedValues?.StorageThroughput).toBeUndefined();
537 });
538
539 // A point-in-time restore swapped into place leaves the same identifier (and
540 // props) on a different physical instance. Do exactly that against the real
541 // API: restore the deployed instance to its latest restorable time under a
542 // temporary identifier, carrying every source setting over, then rename the
543 // original away and the restored copy into place. The redeploy must publish
544 // the restored server's `dbiResourceId`; on main it plans no change and keeps
545 // the old one.
546 lifecycle(1, process.env.RDS_TEST_LIFECYCLE)(
547 "identity: a point-in-time restore swapped into place is picked up on redeploy",
548 (stack) =>
549 Effect.gen(function* () {
550 yield* stack.destroy();
551 const identifier = `alchemy-pitr-${stack.stage.toLowerCase().replace(/[^a-z0-9]+/g, "-")}`;
552 const restoredId = `${identifier}-restored`;
553 const displacedId = `${identifier}-old`;
554
555 const describeInstance = (id: string) =>
556 rds.describeDBInstances({ DBInstanceIdentifier: id }).pipe(
557 Effect.map((r) => r.DBInstances?.[0]),
558 Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(undefined)),
559 );
560 const waitUntil = (id: string, done: (instance: rds.DBInstance | undefined) => boolean) =>
561 describeInstance(id).pipe(
562 Effect.repeat({ schedule: Schedule.spaced("20 seconds"), times: 120, until: done }),
563 );
564 const waitAvailable = (id: string) =>
565 waitUntil(id, (i) => i?.DBInstanceStatus === "available");
566 const deleteIfPresent = (id: string) =>
567 rds
568 .deleteDBInstance({
569 DBInstanceIdentifier: id,
570 SkipFinalSnapshot: true,
571 DeleteAutomatedBackups: true,
572 })
573 .pipe(
574 Effect.catchTag("DBInstanceNotFoundFault", () => Effect.void),
575 Effect.andThen(waitUntil(id, (i) => i === undefined)),
576 Effect.ignore,
577 );
578
579 const program = Effect.gen(function* () {
580 const net = yield* Network("PitrNet", { cidrBlock: "10.48.0.0/16" });
581 const subnetGroup = yield* DBSubnetGroup("PitrSubnetGroup", {
582 description: "alchemy DBInstance point-in-time restore identity",
583 subnetIds: net.privateSubnetIds,
584 });
585 return yield* DBInstance("PitrInstance", {
586 dbInstanceIdentifier: identifier,
587 engine: "postgres",
588 dbInstanceClass: "db.t3.micro",
589 allocatedStorage: 20,
590 masterUsername: "alchemy",
591 masterUserPassword: Redacted.make("alchemy-pitr-test-password"),
592 backupRetentionPeriod: "1 day",
593 deletionProtection: false,
594 skipFinalSnapshot: true,
595 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
596 publiclyAccessible: false,
597 });
598 });
599
600 yield* Effect.gen(function* () {
601 const first = yield* stack.deploy(program);
602
603 // Automated backups make the instance restorable a few minutes in.
604 const source = (yield* waitUntil(
605 identifier,
606 (i) => i?.LatestRestorableTime !== undefined,
607 ))!;
608
609 yield* rds.restoreDBInstanceToPointInTime({
610 SourceDBInstanceIdentifier: identifier,
611 TargetDBInstanceIdentifier: restoredId,
612 UseLatestRestorableTime: true,
613 DBInstanceClass: source.DBInstanceClass,
614 DBSubnetGroupName: source.DBSubnetGroup?.DBSubnetGroupName,
615 VpcSecurityGroupIds: (source.VpcSecurityGroups ?? []).flatMap((g) =>
616 g.VpcSecurityGroupId ? [g.VpcSecurityGroupId] : [],
617 ),
618 DBParameterGroupName: source.DBParameterGroups?.[0]?.DBParameterGroupName,
619 OptionGroupName: source.OptionGroupMemberships?.[0]?.OptionGroupName,
620 StorageType: source.StorageType,
621 AllocatedStorage: source.AllocatedStorage,
622 MaxAllocatedStorage: source.MaxAllocatedStorage,
623 Iops: source.Iops,
624 StorageThroughput: source.StorageThroughput,
625 Port: source.Endpoint?.Port,
626 PubliclyAccessible: source.PubliclyAccessible,
627 MultiAZ: source.MultiAZ,
628 EnableIAMDatabaseAuthentication: source.IAMDatabaseAuthenticationEnabled,
629 CACertificateIdentifier: source.CACertificateIdentifier,
630 BackupRetentionPeriod: source.BackupRetentionPeriod,
631 CopyTagsToSnapshot: source.CopyTagsToSnapshot,
632 DeletionProtection: false,
633 Tags: source.TagList,
634 });
635 const restored = (yield* waitAvailable(restoredId))!;
636 expect(restored.DbiResourceId).not.toBe(first.dbiResourceId);
637
638 // Swap: original out of the way, restored copy into place.
639 yield* rds.modifyDBInstance({
640 DBInstanceIdentifier: identifier,
641 NewDBInstanceIdentifier: displacedId,
642 ApplyImmediately: true,
643 });
644 yield* waitAvailable(displacedId);
645 yield* rds.modifyDBInstance({
646 DBInstanceIdentifier: restoredId,
647 NewDBInstanceIdentifier: identifier,
648 ApplyImmediately: true,
649 });
650 const swapped = (yield* waitUntil(
651 identifier,
652 (i) => i?.DBInstanceStatus === "available" && i.DbiResourceId === restored.DbiResourceId,
653 ))!;
654 expect(swapped.DbiResourceId).toBe(restored.DbiResourceId);
655
656 // Unchanged props, new physical server: the redeploy publishes it.
657 const second = yield* stack.deploy(program);
658 expect(second.dbInstanceIdentifier).toBe(identifier);
659 expect(second.dbiResourceId).toBe(restored.DbiResourceId);
660
661 // The displaced original still sits in the stack's subnet group.
662 yield* deleteIfPresent(displacedId);
663 yield* stack.destroy();
664 yield* assertInstanceGone(identifier);
665 }).pipe(
666 // The displaced original (and a stranded restore) are not tracked by
667 // the stack; remove them whether the test passes or fails.
668 Effect.ensuring(
669 Effect.all([deleteIfPresent(displacedId), deleteIfPresent(restoredId)], {
670 concurrency: 2,
671 }),
672 ),
673 );
674 }),
675 {
676 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
677 timeout: 5_400_000,
678 },
679 );
680
681 const assertInstanceGone = Effect.fn(function* (identifier: string) {
682 const gone = yield* rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
683 Effect.as(false),
684 Effect.catchTag("DBInstanceNotFoundFault", () => Effect.succeed(true)),
685 Effect.repeat({
686 schedule: Schedule.spaced("5 seconds"),
687 times: 8,
688 until: (gone) => gone,
689 }),
690 );
691 expect(gone).toBe(true);
692 });
693
694 interface StorageCase {
695 name: string;
696 initial: StorageProps;
697 desired: StorageProps;
698 before: StorageState;
699 after: StorageState;
700 equivalent?: StorageProps;
701 }
702
703 // Each database receives at most one storage modification; optimization can take hours.
704 const storageCases: StorageCase[] = [
705 {
706 name: "small gp3 defaults and resize",
707 initial: {},
708 desired: {
709 allocatedStorage: 25,
710 storageType: "gp3",
711 iops: 3000,
712 storageThroughput: 125,
713 },
714 before: [20, "gp3", 3000, 125],
715 after: [25, "gp3", 3000, 125],
716 equivalent: {},
717 },
718 {
719 name: "gp3 resize retains provisioned performance",
720 initial: { allocatedStorage: 400 },
721 desired: { allocatedStorage: 500 },
722 before: [400, "gp3", 12000, 500],
723 after: [500, "gp3", 12000, 500],
724 },
725 {
726 name: "gp3 IOPS update includes allocation",
727 initial: { allocatedStorage: 400 },
728 desired: { allocatedStorage: 400, iops: 16000 },
729 before: [400, "gp3", 12000, 500],
730 after: [400, "gp3", 16000, 500],
731 },
732 {
733 name: "gp3 throughput update includes IOPS",
734 initial: { allocatedStorage: 400 },
735 desired: { allocatedStorage: 400, storageThroughput: 750 },
736 before: [400, "gp3", 12000, 500],
737 after: [400, "gp3", 12000, 750],
738 },
739 {
740 name: "gp3 performance removal restores defaults",
741 initial: { allocatedStorage: 400, iops: 16000, storageThroughput: 750 },
742 desired: { allocatedStorage: 400 },
743 before: [400, "gp3", 16000, 750],
744 after: [400, "gp3", 12000, 500],
745 },
746 {
747 name: "storage type removal restores gp3",
748 initial: { allocatedStorage: 25, storageType: "gp2" },
749 desired: {},
750 before: [25, "gp2", 0, 0],
751 after: [25, "gp3", 3000, 125],
752 },
753 ...(["io1", "io2"] as const).flatMap((storageType): StorageCase[] => [
754 {
755 name: `${storageType} resize includes unchanged IOPS`,
756 initial: { storageType },
757 desired: { storageType, allocatedStorage: 120 },
758 before: [100, storageType, 1000, 0],
759 after: [120, storageType, 1000, 0],
760 },
761 {
762 name: `${storageType} performance and autoscaling removal are atomic`,
763 initial: { storageType, iops: 3000, maxAllocatedStorage: 6000 },
764 desired: { storageType },
765 before: [100, storageType, 3000, 0],
766 after: [100, storageType, 1000, 0],
767 equivalent: { storageType, maxAllocatedStorage: 0 },
768 },
769 ]),
770 ];
771
772 // Lane per storage case (index-aligned with `storageCases`).
773 const storageLanes = [6, 7, 7, 7, 8, 6, 8, 8, 9, 9];
774
775 for (const [index, scenario] of storageCases.entries()) {
776 lifecycle(storageLanes[index]!, process.env.RDS_TEST_LIFECYCLE)(
777 `storage coupling: ${scenario.name}`,
778 (stack) =>
779 Effect.gen(function* () {
780 yield* stack.destroy();
781 const created = yield* stack.deploy(storageProgram(scenario.initial));
782 yield* assertStorageState(
783 created.dbInstanceIdentifier,
784 scenario.before,
785 scenario.initial.maxAllocatedStorage,
786 );
787 const updated = yield* stack.deploy(storageProgram(scenario.desired));
788 expect(updated.dbInstanceArn).toBe(created.dbInstanceArn);
789 yield* assertStorageState(
790 updated.dbInstanceIdentifier,
791 scenario.after,
792 scenario.desired.maxAllocatedStorage,
793 );
794 const finalProps = scenario.equivalent ?? scenario.desired;
795 if (scenario.equivalent) {
796 const equivalent = yield* stack.deploy(storageProgram(finalProps));
797 expect(equivalent.dbInstanceArn).toBe(created.dbInstanceArn);
798 yield* assertStorageState(
799 equivalent.dbInstanceIdentifier,
800 scenario.after,
801 finalProps.maxAllocatedStorage,
802 );
803 }
804 expect(
805 (yield* stack.plan(storageProgram(finalProps))).resources.StorageInstance,
806 ).toMatchObject({ action: "noop" });
807 yield* stack.destroy();
808 yield* assertInstanceGone(created.dbInstanceIdentifier);
809 }),
810 {
811 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
812 timeout: INSTANCE_TIMEOUT,
813 },
814 );
815 }
816
817 lifecycle(9, process.env.RDS_TEST_LIFECYCLE)(
818 "storage coupling: plans correction for external storage drift",
819 (stack) =>
820 Effect.gen(function* () {
821 yield* stack.destroy();
822 const desired = storageProgram({});
823 const created = yield* stack.deploy(desired);
824 yield* assertStorageState(created.dbInstanceIdentifier, [20, "gp3", 3000, 125]);
825 yield* rds.modifyDBInstance({
826 DBInstanceIdentifier: created.dbInstanceIdentifier,
827 StorageType: "gp2",
828 ApplyImmediately: true,
829 });
830 yield* rds
831 .describeDBInstances({
832 DBInstanceIdentifier: created.dbInstanceIdentifier,
833 })
834 .pipe(
835 Effect.repeat({
836 schedule: Schedule.min([
837 Schedule.exponential("5 seconds"),
838 Schedule.spaced("1 minute"),
839 ]),
840 times: 10,
841 until: (response) => {
842 const instance = response.DBInstances?.[0];
843 return (
844 instance?.StorageType === "gp2" &&
845 (instance.Iops ?? 0) === 0 &&
846 (instance.StorageThroughput ?? 0) === 0 &&
847 instance.PendingModifiedValues?.StorageType === undefined
848 );
849 },
850 }),
851 );
852 yield* assertStorageState(created.dbInstanceIdentifier, [20, "gp2", 0, 0]);
853 const drift = yield* Drift.detect({
854 name: stack.name,
855 stage: stack.stage,
856 });
857 expect(drift.resources.StorageInstance?.action).toBe("drifted");
858 expect((yield* stack.plan(desired)).resources.StorageInstance).toMatchObject({
859 action: "update",
860 });
861 // A second storage modification must wait for AWS's optimization cooldown.
862 yield* stack.destroy();
863 yield* assertInstanceGone(created.dbInstanceIdentifier);
864 }),
865 {
866 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
867 timeout: INSTANCE_TIMEOUT,
868 },
869 );
870
871 const observeInstanceRequests = Effect.gen(function* () {
872 const client = yield* HttpClient.HttpClient;
873 const requests: Array<{
874 action: string;
875 port: string | null;
876 parameterGroup: string | null;
877 securityGroups: string[];
878 iamAuthentication: string | null;
879 publiclyAccessible: string | null;
880 deletionProtection: string | null;
881 networkType: string | null;
882 applyImmediately: string | null;
883 enabledLogs: string[];
884 disabledLogs: string[];
885 }> = [];
886 const observedClient = client.pipe(
887 HttpClient.tapRequest((request) =>
888 Effect.sync(() => {
889 if (request.body._tag !== "Uint8Array") return;
890 const parameters = new URLSearchParams(new TextDecoder().decode(request.body.body));
891 const action = parameters.get("Action");
892 if (
893 action === "CreateDBInstance" ||
894 action === "ModifyDBInstance" ||
895 action === "DescribeDBInstances"
896 ) {
897 requests.push({
898 action,
899 iamAuthentication: parameters.get("EnableIAMDatabaseAuthentication"),
900 publiclyAccessible: parameters.get("PubliclyAccessible"),
901 deletionProtection: parameters.get("DeletionProtection"),
902 networkType: parameters.get("NetworkType"),
903 applyImmediately: parameters.get("ApplyImmediately"),
904 enabledLogs: [...parameters.entries()]
905 .filter(
906 ([key]) =>
907 key.startsWith("EnableCloudwatchLogsExports.") ||
908 key.startsWith("CloudwatchLogsExportConfiguration.EnableLogTypes."),
909 )
910 .map(([, value]) => value)
911 .sort(),
912 disabledLogs: [...parameters.entries()]
913 .filter(([key]) =>
914 key.startsWith("CloudwatchLogsExportConfiguration.DisableLogTypes."),
915 )
916 .map(([, value]) => value)
917 .sort(),
918 parameterGroup: parameters.get("DBParameterGroupName"),
919 securityGroups: [...parameters.entries()]
920 .filter(([key]) => key.startsWith("VpcSecurityGroupIds."))
921 .map(([, value]) => value)
922 .sort(),
923 port: parameters.get(action === "CreateDBInstance" ? "Port" : "DBPortNumber"),
924 });
925 }
926 }),
927 ),
928 );
929 return { client: observedClient, requests };
930 });
931
932 const portProgram = (port?: number, round = "ports", identifier?: string) =>
933 Effect.gen(function* () {
934 const network = yield* Network("PortNet", { cidrBlock: "10.45.0.0/16" });
935 const subnetGroup = yield* DBSubnetGroup("PortSubnetGroup", {
936 description: "alchemy listener port lifecycle",
937 subnetIds: network.privateSubnetIds,
938 });
939 return yield* DBInstance("PortInstance", {
940 dbInstanceIdentifier: identifier,
941 engine: "postgres",
942 dbInstanceClass: "db.t3.micro",
943 masterUsername: "alchemy",
944 manageMasterUserPassword: true,
945 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
946 backupRetentionPeriod: "0 days",
947 deletionProtection: false,
948 skipFinalSnapshot: true,
949 publiclyAccessible: false,
950 ...(port === undefined ? {} : { port }),
951 tags: { round },
952 });
953 });
954
955 const assertPort = Effect.fn(function* (identifier: string, port: number) {
956 const instance = (yield* rds.describeDBInstances({
957 DBInstanceIdentifier: identifier,
958 })).DBInstances?.[0];
959 expect(instance?.Endpoint?.Port).toBe(port);
960 expect(instance?.PendingModifiedValues?.Port).toBeUndefined();
961 expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
962 return instance;
963 });
964
965 const injectPort = Effect.fn(function* (identifier: string, port: number) {
966 yield* rds.modifyDBInstance({
967 DBInstanceIdentifier: identifier,
968 DBPortNumber: port,
969 ApplyImmediately: true,
970 });
971 yield* rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
972 Effect.repeat({
973 schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("1 minute")]),
974 times: 10,
975 until: (response) => {
976 const instance = response.DBInstances?.[0];
977 return (
978 instance?.DBInstanceStatus === "available" &&
979 instance.Endpoint?.Port === port &&
980 instance.PendingModifiedValues?.Port === undefined
981 );
982 },
983 }),
984 );
985 yield* assertPort(identifier, port);
986 });
987
988 lifecycle(4, process.env.RDS_TEST_LIFECYCLE)(
989 "listener port: defaults, updates, removal, and no redundant writes",
990 (stack) =>
991 Effect.gen(function* () {
992 yield* stack.destroy();
993 const { client, requests } = yield* observeInstanceRequests;
994 const deploy = (port?: number, round?: string) =>
995 stack
996 .deploy(portProgram(port, round))
997 .pipe(Effect.provideService(HttpClient.HttpClient, client));
998 const writes = () =>
999 requests
1000 .filter((request) => request.action === "ModifyDBInstance" && request.port !== null)
1001 .map((request) => request.port);
1002 const created = yield* deploy();
1003 expect(created.endpointPort).toBe(5432);
1004 expect(
1005 requests
1006 .filter((request) => request.action === "CreateDBInstance")
1007 .map((request) => request.port),
1008 ).toEqual(["5432"]);
1009 expect((yield* assertPort(created.dbInstanceIdentifier, 5432))?.DbInstancePort).toBe(0);
1010
1011 requests.length = 0;
1012 yield* deploy(5432, "same-port");
1013 expect(requests.some((request) => request.action === "DescribeDBInstances")).toBe(true);
1014 expect(writes()).toEqual([]);
1015
1016 requests.length = 0;
1017 const changed = yield* deploy(5433);
1018 expect(changed.dbInstanceArn).toBe(created.dbInstanceArn);
1019 expect(changed.endpointPort).toBe(5433);
1020 expect(writes()).toEqual(["5433"]);
1021 yield* assertPort(created.dbInstanceIdentifier, 5433);
1022
1023 requests.length = 0;
1024 yield* deploy(5433, "same-custom-port");
1025 expect(writes()).toEqual([]);
1026 yield* assertPort(created.dbInstanceIdentifier, 5433);
1027
1028 requests.length = 0;
1029 const restored = yield* deploy();
1030 expect(restored.dbInstanceArn).toBe(created.dbInstanceArn);
1031 expect(restored.endpointPort).toBe(5432);
1032 expect(writes()).toEqual(["5432"]);
1033 yield* assertPort(created.dbInstanceIdentifier, 5432);
1034 expect((yield* stack.plan(portProgram())).resources.PortInstance).toMatchObject({
1035 action: "noop",
1036 });
1037 yield* stack.destroy();
1038 yield* assertInstanceGone(created.dbInstanceIdentifier);
1039 }),
1040 {
1041 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1042 timeout: INSTANCE_TIMEOUT,
1043 },
1044 );
1045
1046 lifecycle(4, process.env.RDS_TEST_LIFECYCLE)(
1047 "listener port: unchanged-input drift repair and adoption defaults",
1048 (stack) =>
1049 Effect.gen(function* () {
1050 yield* stack.destroy();
1051 const identifier = "alchemy-rds-port-adoption";
1052 const program = (port?: number) => portProgram(port, "adoption", identifier);
1053 const created = yield* stack.deploy(program(5433));
1054 yield* assertPort(identifier, 5433);
1055 yield* injectPort(identifier, 5434);
1056 expect((yield* stack.plan(program(5433))).resources.PortInstance).toMatchObject({
1057 action: "update",
1058 });
1059 const drift = yield* Drift.detect({
1060 name: stack.name,
1061 stage: stack.stage,
1062 });
1063 expect(drift.resources.PortInstance?.action).toBe("drifted");
1064 const repaired = yield* stack.deploy(program(5433));
1065 expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
1066 expect(repaired.endpointPort).toBe(5433);
1067 yield* assertPort(identifier, 5433);
1068 expect((yield* stack.plan(program(5433))).resources.PortInstance).toMatchObject({
1069 action: "noop",
1070 });
1071
1072 // Removing the saved row exercises discovery with no previous props or attributes.
1073 yield* Effect.gen(function* () {
1074 const state = yield* yield* State;
1075 yield* state.delete({
1076 stack: stack.name,
1077 stage: stack.stage,
1078 fqn: "PortInstance",
1079 });
1080 }).pipe(Effect.provide(stack.state));
1081 const adopted = yield* stack.deploy(program());
1082 expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
1083 expect(adopted.endpointPort).toBe(5432);
1084 yield* assertPort(identifier, 5432);
1085 expect((yield* stack.plan(program())).resources.PortInstance).toMatchObject({
1086 action: "noop",
1087 });
1088
1089 yield* injectPort(identifier, 5434);
1090 expect((yield* stack.plan(program())).resources.PortInstance).toMatchObject({
1091 action: "update",
1092 });
1093 const defaultRepaired = yield* stack.deploy(program());
1094 expect(defaultRepaired.endpointPort).toBe(5432);
1095 yield* assertPort(identifier, 5432);
1096 expect((yield* stack.plan(program())).resources.PortInstance).toMatchObject({
1097 action: "noop",
1098 });
1099 yield* stack.destroy();
1100 yield* assertInstanceGone(identifier);
1101 }),
1102 {
1103 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1104 timeout: LONG_INSTANCE_TIMEOUT,
1105 },
1106 );
1107
1108 lifecycle(4, process.env.RDS_TEST_LIFECYCLE)(
1109 "listener port: waits for an accepted change without resubmitting",
1110 (stack) =>
1111 Effect.gen(function* () {
1112 yield* stack.destroy();
1113 const created = yield* stack.deploy(portProgram());
1114 yield* assertPort(created.dbInstanceIdentifier, 5432);
1115 yield* rds.modifyDBInstance({
1116 DBInstanceIdentifier: created.dbInstanceIdentifier,
1117 DBPortNumber: 5433,
1118 ApplyImmediately: false,
1119 });
1120 const { client, requests } = yield* observeInstanceRequests;
1121 const settled = yield* stack
1122 .deploy(portProgram(5433))
1123 .pipe(Effect.provideService(HttpClient.HttpClient, client));
1124 expect(settled.dbInstanceArn).toBe(created.dbInstanceArn);
1125 expect(settled.endpointPort).toBe(5433);
1126 expect(requests.some((request) => request.action === "DescribeDBInstances")).toBe(true);
1127 expect(
1128 requests.filter(
1129 (request) => request.action === "ModifyDBInstance" && request.port !== null,
1130 ),
1131 ).toEqual([]);
1132 yield* assertPort(created.dbInstanceIdentifier, 5433);
1133 expect((yield* stack.plan(portProgram(5433))).resources.PortInstance).toMatchObject({
1134 action: "noop",
1135 });
1136 yield* stack.destroy();
1137 yield* assertInstanceGone(created.dbInstanceIdentifier);
1138 }),
1139 {
1140 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1141 timeout: INSTANCE_TIMEOUT,
1142 },
1143 );
1144
1145 type SecurityProps = Pick<
1146 DBInstanceProps,
1147 | "enableIAMDatabaseAuthentication"
1148 | "publiclyAccessible"
1149 | "deletionProtection"
1150 | "networkType"
1151 | "enableCloudwatchLogsExports"
1152 >;
1153
1154 const securityProgram = (
1155 security: SecurityProps = {},
1156 round = "security",
1157 identifier?: string,
1158 preferredMaintenanceWindow?: string,
1159 ) =>
1160 Effect.gen(function* () {
1161 const network = yield* Network("SecurityNet", {
1162 cidrBlock: "10.51.0.0/16",
1163 });
1164 const subnetGroup = yield* DBSubnetGroup("SecuritySubnetGroup", {
1165 description: "RDS security desired-state lifecycle",
1166 subnetIds: network.publicSubnetIds,
1167 });
1168 return yield* DBInstance("SecurityInstance", {
1169 dbInstanceIdentifier: identifier,
1170 engine: "postgres",
1171 dbInstanceClass: "db.t3.micro",
1172 masterUsername: "alchemy",
1173 manageMasterUserPassword: true,
1174 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
1175 backupRetentionPeriod: "0 days",
1176 preferredMaintenanceWindow,
1177 skipFinalSnapshot: true,
1178 ...security,
1179 tags: { round },
1180 });
1181 });
1182
1183 const assertSecurity = Effect.fn(function* (identifier: string, security: SecurityProps = {}) {
1184 const instance = (yield* rds.describeDBInstances({
1185 DBInstanceIdentifier: identifier,
1186 })).DBInstances?.[0];
1187 expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
1188 expect(instance?.IAMDatabaseAuthenticationEnabled).toBe(
1189 security.enableIAMDatabaseAuthentication ?? false,
1190 );
1191 expect(instance?.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled).toBeUndefined();
1192 expect(instance?.PubliclyAccessible).toBe(security.publiclyAccessible ?? false);
1193 expect(instance?.DeletionProtection).toBe(security.deletionProtection ?? false);
1194 expect(instance?.NetworkType).toBe(security.networkType ?? "IPV4");
1195 expect([...(instance?.EnabledCloudwatchLogsExports ?? [])].sort()).toEqual(
1196 [...new Set(security.enableCloudwatchLogsExports ?? [])].sort(),
1197 );
1198 expect(
1199 instance?.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToEnable ?? [],
1200 ).toEqual([]);
1201 expect(
1202 instance?.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToDisable ?? [],
1203 ).toEqual([]);
1204 return instance;
1205 });
1206
1207 const waitForSecurityObservation = (
1208 identifier: string,
1209 until: (instance: rds.DBInstance) => boolean,
1210 ) =>
1211 rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
1212 Effect.map((response) => response.DBInstances?.[0]),
1213 Effect.repeat({
1214 schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("1 minute")]),
1215 times: 10,
1216 until: (instance) =>
1217 instance !== undefined &&
1218 ["available", "storage-optimization"].includes(instance.DBInstanceStatus ?? "") &&
1219 until(instance),
1220 }),
1221 Effect.tap((instance) =>
1222 Effect.sync(() => {
1223 expect(instance).toBeDefined();
1224 expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
1225 expect(instance !== undefined && until(instance)).toBe(true);
1226 }),
1227 ),
1228 );
1229
1230 const enabledSecurity: SecurityProps = {
1231 enableIAMDatabaseAuthentication: true,
1232 publiclyAccessible: true,
1233 deletionProtection: true,
1234 networkType: "IPV4",
1235 enableCloudwatchLogsExports: ["postgresql", "upgrade"],
1236 };
1237
1238 lifecycle(2, process.env.RDS_TEST_LIFECYCLE)(
1239 "security: defaults, removal, drift, adoption, and no redundant writes",
1240 (stack) =>
1241 Effect.gen(function* () {
1242 yield* stack.destroy();
1243 const identifier = "alchemy-rds-security-defaults";
1244 const { client, requests } = yield* observeInstanceRequests;
1245 const program = (security: SecurityProps = {}, round = "security") =>
1246 securityProgram(security, round, identifier);
1247 const deploy = (security: SecurityProps = {}, round = "security") =>
1248 stack
1249 .deploy(program(security, round))
1250 .pipe(Effect.provideService(HttpClient.HttpClient, client));
1251 const created = yield* deploy();
1252 yield* assertSecurity(identifier);
1253 expect(created.iamDatabaseAuthenticationEnabled).toBe(false);
1254 expect(created.pendingIamDatabaseAuthenticationEnabled).toBeUndefined();
1255 expect(created.publiclyAccessible).toBe(false);
1256 expect(created.deletionProtection).toBe(false);
1257 expect(created.networkType).toBe("IPV4");
1258 expect(created.enabledCloudwatchLogsExports).toEqual([]);
1259 expect(Object.keys(created.vpcSecurityGroupStatuses).sort()).toEqual(
1260 [...created.vpcSecurityGroupIds].sort(),
1261 );
1262 expect(Object.values(created.vpcSecurityGroupStatuses)).toEqual(["active"]);
1263 expect(requests.filter((request) => request.action === "CreateDBInstance")).toMatchObject([
1264 {
1265 iamAuthentication: "false",
1266 publiclyAccessible: "false",
1267 deletionProtection: "false",
1268 networkType: "IPV4",
1269 enabledLogs: [],
1270 },
1271 ]);
1272
1273 const enabled = yield* deploy(enabledSecurity);
1274 expect(enabled.dbInstanceArn).toBe(created.dbInstanceArn);
1275 expect(enabled.iamDatabaseAuthenticationEnabled).toBe(true);
1276 expect(enabled.pendingIamDatabaseAuthenticationEnabled).toBeUndefined();
1277 yield* assertSecurity(identifier, enabledSecurity);
1278 requests.length = 0;
1279 yield* deploy(
1280 {
1281 ...enabledSecurity,
1282 enableCloudwatchLogsExports: ["upgrade", "postgresql", "postgresql"],
1283 },
1284 "set-equivalent",
1285 );
1286 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toEqual([]);
1287
1288 requests.length = 0;
1289 const removed = yield* deploy();
1290 expect(removed.dbInstanceArn).toBe(created.dbInstanceArn);
1291 yield* assertSecurity(identifier);
1292 expect(requests.filter((request) => request.disabledLogs.length > 0)).toMatchObject([
1293 {
1294 disabledLogs: ["postgresql", "upgrade"],
1295 applyImmediately: "false",
1296 },
1297 ]);
1298 expect((yield* stack.plan(program())).resources.SecurityInstance).toMatchObject({
1299 action: "noop",
1300 });
1301
1302 const inject = Effect.gen(function* () {
1303 yield* rds.modifyDBInstance({
1304 DBInstanceIdentifier: identifier,
1305 EnableIAMDatabaseAuthentication: true,
1306 PubliclyAccessible: true,
1307 DeletionProtection: true,
1308 CloudwatchLogsExportConfiguration: {
1309 EnableLogTypes: ["postgresql", "upgrade"],
1310 },
1311 ApplyImmediately: true,
1312 });
1313 yield* waitForSecurityObservation(
1314 identifier,
1315 (instance) =>
1316 instance.IAMDatabaseAuthenticationEnabled === true &&
1317 instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === undefined &&
1318 instance.PubliclyAccessible === true &&
1319 instance.DeletionProtection === true &&
1320 (instance.EnabledCloudwatchLogsExports ?? []).length === 2 &&
1321 (instance.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToEnable
1322 ?.length ?? 0) === 0 &&
1323 (instance.PendingModifiedValues?.PendingCloudwatchLogsExports?.LogTypesToDisable
1324 ?.length ?? 0) === 0,
1325 );
1326 yield* assertSecurity(identifier, enabledSecurity);
1327 });
1328 yield* inject;
1329 const drift = yield* Drift.detect({
1330 name: stack.name,
1331 stage: stack.stage,
1332 });
1333 expect(drift.resources.SecurityInstance?.action).toBe("drifted");
1334 expect((yield* stack.plan(program())).resources.SecurityInstance).toMatchObject({
1335 action: "update",
1336 });
1337 const repaired = yield* deploy();
1338 expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
1339 yield* assertSecurity(identifier);
1340
1341 yield* inject;
1342 yield* Effect.gen(function* () {
1343 const state = yield* yield* State;
1344 yield* state.delete({
1345 stack: stack.name,
1346 stage: stack.stage,
1347 fqn: "SecurityInstance",
1348 });
1349 }).pipe(Effect.provide(stack.state));
1350 const adopted = yield* deploy();
1351 expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
1352 yield* assertSecurity(identifier);
1353 requests.length = 0;
1354 yield* deploy({}, "tag-only");
1355 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toEqual([]);
1356 expect((yield* stack.plan(program({}, "tag-only"))).resources.SecurityInstance).toMatchObject(
1357 { action: "noop" },
1358 );
1359 yield* stack.destroy();
1360 yield* assertInstanceGone(identifier);
1361 }),
1362 {
1363 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1364 timeout: LONG_INSTANCE_TIMEOUT,
1365 },
1366 );
1367
1368 lifecycle(2, process.env.RDS_TEST_LIFECYCLE)(
1369 "security: maintenance-queued IAM convergence and immediate-only queue isolation",
1370 (stack) =>
1371 Effect.gen(function* () {
1372 yield* stack.destroy();
1373 const maintenance = yield* Effect.sync(() => {
1374 const day = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"][
1375 (new Date().getUTCDay() + 2) % 7
1376 ];
1377 return `${day}:03:00-${day}:04:00`;
1378 });
1379 const { client, requests } = yield* observeInstanceRequests;
1380 const program = (security: SecurityProps = {}, round = "security") =>
1381 securityProgram(security, round, undefined, maintenance);
1382 const deploy = (security: SecurityProps = {}, round = "security") =>
1383 stack
1384 .deploy(program(security, round))
1385 .pipe(Effect.provideService(HttpClient.HttpClient, client));
1386 const created = yield* deploy();
1387 const identifier = created.dbInstanceIdentifier;
1388 yield* rds.modifyDBInstance({
1389 DBInstanceIdentifier: identifier,
1390 BackupRetentionPeriod: 1,
1391 ApplyImmediately: false,
1392 });
1393 yield* waitForSecurityObservation(
1394 identifier,
1395 (instance) => instance.PendingModifiedValues?.BackupRetentionPeriod === 1,
1396 );
1397 requests.length = 0;
1398 const immediateOnly: SecurityProps = {
1399 publiclyAccessible: true,
1400 deletionProtection: true,
1401 enableCloudwatchLogsExports: ["postgresql"],
1402 };
1403 const changed = yield* deploy(immediateOnly);
1404 expect(changed.dbInstanceArn).toBe(created.dbInstanceArn);
1405 expect(
1406 (yield* assertSecurity(identifier, immediateOnly))?.PendingModifiedValues
1407 ?.BackupRetentionPeriod,
1408 ).toBe(1);
1409 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
1410 { applyImmediately: "false", iamAuthentication: null },
1411 ]);
1412 expect((yield* stack.plan(program(immediateOnly))).resources.SecurityInstance).toMatchObject({
1413 action: "noop",
1414 });
1415 yield* deploy();
1416 expect(
1417 (yield* assertSecurity(identifier))?.PendingModifiedValues?.BackupRetentionPeriod,
1418 ).toBe(1);
1419 // Cancel the test's unrelated queued change before exercising queue promotion.
1420 yield* rds.modifyDBInstance({
1421 DBInstanceIdentifier: identifier,
1422 BackupRetentionPeriod: 0,
1423 ApplyImmediately: true,
1424 });
1425 yield* waitForSecurityObservation(
1426 identifier,
1427 (instance) =>
1428 instance.BackupRetentionPeriod === 0 &&
1429 instance.PendingModifiedValues?.BackupRetentionPeriod === undefined,
1430 );
1431
1432 yield* rds.modifyDBInstance({
1433 DBInstanceIdentifier: identifier,
1434 EnableIAMDatabaseAuthentication: true,
1435 ApplyImmediately: false,
1436 });
1437 yield* waitForSecurityObservation(
1438 identifier,
1439 (instance) =>
1440 instance.IAMDatabaseAuthenticationEnabled === false &&
1441 instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === true,
1442 );
1443 const pendingDrift = yield* Drift.detect({
1444 name: stack.name,
1445 stage: stack.stage,
1446 });
1447 expect(pendingDrift.resources.SecurityInstance).toMatchObject({
1448 action: "drifted",
1449 attr: {
1450 iamDatabaseAuthenticationEnabled: false,
1451 pendingIamDatabaseAuthenticationEnabled: true,
1452 },
1453 });
1454 requests.length = 0;
1455 const desired = { enableIAMDatabaseAuthentication: true };
1456 const promoted = yield* deploy(desired);
1457 expect(promoted.iamDatabaseAuthenticationEnabled).toBe(true);
1458 expect(promoted.pendingIamDatabaseAuthenticationEnabled).toBeUndefined();
1459 yield* assertSecurity(identifier, desired);
1460 // Matching pending IAM needs queue promotion, not another IAM assignment.
1461 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
1462 { applyImmediately: "true", iamAuthentication: null },
1463 ]);
1464
1465 yield* rds.modifyDBInstance({
1466 DBInstanceIdentifier: identifier,
1467 EnableIAMDatabaseAuthentication: false,
1468 ApplyImmediately: false,
1469 });
1470 yield* waitForSecurityObservation(
1471 identifier,
1472 (instance) =>
1473 instance.IAMDatabaseAuthenticationEnabled === true &&
1474 instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === false,
1475 );
1476 expect((yield* stack.plan(program(desired))).resources.SecurityInstance).toMatchObject({
1477 action: "update",
1478 });
1479 requests.length = 0;
1480 const repaired = yield* deploy(desired);
1481 expect(repaired.dbInstanceArn).toBe(created.dbInstanceArn);
1482 yield* assertSecurity(identifier, desired);
1483 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
1484 { applyImmediately: "true", iamAuthentication: "true" },
1485 ]);
1486 requests.length = 0;
1487 yield* deploy(desired, "tag-only");
1488 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toEqual([]);
1489 yield* rds.modifyDBInstance({
1490 DBInstanceIdentifier: identifier,
1491 EnableIAMDatabaseAuthentication: false,
1492 ApplyImmediately: false,
1493 });
1494 yield* waitForSecurityObservation(
1495 identifier,
1496 (instance) =>
1497 instance.IAMDatabaseAuthenticationEnabled === true &&
1498 instance.PendingModifiedValues?.IAMDatabaseAuthenticationEnabled === false,
1499 );
1500 requests.length = 0;
1501 yield* deploy();
1502 yield* assertSecurity(identifier);
1503 expect(requests.filter((request) => request.action === "ModifyDBInstance")).toMatchObject([
1504 { applyImmediately: "true", iamAuthentication: null },
1505 ]);
1506 expect((yield* stack.plan(program())).resources.SecurityInstance).toMatchObject({
1507 action: "noop",
1508 });
1509 yield* stack.destroy();
1510 yield* assertInstanceGone(identifier);
1511 }),
1512 {
1513 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1514 timeout: INSTANCE_TIMEOUT,
1515 },
1516 );
1517
1518 lifecycle(3, process.env.AWS_TEST_RDS_DBINSTANCE)(
1519 "security: Aurora cluster ownership survives omitted membership and adoption",
1520 (stack) =>
1521 Effect.gen(function* () {
1522 yield* stack.destroy();
1523 const identifier = "alchemy-rds-security-aurora";
1524 const { client, requests } = yield* observeInstanceRequests;
1525 const program = (membership = true, explicit = true, round = "created") =>
1526 Effect.gen(function* () {
1527 const network = yield* Network("AuroraSecurityNet", {
1528 cidrBlock: "10.52.0.0/16",
1529 });
1530 const subnetGroup = yield* DBSubnetGroup("AuroraSecuritySubnetGroup", {
1531 description: "Aurora instance security ownership",
1532 subnetIds: network.privateSubnetIds,
1533 });
1534 const cluster = yield* DBCluster("AuroraSecurityCluster", {
1535 engine: "aurora-postgresql",
1536 engineMode: "provisioned",
1537 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
1538 masterUsername: "alchemy",
1539 manageMasterUserPassword: true,
1540 enableIAMDatabaseAuthentication: true,
1541 enableCloudwatchLogsExports: ["postgresql"],
1542 networkType: "IPV4",
1543 deletionProtection: false,
1544 serverlessV2ScalingConfiguration: {
1545 MinCapacity: 0.5,
1546 MaxCapacity: 1,
1547 },
1548 });
1549 return yield* DBInstance("AuroraSecurityInstance", {
1550 dbInstanceIdentifier: identifier,
1551 dbClusterIdentifier: membership ? cluster.dbClusterIdentifier : undefined,
1552 engine: "aurora-postgresql",
1553 dbInstanceClass: "db.serverless",
1554 ...(explicit
1555 ? {
1556 enableIAMDatabaseAuthentication: false,
1557 deletionProtection: true,
1558 networkType: "DUAL",
1559 enableCloudwatchLogsExports: ["not-an-instance-log"],
1560 }
1561 : {}),
1562 tags: { round, cluster: cluster.dbClusterIdentifier },
1563 });
1564 });
1565 const deploy = (membership = true, explicit = true, round = "created") =>
1566 stack
1567 .deploy(program(membership, explicit, round))
1568 .pipe(Effect.provideService(HttpClient.HttpClient, client));
1569 const created = yield* deploy();
1570 expect(created.publiclyAccessible).toBe(false);
1571 const check = Effect.gen(function* () {
1572 const instance = (yield* rds.describeDBInstances({
1573 DBInstanceIdentifier: identifier,
1574 })).DBInstances![0]!;
1575 const cluster = (yield* rds.describeDBClusters({
1576 DBClusterIdentifier: instance.DBClusterIdentifier!,
1577 })).DBClusters![0]!;
1578 expect(cluster.IAMDatabaseAuthenticationEnabled).toBe(true);
1579 expect(cluster.EnabledCloudwatchLogsExports).toEqual(["postgresql"]);
1580 expect(cluster.DeletionProtection).toBe(false);
1581 expect(cluster.NetworkType).toBe("IPV4");
1582 expect(instance.PubliclyAccessible).toBe(false);
1583 expect(
1584 requests
1585 .filter(
1586 (request) =>
1587 request.action === "CreateDBInstance" || request.action === "ModifyDBInstance",
1588 )
1589 .every(
1590 (request) =>
1591 request.iamAuthentication === null &&
1592 request.deletionProtection === null &&
1593 request.networkType === null &&
1594 request.enabledLogs.length === 0 &&
1595 request.disabledLogs.length === 0,
1596 ),
1597 ).toBe(true);
1598 return instance;
1599 });
1600 yield* check;
1601 const omitted = yield* deploy(true, false, "omitted");
1602 expect(omitted.dbInstanceArn).toBe(created.dbInstanceArn);
1603 yield* check;
1604 yield* Effect.gen(function* () {
1605 const state = yield* yield* State;
1606 yield* state.delete({
1607 stack: stack.name,
1608 stage: stack.stage,
1609 fqn: "AuroraSecurityInstance",
1610 });
1611 }).pipe(Effect.provide(stack.state));
1612 const adopted = yield* deploy(false, true, "adopted");
1613 expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
1614 const observed = yield* check;
1615 expect(adopted.iamDatabaseAuthenticationEnabled).toBe(
1616 observed.IAMDatabaseAuthenticationEnabled,
1617 );
1618 expect(adopted.enabledCloudwatchLogsExports).toEqual(
1619 observed.EnabledCloudwatchLogsExports ?? [],
1620 );
1621 expect(
1622 (yield* stack.plan(program(false, true, "adopted"))).resources.AuroraSecurityInstance,
1623 ).toMatchObject({ action: "noop" });
1624 yield* stack.destroy();
1625 yield* assertInstanceGone(identifier);
1626 }),
1627 {
1628 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1629 timeout: INSTANCE_TIMEOUT,
1630 },
1631 );
1632
1633 const associationProgram = (
1634 options: {
1635 explicit?: boolean;
1636 reverse?: boolean;
1637 version?: string;
1638 identifier?: string;
1639 round?: string;
1640 omitSubnet?: boolean;
1641 } = {},
1642 ) =>
1643 Effect.gen(function* () {
1644 const network = yield* Network("AssociationNet", {
1645 cidrBlock: "10.46.0.0/16",
1646 });
1647 const subnetGroup = yield* DBSubnetGroup("AssociationSubnetGroup", {
1648 description: "RDS association lifecycle",
1649 subnetIds: network.privateSubnetIds,
1650 });
1651 const groupA = yield* SecurityGroup("AssociationGroupA", {
1652 vpcId: network.vpcId,
1653 description: "RDS association A",
1654 });
1655 const groupB = yield* SecurityGroup("AssociationGroupB", {
1656 vpcId: network.vpcId,
1657 description: "RDS association B",
1658 });
1659 const parameterGroup = yield* DBParameterGroup("AssociationParameters", {
1660 family: "postgres16",
1661 parameters: { max_connections: "120" },
1662 });
1663 const groups = options.reverse
1664 ? [groupB.groupId, groupA.groupId, groupA.groupId]
1665 : [groupA.groupId, groupB.groupId];
1666 const instance = yield* DBInstance("AssociationInstance", {
1667 dbInstanceIdentifier: options.identifier,
1668 engine: "postgres",
1669 engineVersion: options.version,
1670 dbInstanceClass: "db.t3.micro",
1671 masterUsername: "alchemy",
1672 manageMasterUserPassword: true,
1673 ...(options.omitSubnet ? {} : { dbSubnetGroupName: subnetGroup.dbSubnetGroupName }),
1674 ...(options.explicit
1675 ? {
1676 dbParameterGroupName: parameterGroup.dbParameterGroupName,
1677 vpcSecurityGroupIds: groups,
1678 }
1679 : {}),
1680 backupRetentionPeriod: "0 days",
1681 deletionProtection: false,
1682 skipFinalSnapshot: true,
1683 publiclyAccessible: false,
1684 // Keep teardown ordered when adoption omits the subnet input.
1685 tags: {
1686 round: options.round ?? "associations",
1687 subnetGroup: subnetGroup.dbSubnetGroupName,
1688 },
1689 });
1690 return {
1691 instance,
1692 parameterGroupName: parameterGroup.dbParameterGroupName,
1693 groupIds: [groupA.groupId, groupB.groupId],
1694 vpcId: network.vpcId,
1695 };
1696 });
1697
1698 const defaultGroupForVpc = Effect.fn(function* (vpcId: string) {
1699 const groups = yield* ec2.describeSecurityGroups({
1700 Filters: [
1701 { Name: "vpc-id", Values: [vpcId] },
1702 { Name: "group-name", Values: ["default"] },
1703 ],
1704 });
1705 expect(groups.SecurityGroups).toHaveLength(1);
1706 return groups.SecurityGroups![0]!.GroupId!;
1707 });
1708
1709 const assertAssociations = Effect.fn(function* (
1710 identifier: string,
1711 parameterGroup: string,
1712 securityGroups: string[],
1713 ) {
1714 const instance = (yield* rds.describeDBInstances({
1715 DBInstanceIdentifier: identifier,
1716 })).DBInstances?.[0];
1717 expect(["available", "storage-optimization"]).toContain(instance?.DBInstanceStatus);
1718 expect(instance?.DBParameterGroups?.map((group) => group.DBParameterGroupName)).toEqual([
1719 parameterGroup,
1720 ]);
1721 expect(["in-sync", "pending-reboot"]).toContain(
1722 instance?.DBParameterGroups?.[0]?.ParameterApplyStatus,
1723 );
1724 expect(instance?.VpcSecurityGroups?.map((group) => group.VpcSecurityGroupId).sort()).toEqual(
1725 [...securityGroups].sort(),
1726 );
1727 expect(instance?.VpcSecurityGroups?.every((group) => group.Status === "active")).toBe(true);
1728 return instance;
1729 });
1730
1731 const injectAssociations = Effect.fn(function* (
1732 identifier: string,
1733 parameterGroup: string,
1734 securityGroups: string[],
1735 ) {
1736 yield* rds.modifyDBInstance({
1737 DBInstanceIdentifier: identifier,
1738 DBParameterGroupName: parameterGroup,
1739 VpcSecurityGroupIds: securityGroups,
1740 ApplyImmediately: true,
1741 });
1742 yield* rds.describeDBInstances({ DBInstanceIdentifier: identifier }).pipe(
1743 Effect.repeat({
1744 schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("1 minute")]),
1745 times: 10,
1746 until: (response) => {
1747 const db = response.DBInstances?.[0];
1748 return (
1749 db?.DBInstanceStatus === "available" &&
1750 db.DBParameterGroups?.[0]?.DBParameterGroupName === parameterGroup &&
1751 ["in-sync", "pending-reboot"].includes(
1752 db.DBParameterGroups?.[0]?.ParameterApplyStatus ?? "",
1753 ) &&
1754 db.VpcSecurityGroups?.every((group) => group.Status === "active") === true &&
1755 JSON.stringify(db.VpcSecurityGroups.map((group) => group.VpcSecurityGroupId).sort()) ===
1756 JSON.stringify([...securityGroups].sort())
1757 );
1758 },
1759 }),
1760 );
1761 yield* assertAssociations(identifier, parameterGroup, securityGroups);
1762 });
1763
1764 lifecycle(5, process.env.RDS_TEST_LIFECYCLE)(
1765 "associations: defaults, explicit attachments, and order-independent no-op writes",
1766 (stack) =>
1767 Effect.gen(function* () {
1768 yield* stack.destroy();
1769 const { client, requests } = yield* observeInstanceRequests;
1770 const options = { version: "16.13" };
1771 const deploy = (extra: Parameters<typeof associationProgram>[0] = {}) =>
1772 stack
1773 .deploy(associationProgram({ ...options, ...extra }))
1774 .pipe(Effect.provideService(HttpClient.HttpClient, client));
1775 const created = yield* deploy();
1776 const defaultGroup = yield* defaultGroupForVpc(created.vpcId);
1777 yield* assertAssociations(created.instance.dbInstanceIdentifier, "default.postgres16", [
1778 defaultGroup,
1779 ]);
1780 expect(created.instance.vpcSecurityGroupIds).toEqual([defaultGroup]);
1781 expect(requests.filter((request) => request.action === "CreateDBInstance")).toMatchObject([
1782 {
1783 parameterGroup: "default.postgres16",
1784 securityGroups: [defaultGroup],
1785 },
1786 ]);
1787
1788 requests.length = 0;
1789 const attached = yield* deploy({ explicit: true });
1790 expect(attached.instance.dbInstanceArn).toBe(created.instance.dbInstanceArn);
1791 yield* assertAssociations(
1792 created.instance.dbInstanceIdentifier,
1793 created.parameterGroupName,
1794 created.groupIds,
1795 );
1796 expect(
1797 requests.filter(
1798 (request) =>
1799 request.action === "ModifyDBInstance" &&
1800 (request.parameterGroup !== null || request.securityGroups.length > 0),
1801 ),
1802 ).toHaveLength(1);
1803 expect(["in-sync", "pending-reboot"]).toContain(
1804 attached.instance.dbParameterGroupApplyStatuses[created.parameterGroupName],
1805 );
1806
1807 requests.length = 0;
1808 yield* deploy({ explicit: true, reverse: true, round: "reordered" });
1809 expect(requests.some((request) => request.action === "DescribeDBInstances")).toBe(true);
1810 expect(
1811 requests.filter(
1812 (request) =>
1813 request.action === "ModifyDBInstance" &&
1814 (request.parameterGroup !== null || request.securityGroups.length > 0),
1815 ),
1816 ).toEqual([]);
1817 yield* assertAssociations(
1818 created.instance.dbInstanceIdentifier,
1819 created.parameterGroupName,
1820 created.groupIds,
1821 );
1822 expect(
1823 (yield* stack.plan(
1824 associationProgram({
1825 ...options,
1826 explicit: true,
1827 reverse: true,
1828 round: "reordered",
1829 }),
1830 )).resources.AssociationInstance,
1831 ).toMatchObject({ action: "noop" });
1832 yield* stack.destroy();
1833 yield* assertInstanceGone(created.instance.dbInstanceIdentifier);
1834 }),
1835 {
1836 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1837 timeout: INSTANCE_TIMEOUT,
1838 },
1839 );
1840
1841 lifecycle(5, process.env.RDS_TEST_LIFECYCLE)(
1842 "associations: removal, older-engine defaults, drift, and nondefault-VPC adoption",
1843 (stack) =>
1844 Effect.gen(function* () {
1845 yield* stack.destroy();
1846 const identifier = "alchemy-rds-association-defaults";
1847 const program = (extra: Parameters<typeof associationProgram>[0] = {}) =>
1848 associationProgram({ identifier, ...extra });
1849 const created = yield* stack.deploy(program({ version: "16.13", explicit: true }));
1850 const defaultGroup = yield* defaultGroupForVpc(created.vpcId);
1851 yield* assertAssociations(identifier, created.parameterGroupName, created.groupIds);
1852
1853 const removed = yield* stack.deploy(program());
1854 expect(removed.instance.dbInstanceArn).toBe(created.instance.dbInstanceArn);
1855 expect(removed.instance.engineVersion).toBe("16.13");
1856 yield* assertAssociations(identifier, "default.postgres16", [defaultGroup]);
1857 expect(
1858 (yield* ec2.describeSecurityGroups({ GroupIds: created.groupIds })).SecurityGroups,
1859 ).toHaveLength(2);
1860 expect(
1861 (yield* rds.describeDBParameterGroups({
1862 DBParameterGroupName: created.parameterGroupName,
1863 })).DBParameterGroups,
1864 ).toHaveLength(1);
1865
1866 yield* injectAssociations(identifier, created.parameterGroupName, created.groupIds);
1867 const drift = yield* Drift.detect({
1868 name: stack.name,
1869 stage: stack.stage,
1870 });
1871 expect(drift.resources.AssociationInstance?.action).toBe("drifted");
1872 expect((yield* stack.plan(program())).resources.AssociationInstance).toMatchObject({
1873 action: "update",
1874 });
1875 yield* stack.deploy(program());
1876 yield* assertAssociations(identifier, "default.postgres16", [defaultGroup]);
1877 expect((yield* stack.plan(program())).resources.AssociationInstance).toMatchObject({
1878 action: "noop",
1879 });
1880
1881 yield* injectAssociations(identifier, created.parameterGroupName, created.groupIds);
1882 yield* Effect.gen(function* () {
1883 const state = yield* yield* State;
1884 yield* state.delete({
1885 stack: stack.name,
1886 stage: stack.stage,
1887 fqn: "AssociationInstance",
1888 });
1889 }).pipe(Effect.provide(stack.state));
1890 const adopted = yield* stack.deploy(program({ omitSubnet: true }));
1891 expect(adopted.instance.dbInstanceArn).toBe(created.instance.dbInstanceArn);
1892 expect(adopted.instance.engineVersion).toBe("16.13");
1893 yield* assertAssociations(identifier, "default.postgres16", [defaultGroup]);
1894 expect(
1895 (yield* stack.plan(program({ omitSubnet: true }))).resources.AssociationInstance,
1896 ).toMatchObject({ action: "noop" });
1897 yield* stack.destroy();
1898 yield* assertInstanceGone(identifier);
1899 }),
1900 {
1901 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
1902 timeout: LONG_INSTANCE_TIMEOUT,
1903 },
1904 );
1905
1906 lifecycle(6, process.env.RDS_TEST_LIFECYCLE)(
1907 "managed secret policy: create, update, removal, drift, adoption, and public rejection",
1908 (stack) =>
1909 Effect.gen(function* () {
1910 yield* stack.destroy();
1911 const { accountId } = yield* AWSEnvironment.current;
1912 const policy = (actions: string[]): PolicyDocument => ({
1913 Version: "2012-10-17",
1914 Statement: [
1915 {
1916 Effect: "Allow",
1917 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
1918 Action: actions,
1919 Resource: "*",
1920 },
1921 ],
1922 });
1923 const initialPolicy = policy(["secretsmanager:DescribeSecret"]);
1924 const updatedPolicy = policy([
1925 "secretsmanager:DescribeSecret",
1926 "secretsmanager:GetResourcePolicy",
1927 ]);
1928 const client = yield* HttpClient.HttpClient;
1929 const requests: Array<{
1930 action: string;
1931 blockPublicPolicy: boolean | undefined;
1932 secretId: string | undefined;
1933 }> = [];
1934 const observedClient = client.pipe(
1935 HttpClient.tapRequest((request) =>
1936 Effect.sync(() => {
1937 const target = request.headers["x-amz-target"];
1938 if (!target?.startsWith("secretsmanager.")) return;
1939 const body =
1940 request.body._tag === "Uint8Array"
1941 ? JSON.parse(new TextDecoder().decode(request.body.body))
1942 : {};
1943 requests.push({
1944 action: target.split(".").at(-1)!,
1945 blockPublicPolicy: body.BlockPublicPolicy,
1946 secretId: body.SecretId,
1947 });
1948 }),
1949 ),
1950 );
1951 const identifier = "alchemy-rds-secret-policy";
1952 const program = (
1953 masterUserSecretResourcePolicy: PolicyDocument | undefined,
1954 round = "default",
1955 ) =>
1956 Effect.gen(function* () {
1957 const net = yield* Network("SecretPolicyNet", {
1958 cidrBlock: "10.49.0.0/16",
1959 });
1960 const subnetGroup = yield* DBSubnetGroup("SecretPolicySubnetGroup", {
1961 description: "RDS managed secret policy lifecycle",
1962 subnetIds: net.privateSubnetIds,
1963 });
1964 return yield* DBInstance("SecretPolicyInstance", {
1965 dbInstanceIdentifier: identifier,
1966 engine: "postgres",
1967 dbInstanceClass: "db.t3.micro",
1968 masterUsername: "alchemy",
1969 manageMasterUserPassword: true,
1970 masterUserSecretResourcePolicy,
1971 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
1972 backupRetentionPeriod: "0 days",
1973 deletionProtection: false,
1974 skipFinalSnapshot: true,
1975 publiclyAccessible: false,
1976 tags: { round },
1977 });
1978 });
1979 const deploy = (desired: PolicyDocument | undefined, round = "default") =>
1980 stack
1981 .deploy(program(desired, round))
1982 .pipe(Effect.provideService(HttpClient.HttpClient, observedClient));
1983 const created = yield* deploy(initialPolicy);
1984 const secretArn = created.masterUserSecretArn;
1985 if (!secretArn)
1986 return yield* Effect.fail(new Error("RDS did not return its managed secret ARN"));
1987 const readPolicy = secretsmanager
1988 .getResourcePolicy({ SecretId: secretArn })
1989 .pipe(
1990 Effect.map((response) =>
1991 response.ResourcePolicy === undefined
1992 ? undefined
1993 : normalizePolicyDocument(response.ResourcePolicy),
1994 ),
1995 );
1996 const waitPolicy = (desired: PolicyDocument | undefined) =>
1997 readPolicy.pipe(
1998 Effect.repeat({
1999 schedule: Schedule.spaced("5 seconds"),
2000 times: 8,
2001 until: (actual) =>
2002 actual === (desired === undefined ? undefined : normalizePolicyDocument(desired)),
2003 }),
2004 );
2005 const injectPolicy = (desired: PolicyDocument) =>
2006 Effect.gen(function* () {
2007 yield* secretsmanager.putResourcePolicy({
2008 SecretId: secretArn,
2009 ResourcePolicy: JSON.stringify(desired),
2010 BlockPublicPolicy: true,
2011 });
2012 expect(yield* waitPolicy(desired)).toBe(normalizePolicyDocument(desired));
2013 });
2014 const metadata = yield* secretsmanager.describeSecret({
2015 SecretId: secretArn,
2016 });
2017 expect(metadata.OwningService).toBe("rds");
2018 expect(created.masterUserSecretResourcePolicy).toBe(normalizePolicyDocument(initialPolicy));
2019 expect(yield* readPolicy).toBe(normalizePolicyDocument(initialPolicy));
2020 expect(requests.filter((request) => request.action === "PutResourcePolicy")).toEqual([
2021 { action: "PutResourcePolicy", blockPublicPolicy: true, secretId: secretArn },
2022 ]);
2023 // ValidateResourcePolicy is authorized against the secret it names, so
2024 // once the secret exists every validation must name it: an identity
2025 // scoped to this secret's ARN is denied an untargeted (`*`) validation.
2026 // Only the first create, before RDS returns a secret, runs untargeted.
2027 const validatedSecretIds = (from: number) =>
2028 requests
2029 .slice(from)
2030 .filter((request) => request.action === "ValidateResourcePolicy")
2031 .map((request) => request.secretId);
2032 expect(validatedSecretIds(0).length).toBeGreaterThan(0);
2033 expect(validatedSecretIds(0).every((secretId) => secretId === undefined)).toBe(true);
2034 const beforePlan = requests.length;
2035 expect(
2036 (yield* stack
2037 .plan(program(initialPolicy))
2038 .pipe(Effect.provideService(HttpClient.HttpClient, observedClient))).resources
2039 .SecretPolicyInstance,
2040 ).toMatchObject({ action: "noop" });
2041 expect(validatedSecretIds(beforePlan).length).toBeGreaterThan(0);
2042 expect(validatedSecretIds(beforePlan).every((secretId) => secretId === secretArn)).toBe(true);
2043
2044 const beforeNoop = requests.length;
2045 const unchanged = yield* deploy(initialPolicy, "force-reconcile");
2046 expect(unchanged.dbInstanceArn).toBe(created.dbInstanceArn);
2047 expect(validatedSecretIds(beforeNoop).length).toBeGreaterThan(0);
2048 expect(validatedSecretIds(beforeNoop).every((secretId) => secretId === secretArn)).toBe(true);
2049 expect(
2050 requests
2051 .slice(beforeNoop)
2052 .filter((request) =>
2053 ["PutResourcePolicy", "DeleteResourcePolicy"].includes(request.action),
2054 ),
2055 ).toEqual([]);
2056
2057 const updated = yield* deploy(updatedPolicy);
2058 expect(updated.masterUserSecretArn).toBe(secretArn);
2059 expect(updated.masterUserSecretResourcePolicy).toBe(normalizePolicyDocument(updatedPolicy));
2060 expect(yield* readPolicy).toBe(normalizePolicyDocument(updatedPolicy));
2061
2062 yield* secretsmanager.deleteResourcePolicy({ SecretId: secretArn });
2063 expect(yield* waitPolicy(undefined)).toBeUndefined();
2064 expect(
2065 (yield* stack.plan(program(updatedPolicy))).resources.SecretPolicyInstance,
2066 ).toMatchObject({ action: "update" });
2067 yield* deploy(updatedPolicy);
2068 expect(yield* readPolicy).toBe(normalizePolicyDocument(updatedPolicy));
2069
2070 const removed = yield* deploy(undefined);
2071 expect(removed.masterUserSecretArn).toBe(secretArn);
2072 expect(removed.masterUserSecretResourcePolicy).toBeUndefined();
2073 expect(yield* readPolicy).toBeUndefined();
2074 expect((yield* stack.plan(program(undefined))).resources.SecretPolicyInstance).toMatchObject({
2075 action: "noop",
2076 });
2077
2078 yield* injectPolicy(initialPolicy);
2079 const drift = yield* Drift.detect({
2080 name: stack.name,
2081 stage: stack.stage,
2082 });
2083 expect(drift.resources.SecretPolicyInstance?.action).toBe("drifted");
2084 expect((yield* stack.plan(program(undefined))).resources.SecretPolicyInstance).toMatchObject({
2085 action: "update",
2086 });
2087 yield* deploy(undefined);
2088 expect(yield* readPolicy).toBeUndefined();
2089
2090 yield* injectPolicy(updatedPolicy);
2091 yield* Effect.gen(function* () {
2092 const state = yield* yield* State;
2093 yield* state.delete({
2094 stack: stack.name,
2095 stage: stack.stage,
2096 fqn: "SecretPolicyInstance",
2097 });
2098 }).pipe(Effect.provide(stack.state));
2099 const adopted = yield* deploy(undefined);
2100 expect(adopted.dbInstanceArn).toBe(created.dbInstanceArn);
2101 expect(adopted.masterUserSecretArn).toBe(secretArn);
2102 expect(yield* readPolicy).toBeUndefined();
2103
2104 const publicPolicy: PolicyDocument = {
2105 Version: "2012-10-17",
2106 Statement: [
2107 {
2108 Effect: "Allow",
2109 Principal: { AWS: "*" },
2110 Action: ["secretsmanager:GetSecretValue"],
2111 Resource: "*",
2112 },
2113 ],
2114 };
2115 const rejected = yield* deploy(publicPolicy).pipe(Effect.result);
2116 expect(Result.isFailure(rejected)).toBe(true);
2117 expect(renderFailure(rejected)).toContain("InvalidDBInstanceSecretPolicy");
2118 expect(yield* readPolicy).toBeUndefined();
2119 yield* deploy(undefined);
2120 const finalMetadata = yield* secretsmanager.describeSecret({
2121 SecretId: secretArn,
2122 });
2123 expect(finalMetadata.ARN).toBe(metadata.ARN);
2124 expect(finalMetadata.RotationEnabled).toBe(metadata.RotationEnabled);
2125 expect(finalMetadata.VersionIdsToStages).toEqual(metadata.VersionIdsToStages);
2126 expect(
2127 requests.some((request) =>
2128 [
2129 "GetSecretValue",
2130 "BatchGetSecretValue",
2131 "PutSecretValue",
2132 "UpdateSecret",
2133 "RotateSecret",
2134 "DeleteSecret",
2135 ].includes(request.action),
2136 ),
2137 ).toBe(false);
2138 expect(
2139 requests
2140 .filter((request) => request.action === "PutResourcePolicy")
2141 .every((request) => request.blockPublicPolicy),
2142 ).toBe(true);
2143 expect((yield* stack.plan(program(undefined))).resources.SecretPolicyInstance).toMatchObject({
2144 action: "noop",
2145 });
2146 yield* stack.destroy();
2147 yield* assertInstanceGone(identifier);
2148 expect(
2149 yield* secretsmanager.describeSecret({ SecretId: secretArn }).pipe(
2150 Effect.as(false),
2151 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
2152 Effect.repeat({
2153 schedule: Schedule.spaced("5 seconds"),
2154 times: 8,
2155 until: (gone) => gone,
2156 }),
2157 ),
2158 ).toBe(true);
2159 }),
2160 {
2161 tags: [
2162 "provider:aws",
2163 "provider:aws:ec2",
2164 "provider:aws:iam",
2165 "provider:aws:rds",
2166 "provider:aws:secretsmanager",
2167 "live",
2168 ],
2169 timeout: LONG_INSTANCE_TIMEOUT,
2170 },
2171 );
2172
2173 lifecycle(5, process.env.RDS_TEST_LIFECYCLE)(
2174 "managed secret policy: enable management on an existing instance",
2175 (stack) =>
2176 Effect.gen(function* () {
2177 yield* stack.destroy();
2178 const { accountId } = yield* AWSEnvironment.current;
2179 const policy: PolicyDocument = {
2180 Version: "2012-10-17",
2181 Statement: [
2182 {
2183 Effect: "Allow",
2184 Principal: { AWS: `arn:aws:iam::${accountId}:root` },
2185 Action: ["secretsmanager:DescribeSecret"],
2186 Resource: "*",
2187 },
2188 ],
2189 };
2190 const program = (managed: boolean, resourcePolicy?: PolicyDocument) =>
2191 Effect.gen(function* () {
2192 const net = yield* Network("EnableSecretNet", {
2193 cidrBlock: "10.50.0.0/16",
2194 });
2195 const subnetGroup = yield* DBSubnetGroup("EnableSecretSubnetGroup", {
2196 description: "Enable RDS managed credentials",
2197 subnetIds: net.privateSubnetIds,
2198 });
2199 return yield* DBInstance("EnableSecretInstance", {
2200 engine: "postgres",
2201 dbInstanceClass: "db.t3.micro",
2202 masterUsername: "alchemy",
2203 masterUserPassword: managed ? undefined : Redacted.make("Alchemy-secret-policy-42!"),
2204 manageMasterUserPassword: managed,
2205 masterUserSecretResourcePolicy: resourcePolicy,
2206 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
2207 backupRetentionPeriod: "0 days",
2208 deletionProtection: false,
2209 skipFinalSnapshot: true,
2210 publiclyAccessible: false,
2211 });
2212 });
2213 const invalid = yield* stack.deploy(program(false, policy)).pipe(Effect.result);
2214 expect(renderFailure(invalid)).toContain("InvalidDBInstanceSecretPolicy");
2215 const created = yield* stack.deploy(program(false));
2216 expect(created.masterUserSecretArn).toBeUndefined();
2217 expect(created.masterUserSecretResourcePolicy).toBeUndefined();
2218 const enabled = yield* stack.deploy(program(true, policy));
2219 expect(enabled.dbInstanceArn).toBe(created.dbInstanceArn);
2220 const secretArn = enabled.masterUserSecretArn;
2221 if (!secretArn) return yield* Effect.fail(new Error("RDS did not enable its managed secret"));
2222 expect(enabled.masterUserSecretResourcePolicy).toBe(normalizePolicyDocument(policy));
2223 expect(
2224 normalizePolicyDocument(
2225 (yield* secretsmanager.getResourcePolicy({ SecretId: secretArn })).ResourcePolicy!,
2226 ),
2227 ).toBe(normalizePolicyDocument(policy));
2228 const removed = yield* stack.deploy(program(true));
2229 expect(removed.masterUserSecretArn).toBe(secretArn);
2230 expect(
2231 (yield* secretsmanager.getResourcePolicy({ SecretId: secretArn })).ResourcePolicy,
2232 ).toBeUndefined();
2233 expect((yield* stack.plan(program(true))).resources.EnableSecretInstance).toMatchObject({
2234 action: "noop",
2235 });
2236 yield* stack.destroy();
2237 yield* assertInstanceGone(created.dbInstanceIdentifier);
2238 }),
2239 {
2240 tags: [
2241 "provider:aws",
2242 "provider:aws:ec2",
2243 "provider:aws:iam",
2244 "provider:aws:rds",
2245 "provider:aws:secretsmanager",
2246 "live",
2247 ],
2248 timeout: INSTANCE_TIMEOUT,
2249 },
2250 );
2251
2252 lifecycle(3, process.env.RDS_TEST_LIFECYCLE)(
2253 "readiness: pending creation, blocked stop, and restart recovery",
2254 (stack) =>
2255 Effect.gen(function* () {
2256 yield* stack.destroy();
2257 const client = yield* HttpClient.HttpClient;
2258 const observations: Array<{ action: string; statuses: string[] }> = [];
2259 const observedClient = client.pipe(
2260 HttpClient.transformResponse(
2261 Effect.flatMap((response) =>
2262 Effect.gen(function* () {
2263 const body = response.request.body;
2264 if (body._tag !== "Uint8Array") return response;
2265 const action = yield* Effect.sync(() =>
2266 new URLSearchParams(new TextDecoder().decode(body.body)).get("Action"),
2267 );
2268 if (action !== "DescribeDBInstances") return response;
2269 const bytes = yield* response.arrayBuffer;
2270 return yield* Effect.sync(() => {
2271 const text = new TextDecoder().decode(bytes);
2272 observations.push({
2273 action,
2274 statuses: [
2275 ...text.matchAll(/<DBInstanceStatus>([^<]+)<\/DBInstanceStatus>/g),
2276 ].map((match) => match[1]!),
2277 });
2278 // Forward the real response bytes unchanged to the SDK parser.
2279 return HttpClientResponse.fromWeb(
2280 response.request,
2281 new Response(bytes, {
2282 status: response.status,
2283 headers: response.headers,
2284 }),
2285 );
2286 });
2287 }),
2288 ),
2289 ),
2290 );
2291 const program = Effect.gen(function* () {
2292 const network = yield* Network("ReadinessNet", {
2293 cidrBlock: "10.48.0.0/16",
2294 });
2295 const subnetGroup = yield* DBSubnetGroup("ReadinessSubnetGroup", {
2296 description: "RDS readiness lifecycle",
2297 subnetIds: network.privateSubnetIds,
2298 });
2299 return yield* DBInstance("ReadinessInstance", {
2300 engine: "postgres",
2301 dbInstanceClass: "db.t3.micro",
2302 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
2303 masterUsername: "alchemy",
2304 manageMasterUserPassword: true,
2305 backupRetentionPeriod: "0 days",
2306 deletionProtection: false,
2307 skipFinalSnapshot: true,
2308 publiclyAccessible: false,
2309 });
2310 });
2311 const deploy = stack
2312 .deploy(program)
2313 .pipe(Effect.provideService(HttpClient.HttpClient, observedClient));
2314 const created = yield* deploy;
2315 const statuses = () => observations.flatMap((observation) => observation.statuses);
2316 expect(statuses()).toContain("creating");
2317 expect(statuses().at(-1)).toBe("available");
2318 expect(created.status).toBe("available");
2319 const identifier = created.dbInstanceIdentifier;
2320 const describe = rds.describeDBInstances({
2321 DBInstanceIdentifier: identifier,
2322 });
2323 const waitForStatus = (status: string) =>
2324 describe.pipe(
2325 Effect.repeat({
2326 schedule: Schedule.min([
2327 Schedule.exponential("5 seconds"),
2328 Schedule.spaced("1 minute"),
2329 ]),
2330 times: 10,
2331 until: (response) => response.DBInstances?.[0]?.DBInstanceStatus === status,
2332 }),
2333 );
2334 expect((yield* describe).DBInstances?.[0]?.DBInstanceStatus).toBe("available");
2335
2336 yield* rds.stopDBInstance({ DBInstanceIdentifier: identifier });
2337 const stopped = yield* waitForStatus("stopped");
2338 expect(stopped.DBInstances?.[0]?.DBInstanceStatus).toBe("stopped");
2339 expect((yield* stack.plan(program)).resources.ReadinessInstance).toMatchObject({
2340 action: "update",
2341 });
2342 const [elapsed, blocked] = yield* deploy.pipe(Effect.result, Effect.timed);
2343 expect(Result.isFailure(blocked)).toBe(true);
2344 expect(renderFailure(blocked)).toContain("DBInstanceReadinessBlocked");
2345 expect(renderFailure(blocked)).toContain("stopped");
2346 expect(Duration.toMillis(elapsed)).toBeLessThan(60_000);
2347
2348 yield* rds.startDBInstance({ DBInstanceIdentifier: identifier });
2349 const starting = yield* describe.pipe(
2350 Effect.repeat({
2351 schedule: Schedule.spaced("5 seconds"),
2352 times: 8,
2353 until: (response) =>
2354 ["starting", "available"].includes(response.DBInstances?.[0]?.DBInstanceStatus ?? ""),
2355 }),
2356 );
2357 expect(["starting", "available"]).toContain(starting.DBInstances?.[0]?.DBInstanceStatus);
2358 observations.length = 0;
2359 const started = yield* deploy;
2360 expect(statuses().at(-1)).toBe("available");
2361 expect(started.dbInstanceArn).toBe(created.dbInstanceArn);
2362 expect(started.status).toBe("available");
2363 expect((yield* describe).DBInstances?.[0]?.DBInstanceStatus).toBe("available");
2364 expect((yield* stack.plan(program)).resources.ReadinessInstance).toMatchObject({
2365 action: "noop",
2366 });
2367 yield* stack.destroy();
2368 yield* assertInstanceGone(identifier);
2369 }),
2370 {
2371 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
2372 timeout: INSTANCE_TIMEOUT,
2373 },
2374 );
2375
2376 // Fingerprint-guarded master password lifecycle (#876), gated behind
2377 // RDS_TEST_LIFECYCLE=1 (real db.t3.micro, ~15-25 min).
2378 //
2379 // AWS never returns the master password, so the provider fingerprints the
2380 // configured value (identifier-salted sha256, persisted `Redacted`) and only
2381 // sends `MasterUserPassword` on modify when the fingerprint changed. RDS
2382 // durably records a "Reset master credentials" event whenever a password
2383 // modify actually applies, which makes the guard observable out-of-band:
2384 //
2385 // 1. create with password P1 — set at create time, no reset event
2386 // 2. redeploy P1 with a tag change (forces reconcile) — fingerprint stable
2387 // 3. redeploy P2 — fingerprint changes; RDS records the credentials reset
2388 //
2389 // After step 3's event is observed, the reset-event count over the whole run
2390 // must be exactly 1 — the anchored positive event proves step 2's reconcile
2391 // did not re-send the unchanged password (pre-#876 every reconcile did,
2392 // putting the instance through a live `resetting-master-credentials` cycle).
2393 lifecycle(2, process.env.RDS_TEST_LIFECYCLE)(
2394 "master password: fingerprint guard skips unchanged, applies rotation",
2395 (stack) =>
2396 Effect.gen(function* () {
2397 yield* stack.destroy();
2398
2399 const identifier = "alchemy-rds-fingerprint";
2400 const startedAt = yield* Effect.sync(() => new Date());
2401
2402 // The testing account has no default VPC/subnets — provision a network
2403 // and DB subnet group like the standalone lifecycle test above.
2404 const network = Effect.gen(function* () {
2405 const net = yield* Network("FingerprintNet", {
2406 cidrBlock: "10.42.0.0/16",
2407 });
2408 const subnetGroup = yield* DBSubnetGroup("FingerprintSubnetGroup", {
2409 description: "alchemy master-password fingerprint lifecycle",
2410 subnetIds: net.privateSubnetIds,
2411 });
2412 return { dbSubnetGroupName: subnetGroup.dbSubnetGroupName };
2413 });
2414
2415 const deployInstance = (password: string, round: string) =>
2416 stack.deploy(
2417 Effect.gen(function* () {
2418 const { dbSubnetGroupName } = yield* network;
2419 return yield* DBInstance("FingerprintInstance", {
2420 dbInstanceIdentifier: identifier,
2421 engine: "postgres",
2422 dbInstanceClass: "db.t3.micro",
2423 allocatedStorage: 20,
2424 masterUsername: "alchemy",
2425 masterUserPassword: Redacted.make(password),
2426 deletionProtection: false,
2427 dbSubnetGroupName,
2428 publiclyAccessible: false,
2429 // A changed tag guarantees the engine sees a props diff and
2430 // runs `reconcile` — the exact path that used to re-send the
2431 // unchanged password.
2432 tags: { round },
2433 });
2434 }),
2435 );
2436
2437 const resetEvents = rds
2438 .describeEvents({
2439 SourceIdentifier: identifier,
2440 SourceType: "db-instance",
2441 StartTime: startedAt,
2442 })
2443 .pipe(
2444 Effect.map((response) =>
2445 (response.Events ?? []).filter((event) =>
2446 /reset master credentials/i.test(event.Message ?? ""),
2447 ),
2448 ),
2449 );
2450
2451 const created = yield* deployInstance("FingerprintPass1", "one");
2452 const createdFingerprint = created.masterUserPasswordFingerprint;
2453 expect(createdFingerprint).toBeDefined();
2454 // sha256 hex digest — never the password itself.
2455 expect(Redacted.value(createdFingerprint!)).toMatch(/^[0-9a-f]{64}$/);
2456
2457 // Same password, tag-only change → reconcile runs but must skip the
2458 // `MasterUserPassword` modify (same fingerprint).
2459 const unchanged = yield* deployInstance("FingerprintPass1", "two");
2460 expect(unchanged.dbInstanceArn).toBe(created.dbInstanceArn);
2461 expect(Redacted.value(unchanged.masterUserPasswordFingerprint!)).toBe(
2462 Redacted.value(createdFingerprint!),
2463 );
2464
2465 // Rotation: new password → new fingerprint, and RDS applies a real
2466 // master-credentials reset.
2467 const rotated = yield* deployInstance("FingerprintPass2", "three");
2468 expect(rotated.dbInstanceArn).toBe(created.dbInstanceArn);
2469 expect(Redacted.value(rotated.masterUserPasswordFingerprint!)).not.toBe(
2470 Redacted.value(createdFingerprint!),
2471 );
2472
2473 // The reset event lands when the modify applies; poll bounded for it,
2474 // then assert the count over the whole run is exactly 1 — proving
2475 // round "two" (unchanged password) never triggered a reset.
2476 const events = yield* resetEvents.pipe(
2477 Effect.repeat({
2478 schedule: Schedule.min([Schedule.exponential("5 seconds"), Schedule.spaced("1 minute")]),
2479 until: (found) => found.length > 0,
2480 times: 10,
2481 }),
2482 );
2483 expect(events).toHaveLength(1);
2484
2485 yield* stack.destroy();
2486 }),
2487 {
2488 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
2489 timeout: INSTANCE_TIMEOUT,
2490 },
2491 );
2492
2493 // Regression for #2317: a deploy whose core modify resets the master
2494 // password and also enables IAM authentication. RDS keeps reporting
2495 // `available` for a few seconds after accepting the reset, so the follow-up
2496 // IAM modify used to reach a busy instance and fail with
2497 // `InvalidDBInstanceStateFault`.
2498 lifecycle(7, process.env.RDS_TEST_LIFECYCLE)(
2499 "master password: a password reset followed by an IAM change in one deploy",
2500 (stack) =>
2501 Effect.gen(function* () {
2502 yield* stack.destroy();
2503
2504 const startedAt = yield* Effect.sync(() => new Date());
2505 const deployInstance = (password: string, iam: boolean) =>
2506 stack.deploy(
2507 Effect.gen(function* () {
2508 const net = yield* Network("ResetIamNet", {
2509 cidrBlock: "10.43.0.0/16",
2510 });
2511 const subnetGroup = yield* DBSubnetGroup("ResetIamSubnetGroup", {
2512 description: "alchemy password reset + IAM lifecycle",
2513 subnetIds: net.privateSubnetIds,
2514 });
2515 return yield* DBInstance("ResetIamInstance", {
2516 engine: "postgres",
2517 dbInstanceClass: "db.t3.micro",
2518 allocatedStorage: 20,
2519 masterUsername: "alchemy",
2520 masterUserPassword: Redacted.make(password),
2521 enableIAMDatabaseAuthentication: iam,
2522 deletionProtection: false,
2523 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
2524 publiclyAccessible: false,
2525 });
2526 }),
2527 );
2528
2529 const created = yield* deployInstance("ResetIamPass1", false);
2530 // The race is a few seconds wide, so give it several chances: each
2531 // round resets the password and flips IAM authentication together.
2532 const rounds = [
2533 { password: "ResetIamPass2", iam: true },
2534 { password: "ResetIamPass3", iam: false },
2535 { password: "ResetIamPass4", iam: true },
2536 ];
2537 for (const { password, iam } of rounds) {
2538 const updated = yield* deployInstance(password, iam);
2539 expect(updated.dbInstanceArn).toBe(created.dbInstanceArn);
2540 const instance = (yield* rds.describeDBInstances({
2541 DBInstanceIdentifier: updated.dbInstanceIdentifier,
2542 })).DBInstances?.[0];
2543 expect(instance?.IAMDatabaseAuthenticationEnabled).toBe(iam);
2544 }
2545
2546 // Every password reset really applied, so each IAM modify followed one.
2547 const resets = yield* rds
2548 .describeEvents({
2549 SourceIdentifier: created.dbInstanceIdentifier,
2550 SourceType: "db-instance",
2551 StartTime: startedAt,
2552 })
2553 .pipe(
2554 Effect.map((response) =>
2555 (response.Events ?? []).filter((event) =>
2556 /reset master credentials/i.test(event.Message ?? ""),
2557 ),
2558 ),
2559 Effect.repeat({
2560 schedule: Schedule.min([
2561 Schedule.exponential("5 seconds"),
2562 Schedule.spaced("1 minute"),
2563 ]),
2564 until: (found) => found.length >= rounds.length,
2565 times: 10,
2566 }),
2567 );
2568 expect(resets).toHaveLength(rounds.length);
2569
2570 yield* stack.destroy();
2571 }),
2572 {
2573 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
2574 timeout: INSTANCE_TIMEOUT,
2575 },
2576 );
2577
2578 // Both guards fire while the stack is built, before any cloud call.
2579 test.provider(
2580 "Aurora rejects the Data API transport without the Data API, and non-Aurora engines",
2581 (stack) =>
2582 Effect.gen(function* () {
2583 const rejection = (props: Partial<AuroraProps>) =>
2584 Effect.gen(function* () {
2585 const exit = yield* Effect.exit(
2586 stack.deploy(
2587 Aurora("RejectedMigrationsDb", {
2588 subnetIds: [],
2589 securityGroupIds: [],
2590 migrations: "./migrations",
2591 ...props,
2592 }),
2593 ),
2594 );
2595 expect(Exit.isFailure(exit)).toBe(true);
2596 return Exit.isFailure(exit) ? Cause.pretty(exit.cause) : "";
2597 });
2598
2599 yield* stack.destroy();
2600 expect(yield* rejection({ dataApi: false, migrationsTransport: "data-api" })).toContain(
2601 'migrationsTransport "data-api" requires "dataApi" to be enabled',
2602 );
2603 expect(yield* rejection({ engine: "postgres" })).toContain(
2604 '"migrations" support the "aurora-postgresql" and "aurora-mysql" engines',
2605 );
2606 yield* stack.destroy();
2607 }),
2608 { tags: ["provider:aws", "provider:aws:rds", "live"] },
2609 );
2610
2611 // An Aurora cluster plus writer takes ~15+ minutes to create and delete,
2612 // far beyond the default test budget. The fixture SQL is valid in both
2613 // dialects, so each engine runs the same case in its own lane.
2614 for (const { lane, engine, cidrBlock } of [
2615 { lane: 7, engine: "aurora-postgresql", cidrBlock: "10.53.0.0/16" },
2616 { lane: 8, engine: "aurora-mysql", cidrBlock: "10.54.0.0/16" },
2617 ]) {
2618 lifecycle(lane, process.env.AWS_TEST_RDS_DBINSTANCE)(
2619 `Aurora writer applies migrations over the Data API, then only pending ones (${engine})`,
2620 (stack) =>
2621 Effect.gen(function* () {
2622 const fs = yield* FileSystem.FileSystem;
2623 const path = yield* Path.Path;
2624 const migrationsDir = yield* fs.makeTempDirectoryScoped({
2625 prefix: "alchemy-aurora-migrations-",
2626 });
2627 yield* fs.writeFileString(
2628 path.join(migrationsDir, "0001_users.sql"),
2629 "CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL);",
2630 );
2631 const deployDb = stack.deploy(
2632 Effect.gen(function* () {
2633 const net = yield* Network("MigrationsNet", { cidrBlock });
2634 const securityGroup = yield* SecurityGroup("MigrationsDbSecurityGroup", {
2635 vpcId: net.vpcId,
2636 description: "alchemy Aurora migrations test",
2637 });
2638 const key = yield* Key("MigrationsDbKey", {
2639 description: "alchemy Aurora migrations test",
2640 });
2641 // `kmsKeyId` and `migrations` take Outputs (#2363).
2642 const db = yield* Aurora("MigrationsDb", {
2643 engine,
2644 subnetIds: net.privateSubnetIds,
2645 securityGroupIds: [securityGroup.groupId],
2646 storageEncrypted: true,
2647 kmsKeyId: key.keyArn,
2648 migrations: Output.asOutput(migrationsDir),
2649 });
2650 return {
2651 keyArn: key.keyArn,
2652 clusterArn: db.cluster.dbClusterArn,
2653 secretArn: db.secret.secretArn,
2654 writer: db.writer,
2655 };
2656 }),
2657 );
2658
2659 yield* stack.destroy();
2660
2661 const created = yield* deployDb;
2662 expect(created.writer.migrationsTable).toEqual("__alchemy_migrations");
2663 expect(Object.keys(created.writer.migrationsHashes)).toEqual(["0001_users.sql"]);
2664
2665 yield* fs.writeFileString(
2666 path.join(migrationsDir, "0002_posts.sql"),
2667 "CREATE TABLE posts (id SERIAL PRIMARY KEY, title TEXT NOT NULL);",
2668 );
2669 const updated = yield* deployDb;
2670 expect(updated.clusterArn).toEqual(created.clusterArn);
2671 expect(updated.writer.dbiResourceId).toEqual(created.writer.dbiResourceId);
2672 expect(Object.keys(updated.writer.migrationsHashes).sort()).toEqual([
2673 "0001_users.sql",
2674 "0002_posts.sql",
2675 ]);
2676
2677 const cluster = (yield* rds.describeDBClusters({
2678 DBClusterIdentifier: created.clusterArn,
2679 })).DBClusters?.[0];
2680 expect(cluster?.KmsKeyId).toEqual(created.keyArn);
2681
2682 // Out-of-band: the bookkeeping rows via the Data API.
2683 const applied = yield* rdsdata.executeStatement({
2684 resourceArn: created.clusterArn,
2685 secretArn: created.secretArn,
2686 database: "app",
2687 sql: "SELECT name FROM __alchemy_migrations ORDER BY id;",
2688 formatRecordsAs: "JSON",
2689 });
2690 expect(JSON.parse(applied.formattedRecords ?? "[]")).toEqual([
2691 { name: "0001_users.sql" },
2692 { name: "0002_posts.sql" },
2693 ]);
2694
2695 yield* stack.destroy();
2696 }),
2697 {
2698 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
2699 timeout: LONG_INSTANCE_TIMEOUT,
2700 },
2701 );
2702 }
2703
2704 // Writes `0001_users.sql` into a fresh temp directory; the returned `add`
2705 // writes later migrations into the same directory between deploys.
2706 const migrationsFixture = Effect.gen(function* () {
2707 const fs = yield* FileSystem.FileSystem;
2708 const path = yield* Path.Path;
2709 const dir = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-rds-migrations-" });
2710 const add = (name: string, sql: string) => fs.writeFileString(path.join(dir, name), sql);
2711 yield* add("0001_users.sql", "CREATE TABLE users (id INT PRIMARY KEY, name TEXT NOT NULL);");
2712 return { dir, add };
2713 });
2714
2715 const readSecretCredentials = Effect.fn(function* (secretArn: string) {
2716 const value = yield* secretsmanager.getSecretValue({ SecretId: secretArn });
2717 const secretString =
2718 typeof value.SecretString === "string"
2719 ? value.SecretString
2720 : Redacted.value(value.SecretString!);
2721 return JSON.parse(secretString) as { username: string; password: string };
2722 });
2723
2724 // A public PostgreSQL instance reachable only from this machine's address.
2725 lifecycle(4, process.env.AWS_TEST_RDS_DBINSTANCE)(
2726 "standalone PostgreSQL applies migrations directly from the deploying machine",
2727 (stack) =>
2728 Effect.gen(function* () {
2729 const migrations = yield* migrationsFixture;
2730 const ip = yield* HttpClient.get("https://checkip.amazonaws.com").pipe(
2731 Effect.flatMap((response) => response.text),
2732 Effect.map((text) => text.trim()),
2733 );
2734 const deployDb = stack.deploy(
2735 Effect.gen(function* () {
2736 const net = yield* Network("DirectMigrationsNet", { cidrBlock: "10.55.0.0/16" });
2737 const subnetGroup = yield* DBSubnetGroup("DirectMigrationsSubnets", {
2738 description: "alchemy direct migrations test",
2739 subnetIds: net.publicSubnetIds,
2740 });
2741 const securityGroup = yield* SecurityGroup("DirectMigrationsSg", {
2742 vpcId: net.vpcId,
2743 description: "alchemy direct migrations test",
2744 ingress: [{ ipProtocol: "tcp", fromPort: 5432, toPort: 5432, cidrIpv4: `${ip}/32` }],
2745 });
2746 return yield* DBInstance("DirectMigrationsDb", {
2747 engine: "postgres",
2748 dbInstanceClass: "db.t4g.micro",
2749 allocatedStorage: 20,
2750 dbName: "app",
2751 masterUsername: "app",
2752 manageMasterUserPassword: true,
2753 publiclyAccessible: true,
2754 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
2755 vpcSecurityGroupIds: [securityGroup.groupId],
2756 migrations: migrations.dir,
2757 migrationsTransport: "direct",
2758 });
2759 }),
2760 );
2761
2762 yield* stack.destroy();
2763
2764 const created = yield* deployDb;
2765 expect(Object.keys(created.migrationsHashes)).toEqual(["0001_users.sql"]);
2766
2767 yield* migrations.add(
2768 "0002_posts.sql",
2769 "CREATE TABLE posts (id INT PRIMARY KEY, title TEXT NOT NULL);",
2770 );
2771 const updated = yield* deployDb;
2772 expect(updated.dbiResourceId).toEqual(created.dbiResourceId);
2773 expect(Object.keys(updated.migrationsHashes).sort()).toEqual([
2774 "0001_users.sql",
2775 "0002_posts.sql",
2776 ]);
2777
2778 // Out-of-band: read the bookkeeping rows over our own connection.
2779 const credentials = yield* readSecretCredentials(updated.masterUserSecretArn!);
2780 const rows = yield* Effect.gen(function* () {
2781 const sql = yield* PgClient.PgClient;
2782 return yield* sql`SELECT name FROM __alchemy_migrations ORDER BY id`;
2783 }).pipe(
2784 Effect.provide(
2785 PgClient.layer({
2786 host: updated.endpointAddress,
2787 port: updated.endpointPort,
2788 database: "app",
2789 username: credentials.username,
2790 password: Redacted.make(credentials.password),
2791 ssl: { rejectUnauthorized: false },
2792 }),
2793 ),
2794 );
2795 expect(rows).toEqual([{ name: "0001_users.sql" }, { name: "0002_posts.sql" }]);
2796
2797 yield* stack.destroy();
2798 }),
2799 {
2800 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:rds", "live"],
2801 timeout: LONG_INSTANCE_TIMEOUT,
2802 },
2803 );
2804
2805 // A private MySQL instance only the in-VPC runner can reach.
2806 lifecycle(5, process.env.AWS_TEST_RDS_DBINSTANCE)(
2807 "private MySQL applies migrations through an in-VPC MigrationRunner",
2808 (stack) =>
2809 Effect.gen(function* () {
2810 const migrations = yield* migrationsFixture;
2811 const deployDb = stack.deploy(
2812 Effect.gen(function* () {
2813 const net = yield* Network("RunnerMigrationsNet", { cidrBlock: "10.56.0.0/16" });
2814 const subnetGroup = yield* DBSubnetGroup("RunnerMigrationsSubnets", {
2815 description: "alchemy runner migrations test",
2816 subnetIds: net.privateSubnetIds,
2817 });
2818 const runnerSecurityGroup = yield* SecurityGroup("RunnerMigrationsRunnerSg", {
2819 vpcId: net.vpcId,
2820 description: "alchemy migration runner",
2821 });
2822 const databaseSecurityGroup = yield* SecurityGroup("RunnerMigrationsDbSg", {
2823 vpcId: net.vpcId,
2824 description: "alchemy runner migrations database",
2825 ingress: [
2826 {
2827 ipProtocol: "tcp",
2828 fromPort: 3306,
2829 toPort: 3306,
2830 referencedGroupId: runnerSecurityGroup.groupId,
2831 },
2832 ],
2833 });
2834 const runner = yield* MigrationRunner("RunnerMigrationsRunner", {
2835 subnetIds: net.privateSubnetIds,
2836 securityGroupIds: [runnerSecurityGroup.groupId],
2837 });
2838 const db = yield* DBInstance("RunnerMigrationsDb", {
2839 engine: "mysql",
2840 dbInstanceClass: "db.t4g.micro",
2841 allocatedStorage: 20,
2842 dbName: "app",
2843 masterUsername: "app",
2844 manageMasterUserPassword: true,
2845 publiclyAccessible: false,
2846 dbSubnetGroupName: subnetGroup.dbSubnetGroupName,
2847 vpcSecurityGroupIds: [databaseSecurityGroup.groupId],
2848 migrations: migrations.dir,
2849 migrationsTransport: runner,
2850 });
2851 return { db, runnerArn: runner.functionArn };
2852 }),
2853 );
2854
2855 yield* stack.destroy();
2856
2857 const created = yield* deployDb;
2858 expect(Object.keys(created.db.migrationsHashes)).toEqual(["0001_users.sql"]);
2859
2860 yield* migrations.add(
2861 "0002_posts.sql",
2862 "CREATE TABLE posts (id INT PRIMARY KEY, title TEXT NOT NULL);",
2863 );
2864 const updated = yield* deployDb;
2865 expect(updated.db.dbiResourceId).toEqual(created.db.dbiResourceId);
2866 expect(Object.keys(updated.db.migrationsHashes).sort()).toEqual([
2867 "0001_users.sql",
2868 "0002_posts.sql",
2869 ]);
2870
2871 // Out-of-band: the database is private, so query it through the runner.
2872 const credentials = yield* readSecretCredentials(updated.db.masterUserSecretArn!);
2873 const response = yield* Lambda.invoke({
2874 FunctionName: updated.runnerArn,
2875 Payload: JSON.stringify({
2876 op: "query",
2877 target: {
2878 dialect: "mysql",
2879 host: updated.db.endpointAddress,
2880 port: updated.db.endpointPort,
2881 database: "app",
2882 ...credentials,
2883 },
2884 sql: "SELECT name FROM __alchemy_migrations ORDER BY id;",
2885 }),
2886 });
2887 const body = yield* response.Payload!.pipe(Stream.decodeText(), Stream.mkString);
2888 expect(JSON.parse(body)).toEqual({
2889 ok: true,
2890 rows: [{ name: "0001_users.sql" }, { name: "0002_posts.sql" }],
2891 });
2892
2893 yield* stack.destroy();
2894 }),
2895 {
2896 tags: ["provider:aws", "provider:aws:ec2", "provider:aws:lambda", "provider:aws:rds", "live"],
2897 timeout: LONG_INSTANCE_TIMEOUT,
2898 },
2899 );
2900
2901 describe.concurrent("lifecycle lanes", () => {
2902 lanes.forEach((cases, index) =>
2903 describe(`lane ${index + 1}`, () => {
2904 for (const { enabled, args } of cases) {
2905 test.provider.skipIf(!enabled)(...args);
2906 }
2907 }),
2908 );
2909 });