grada-run 0.49.5 → 0.49.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "grada-run",
3
- "version": "0.49.5",
3
+ "version": "0.49.6",
4
4
  "description": "Provision production-ready AWS infrastructure and CI/CD pipelines in seconds.",
5
5
  "engines": {
6
6
  "node": ">=18.0.0"
@@ -111,6 +111,7 @@
111
111
  "@aws-sdk/client-elastic-load-balancing-v2": "3.1119.0",
112
112
  "@aws-sdk/client-iam": "3.1119.0",
113
113
  "@aws-sdk/client-rds": "3.1119.0",
114
+ "@aws-sdk/client-route-53": "3.1119.0",
114
115
  "@aws-sdk/client-s3": "3.1115.0",
115
116
  "@aws-sdk/client-secrets-manager": "3.1112.0",
116
117
  "@aws-sdk/client-sqs": "3.1119.0",
@@ -9,7 +9,7 @@ import { failCommand, isProgrammaticCall, shouldAutoApprove, refuseIfNonInteract
9
9
  import { parseFlags, normalizeOptions, normalizeArgv } from '../utils/args.js';
10
10
  import { spawnSync } from 'child_process';
11
11
  import { runTerraformCommand, getTerraformOutputs, readLastRunMarker, isInterruptedMarker, ensureTerraformInit, checkOutputsContract, DEFAULT_TERRAFORM_PHASE_TIMEOUT_MS, PROGRESS_MODES } from '../utils/terraform.js';
12
- import { classifyTerraformError, countRecreateCollisions, getRunbook, ERROR_CODES } from '../utils/terraform-errors.js';
12
+ import { classifyTerraformError, countRecreateCollisions, getRunbook, ERROR_CODES, parseCollisionImports } from '../utils/terraform-errors.js';
13
13
  import { readInitManifest, recordAppliedTarget } from '../utils/backup.js';
14
14
  import { requireManagedTree, parseStateBucketBackend, upsertAutoTfvar } from '../utils/terraform-metadata.js';
15
15
  import { readTerraformComputeTarget, resolveRegion, readRenderedContainerEnvNames } from '../utils/resolvers.js';
@@ -797,12 +797,54 @@ async function applyStackMain(input = {}) {
797
797
  });
798
798
  return failCommand({ exitCode: 1 });
799
799
  }
800
+ // Guided import first: stderr names the collided resource
801
+ // and (for most families) its provider id — import in-flow
802
+ // instead of hand-typing. Secrets are excluded (import needs
803
+ // the ARN, not derivable from stderr); declined, failed, or
804
+ // unparseable markers fall through to the manual runbook.
805
+ let collisionImportError = null;
806
+ if (options._collisionImported !== true) {
807
+ const pairs = parseCollisionImports(errString)
808
+ .filter((pair) => !pair.address.startsWith('aws_secretsmanager_secret.'));
809
+ if (pairs.length > 0) {
810
+ const refused = await refuseIfNonInteractive(options, {
811
+ message: '\n✖ apply needs approval to import collided resources into state.',
812
+ hint: ' Re-run with --auto-approve to import non-interactively.\n',
813
+ event: 'infrastructure_applied',
814
+ telemetry: { projectName: actualProjectName },
815
+ errorCode: 'COLLISION_IMPORT_NON_INTERACTIVE',
816
+ });
817
+ if (refused) return refused;
818
+ let proceed = shouldAutoApprove(options);
819
+ if (!proceed) {
820
+ const confirmed = await confirm({
821
+ message: `Import ${pairs.length} collided resource${pairs.length === 1 ? '' : 's'} into state and retry?`,
822
+ initialValue: true,
823
+ });
824
+ proceed = !isCancel(confirmed) && confirmed === true;
825
+ }
826
+ if (proceed) {
827
+ try {
828
+ for (const pair of pairs) {
829
+ await runTerraform(['import', pair.address, pair.id], tfDir, null, `Importing ${pair.address}`);
830
+ }
831
+ } catch (error) {
832
+ collisionImportError = error;
833
+ }
834
+ if (collisionImportError === null) {
835
+ trackEvent('collision_resources_imported', { projectName: actualProjectName, count: pairs.length });
836
+ log.message(color.cyan('Imported collided resources — retrying apply...'));
837
+ return applyStack({ ...options, _skipPreview: true, _collisionImported: true });
838
+ }
839
+ }
840
+ }
841
+ }
800
842
  trackEvent('bucket_recovery_probed', { projectName: actualProjectName, collisions });
801
843
  await flushTelemetry();
802
844
  const runbook = getRunbook('RECREATE_COLLISION', { headless: shouldAutoApprove(options) });
803
845
  return failCommand({
804
846
  message: `\n✖ ${runbook.message}`,
805
- hint: ` ${runbook.hint}\n Import each address it names, e.g.:\n ${color.green('cd terraform && terraform import <address> <aws-id>')}\n`,
847
+ hint: ` ${runbook.hint}\n Import each address it names, e.g.:\n ${color.green('cd terraform && terraform import <address> <aws-id>')}\n${collisionImportError ? color.dim(` (Automatic import failed: ${collisionImportError.message})`) + '\n' : ''}`,
806
848
  event: 'infrastructure_applied',
807
849
  telemetry: { projectName: actualProjectName, error_code: 'RECREATE_COLLISION', ...costProps },
808
850
  errorCode: 'RECREATE_COLLISION',
@@ -862,14 +904,51 @@ async function applyStackMain(input = {}) {
862
904
  if (adoptUpsertError) {
863
905
  log.message(color.dim(` (Automatic adoption failed: ${adoptUpsertError.message})`));
864
906
  }
865
- } else if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE') {
907
+ } else if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE' || code === 'SECRET_PENDING_DELETION') {
866
908
  // Named but flow-neutral: runbook guidance with a coded
867
909
  // failure. (RECREATE_COLLISION has its dedicated branch
868
910
  // above; deletion protection's destroy flow lives in
869
911
  // destroy.js.)
912
+ let reconfigureError = null;
913
+ if (code === 'BACKEND_RECONFIGURE_REQUIRED' && options._backendReconfigured !== true) {
914
+ // Offer the reconfigure in-flow instead of shelling the
915
+ // user out to raw terraform; declined or failed runs
916
+ // fall through to the runbook below.
917
+ const refused = await refuseIfNonInteractive(options, {
918
+ message: '\n✖ apply needs approval to reconfigure the Terraform backend.',
919
+ hint: ' Re-run with --auto-approve to reconfigure non-interactively.\n',
920
+ event: 'infrastructure_applied',
921
+ telemetry: { projectName: actualProjectName },
922
+ errorCode: 'BACKEND_RECONFIGURE_NON_INTERACTIVE',
923
+ });
924
+ if (refused) return refused;
925
+ let proceed = shouldAutoApprove(options);
926
+ if (!proceed) {
927
+ const confirmed = await confirm({
928
+ message: 'Reconfigure the Terraform backend (init -reconfigure) and retry?',
929
+ initialValue: true,
930
+ });
931
+ proceed = !isCancel(confirmed) && confirmed === true;
932
+ }
933
+ if (proceed) {
934
+ try {
935
+ await runTerraform(['init', '-reconfigure'], tfDir, null, 'Reconfiguring backend');
936
+ } catch (error) {
937
+ reconfigureError = error;
938
+ }
939
+ if (reconfigureError === null) {
940
+ trackEvent('backend_reconfigured', { projectName: actualProjectName });
941
+ log.message(color.cyan('Reconfigured the backend — retrying apply...'));
942
+ return applyStack({ ...options, _skipPreview: true, _backendReconfigured: true });
943
+ }
944
+ }
945
+ }
870
946
  const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId });
871
947
  log.error(color.red(`✖ ${runbook.message}`));
872
948
  log.message(` ${runbook.hint}\n`);
949
+ if (reconfigureError) {
950
+ log.message(color.dim(` (Automatic reconfigure failed: ${reconfigureError.message})`));
951
+ }
873
952
  log.message(color.dim(errString.split('\n').slice(0, 8).join('\n')));
874
953
  } else {
875
954
  // Generic fallback for unknown errors
@@ -1,9 +1,10 @@
1
1
  import fsSync from 'fs';
2
2
  import path from 'path';
3
- import { intro, outro, confirm, spinner, cancel } from '@clack/prompts';
3
+ import { intro, outro, confirm, spinner, cancel, isCancel } from '@clack/prompts';
4
4
  import color from 'picocolors';
5
5
  import { RDSClient, DescribeDBInstancesCommand, DescribeDBClustersCommand, DescribeDBSnapshotsCommand, DescribeDBClusterSnapshotsCommand, StartDBInstanceCommand, StartDBClusterCommand, CreateDBSnapshotCommand, CreateDBClusterSnapshotCommand, ModifyDBInstanceCommand, ModifyDBClusterCommand } from '@aws-sdk/client-rds';
6
6
  import { S3Client, ListObjectVersionsCommand } from '@aws-sdk/client-s3';
7
+ import { EC2Client, DescribeVpcsCommand, DescribeNetworkInterfacesCommand, DescribeSecurityGroupsCommand, DeleteSecurityGroupCommand } from '@aws-sdk/client-ec2';
7
8
  import { teardownStateBucket, drainBucketObjects, resolveClient } from '../utils/aws.js';
8
9
  import { checkDependency, pollUntil, sleep } from '../utils/system.js';
9
10
  import { trackEvent, flushTelemetry, trackSuccess, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
@@ -14,6 +15,7 @@ import { findDbTarget, dbStatusOf, resolveDbIdentifier, resolveDbClusterIdentifi
14
15
  import { requireManagedTree, parseStateBucketBackend } from '../utils/terraform-metadata.js';
15
16
  import { guardSleepingEnv } from '../utils/sleep-state.js';
16
17
  import { normalizeOptions, parseFlags, normalizeArgv } from '../utils/args.js';
18
+ import { resolveHeadless } from '../utils/resolvers.js';
17
19
  import { truncateSpinnerText } from '../utils/spinner-text.js';
18
20
 
19
21
  export const DEFAULT_DESTROY_DB_TIMEOUT_MS = 600000;
@@ -57,7 +59,37 @@ export function parseDestroyArgs(argv = []) {
57
59
  // Returns `{ retried: true }` when the destroy recovered so the caller
58
60
  // continues to bucket cleanup, else `{ retried: false, result }`.
59
61
  // Exported for unit tests: every branch must honor this contract.
60
- export async function handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error }) {
62
+ async function describeDependencyBlockers(ec2, projectName) {
63
+ const { Vpcs = [] } = await ec2.send(new DescribeVpcsCommand({
64
+ Filters: [
65
+ { Name: 'tag:ManagedBy', Values: ['grada'] },
66
+ { Name: 'tag:GradaProject', Values: [projectName] },
67
+ ],
68
+ }));
69
+ if (Vpcs.length !== 1 || !Vpcs[0].VpcId) return null;
70
+ const vpcId = Vpcs[0].VpcId;
71
+ const [niResp, sgResp] = await Promise.all([
72
+ ec2.send(new DescribeNetworkInterfacesCommand({ Filters: [{ Name: 'vpc-id', Values: [vpcId] }] })),
73
+ ec2.send(new DescribeSecurityGroupsCommand({ Filters: [{ Name: 'vpc-id', Values: [vpcId] }] })),
74
+ ]);
75
+ const owned = (tags = []) => tags.some((tag) => tag.Key === 'ManagedBy' && tag.Value === 'grada')
76
+ && tags.some((tag) => tag.Key === 'GradaProject' && tag.Value === projectName);
77
+ return {
78
+ vpcId,
79
+ enis: (niResp.NetworkInterfaces || []).map((ni) => ({
80
+ id: ni.NetworkInterfaceId,
81
+ detail: ni.Attachment?.InstanceId || ni.Description || ni.InterfaceType || 'attached',
82
+ owned: owned(ni.TagSet),
83
+ })),
84
+ sgs: (sgResp.SecurityGroups || []).map((sg) => ({
85
+ id: sg.GroupId,
86
+ detail: sg.GroupName || '',
87
+ owned: owned(sg.Tags),
88
+ })),
89
+ };
90
+ }
91
+
92
+ export async function handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error, region }) {
61
93
  const actualProjectName = path.basename(process.cwd());
62
94
  const errString = String(error.message || error);
63
95
  const structuredCode = typeof error.code === 'string' && ERROR_CODES.includes(error.code) ? error.code : null;
@@ -138,6 +170,68 @@ export async function handleDestroyTerraformError({ options, tfDirPath, attemptD
138
170
  return { retried: true };
139
171
  }
140
172
 
173
+ if (code === 'DESTROY_DEPENDENCY_VIOLATION') {
174
+ // Enumerate the blockers (read-only, always safe) so the failure
175
+ // names attachments instead of gesturing at the console.
176
+ // Grada-owned security groups can go in-flow; ENIs and foreign
177
+ // attachments are report-only — detaching live interfaces is
178
+ // never automatic. Any discovery failure falls through to the
179
+ // plain runbook below.
180
+ let blockers = null;
181
+ let ec2 = null;
182
+ try {
183
+ ec2 = resolveClient(options.ec2Client, EC2Client, { region: region ?? process.env.AWS_REGION ?? 'us-east-2' });
184
+ blockers = await describeDependencyBlockers(ec2, actualProjectName);
185
+ } catch {
186
+ blockers = null;
187
+ }
188
+ if (blockers !== null) {
189
+ const ownedSgs = blockers.sgs.filter((sg) => sg.owned);
190
+ if (ownedSgs.length > 0) {
191
+ let proceed = shouldAutoApprove(options);
192
+ if (!proceed && !resolveHeadless(normalizeOptions(options))) {
193
+ const confirmed = await confirm({
194
+ message: `Delete ${ownedSgs.length} grada-owned security group${ownedSgs.length === 1 ? '' : 's'} blocking VPC deletion and retry?`,
195
+ initialValue: true,
196
+ });
197
+ proceed = !isCancel(confirmed) && confirmed === true;
198
+ }
199
+ if (proceed) {
200
+ try {
201
+ for (const sg of ownedSgs) {
202
+ await ec2.send(new DeleteSecurityGroupCommand({ GroupId: sg.id }));
203
+ }
204
+ } catch {
205
+ return namedFail('DESTROY_DEPENDENCY_VIOLATION');
206
+ }
207
+ trackEvent('dependency_blockers_removed', { projectName: actualProjectName, count: ownedSgs.length });
208
+ try {
209
+ await attemptDestroy();
210
+ } catch {
211
+ return namedFail('DESTROY_DEPENDENCY_VIOLATION');
212
+ }
213
+ return { retried: true };
214
+ }
215
+ }
216
+ const lines = [
217
+ ...blockers.enis.map((ni) => ` ENI ${ni.id} (${ni.detail})${ni.owned ? '' : ' — foreign, remove by hand'}`),
218
+ ...blockers.sgs.map((sg) => ` SG ${sg.id} (${sg.detail})${sg.owned ? '' : ' — foreign, remove by hand'}`),
219
+ ];
220
+ const runbook = getRunbook('DESTROY_DEPENDENCY_VIOLATION', { headless: shouldAutoApprove(options), lockId });
221
+ return {
222
+ retried: false,
223
+ result: failCommand({
224
+ message: `\n✖ VPC deletion blocked by ${blockers.enis.length + blockers.sgs.length} attachment(s) in ${blockers.vpcId}:\n${lines.join('\n')}\n ${runbook.hint}\n`,
225
+ useErrorStream: true,
226
+ event: 'infrastructure_destroyed',
227
+ telemetry: { projectName: actualProjectName, error_code: 'DESTROY_DEPENDENCY_VIOLATION' },
228
+ errorCode: 'DESTROY_DEPENDENCY_VIOLATION',
229
+ reason: 'destroy-dependency-violation',
230
+ }),
231
+ };
232
+ }
233
+ }
234
+
141
235
  if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'DESTROY_DEPENDENCY_VIOLATION'
142
236
  || code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE') {
143
237
  return namedFail(code);
@@ -280,6 +374,18 @@ async function destroyStackMain(input = {}) {
280
374
  if (refused) return refused;
281
375
  }
282
376
 
377
+ // Secrets enter the 30-day recovery window on destroy: re-applying
378
+ // inside the window fails until the secret is restored or
379
+ // force-deleted (see the apply runbook). Warn in both modes —
380
+ // informational only, the confirm below still gates.
381
+ try {
382
+ if (fsSync.existsSync(path.join(tfDirPath, 'secrets.tf'))) {
383
+ console.warn(color.yellow('⚠ Vault secrets enter a 30-day recovery window on destroy — re-applying within 30 days needs a restore or force-delete first.'));
384
+ }
385
+ } catch {
386
+ // Unreadable tree: the confirm below still gates.
387
+ }
388
+
283
389
  const proceed = shouldAutoApprove(options) ? true : await confirm({
284
390
  message: dbNeedsProtection
285
391
  ? color.red(`⚠️ WARNING: This will permanently destroy all AWS resources including database ${dbTarget.id} (a pre-destroy snapshot will be taken first). Are you absolutely sure?`)
@@ -308,7 +414,7 @@ async function destroyStackMain(input = {}) {
308
414
  try {
309
415
  await runInit();
310
416
  } catch (error) {
311
- const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy: runInit, error });
417
+ const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy: runInit, error, region });
312
418
  if (!handled.retried) return handled.result;
313
419
  }
314
420
 
@@ -672,7 +778,7 @@ async function destroyStackMain(input = {}) {
672
778
  s.stop('AWS compute resources destroyed.');
673
779
  } catch (error) {
674
780
  s.stop(color.red('❌ Terraform destroy failed.'));
675
- const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error });
781
+ const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error, region });
676
782
  if (!handled.retried) return handled.result;
677
783
  }
678
784
 
@@ -1,6 +1,6 @@
1
1
  import { spawnSync } from 'child_process';
2
2
  import { ECSClient, ListTasksCommand, DescribeTasksCommand } from '@aws-sdk/client-ecs';
3
- import { CloudWatchLogsClient } from '@aws-sdk/client-cloudwatch-logs';
3
+ import { CloudWatchLogsClient, DescribeLogGroupsCommand } from '@aws-sdk/client-cloudwatch-logs';
4
4
  import color from 'picocolors';
5
5
  import { intro, outro, spinner } from '@clack/prompts';
6
6
  import { trackEvent, flushTelemetry, trackSuccess, trackFailure, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
@@ -86,7 +86,7 @@ function isLogGroupNotFoundError(error) {
86
86
  // Shared log tail for the ECS and Lambda paths: fetched lines, the honest
87
87
  // fetch-failure warning (never "no events" when the fetch failed), the
88
88
  // missing-group hint, or the genuine empty line — in that order.
89
- function printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchError, listPrefix, region }) {
89
+ async function printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchError, listPrefix, region, logsClient }) {
90
90
  if (logs.length > 0) {
91
91
  console.log(`\n ${color.bold(`Last ${logs.length} log lines (${color.cyan(logGroup)}):`)}`);
92
92
  for (const line of logs) {
@@ -96,6 +96,19 @@ function printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchEr
96
96
  console.log(color.yellow(`\n⚠ Could not fetch recent logs from ${logGroup} (${fetchError}). Log health is unverified.`));
97
97
  } else if (logGroupMissing) {
98
98
  console.log(color.yellow(`\n⚠ Log group not found (expected ${logGroup}).`));
99
+ try {
100
+ const resp = await logsClient.send(new DescribeLogGroupsCommand({ logGroupNamePrefix: listPrefix, limit: 10 }));
101
+ const names = (resp?.logGroups || []).map((group) => group.logGroupName).filter(Boolean).slice(0, 10);
102
+ if (names.length > 0) {
103
+ console.log(color.dim('Matching groups:'));
104
+ for (const name of names) console.log(color.dim(` ${name}`));
105
+ } else {
106
+ console.log(color.dim(`No log groups match "${listPrefix}".`));
107
+ }
108
+ return;
109
+ } catch {
110
+ // Listing failed: fall back to the CLI pointer below.
111
+ }
99
112
  console.log(color.dim(`List matching groups with: aws logs describe-log-groups --log-group-name-prefix "${listPrefix}" --region ${region}`));
100
113
  } else {
101
114
  console.log(color.dim(`\n No recent log events found in ${logGroup}.`));
@@ -215,7 +228,7 @@ export async function runLambdaDiagnose(input = {}) {
215
228
  console.log(` ${color.dim('State:')} ${state === 'Active' ? color.green(state) : color.yellow(state)} ${color.dim(`/ ${lastUpdateStatus}`)}`);
216
229
  if (configuration.LastModified) console.log(` ${color.dim('Modified:')} ${configuration.LastModified}`);
217
230
 
218
- printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchError, listPrefix: '/aws/lambda/', region });
231
+ await printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchError, listPrefix: '/aws/lambda/', region, logsClient });
219
232
 
220
233
  if (healthy) {
221
234
  outro(color.green('Diagnose complete. Nothing to fix!'));
@@ -489,7 +502,7 @@ async function runDiagnoseMain(input = {}) {
489
502
  console.log(` ${color.dim('Container reason:')} ${color.yellow(containerReason)}`);
490
503
  }
491
504
 
492
- printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchError, listPrefix: '/ecs/', region });
505
+ await printLogTail({ logGroup, logs, logGroupMissing, logFetchFailed, fetchError, listPrefix: '/ecs/', region, logsClient });
493
506
 
494
507
  outro(color.green('Diagnose complete. Fix the error above, then redeploy. 🚀'));
495
508
 
@@ -2,12 +2,14 @@ import fsSync from 'fs';
2
2
  import fs from 'fs/promises';
3
3
  import path from 'path';
4
4
  import color from 'picocolors';
5
- import { intro, outro, confirm, cancel, isCancel } from '@clack/prompts';
5
+ import { intro, outro, confirm, cancel, isCancel, select } from '@clack/prompts';
6
6
  import { trackSuccess, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
7
7
  import { resolveProjectName, resolveHeadless, resolveCwd } from '../utils/resolvers.js';
8
8
  import { parseFlags, normalizeOptions, normalizeArgv } from '../utils/args.js';
9
9
  import { failCommand, failProjectNotInitialized, isProgrammaticCall, refuseIfNonInteractive, refuseUnknownFlags } from '../utils/command.js';
10
10
  import { getTerraformOutputs } from '../utils/terraform.js';
11
+ import { Route53Client, ListHostedZonesCommand } from '@aws-sdk/client-route-53';
12
+ import { resolveClient } from '../utils/aws.js';
11
13
  import {
12
14
  findResourceBlock,
13
15
  findNestedBlock,
@@ -435,7 +437,7 @@ async function runDomainMain(input = {}) {
435
437
  return failCommand({
436
438
  noExit,
437
439
  message: `\n✖ Invalid Route 53 zone ID "${options.zoneId}".`,
438
- hint: ' Zone IDs look like Z1234567890ABC (find yours with: aws route53 list-hosted-zones).\n',
440
+ hint: ' Zone IDs look like Z1234567890ABC (re-run without --zone-id to pick from your hosted zones, or find yours with: aws route53 list-hosted-zones).\n',
439
441
  event: 'domain_run',
440
442
  telemetry: { subcommand, error_code: 'INVALID_ZONE_ID' },
441
443
  reason: 'invalid-zone-id',
@@ -483,6 +485,42 @@ async function runDomainMain(input = {}) {
483
485
  }, { scope: 'terraform' });
484
486
  }
485
487
 
488
+ // Hosted-zone discovery for `add` without --zone-id: lists zones
489
+ // matching the domain (apex or parent) and lets the user pick one.
490
+ // Returns the picked zone id, or null for external DNS, no match,
491
+ // cancel, or any lookup failure (all fall back to external mode).
492
+ async function discoverHostedZoneId({ route53Client, region, domain }) {
493
+ let zones;
494
+ try {
495
+ const client = resolveClient(route53Client, Route53Client, { region });
496
+ const resp = await client.send(new ListHostedZonesCommand({ MaxItems: 100 }));
497
+ zones = resp?.HostedZones || [];
498
+ } catch {
499
+ return null;
500
+ }
501
+ const bare = (name) => String(name || '').replace(/\.$/, '').toLowerCase();
502
+ const matches = zones
503
+ .filter((zone) => {
504
+ const name = bare(zone.Name);
505
+ const target = domain.toLowerCase();
506
+ return name !== '' && (target === name || target.endsWith(`.${name}`));
507
+ })
508
+ .sort((a, b) => String(b.Name).length - String(a.Name).length);
509
+ if (matches.length === 0) return null;
510
+ const picked = await select({
511
+ message: `Pick the Route 53 hosted zone for ${domain}:`,
512
+ options: [
513
+ ...matches.map((zone) => {
514
+ const id = normalizeZoneId(zone.Id) || zone.Id;
515
+ return { value: id, label: `${zone.Name} (${id}${zone.Config?.PrivateZone ? ', private' : ''})` };
516
+ }),
517
+ { value: null, label: 'External DNS instead' },
518
+ ],
519
+ });
520
+ if (isCancel(picked) || !picked) return null;
521
+ return picked;
522
+ }
523
+
486
524
  async function runDomainAdd({ options, projectName, domain, zoneId, domainTfPath, cloudfrontTfPath }) {
487
525
  const noExit = isProgrammaticCall(options);
488
526
  const force = options.force === true;
@@ -509,6 +547,15 @@ async function runDomainAdd({ options, projectName, domain, zoneId, domainTfPath
509
547
  }
510
548
  }
511
549
 
550
+ if (!zoneId && !resolveHeadless(options)) {
551
+ const discovered = await discoverHostedZoneId({
552
+ route53Client: options.route53Client,
553
+ region: options.region ?? process.env.AWS_REGION ?? 'us-east-2',
554
+ domain,
555
+ });
556
+ if (discovered) zoneId = discovered;
557
+ }
558
+
512
559
  const mode = zoneId ? 'route53' : (activate ? 'external-active' : 'external-pending');
513
560
  let content;
514
561
  if (mode === 'route53') {
@@ -1,4 +1,4 @@
1
- import { CloudWatchLogsClient, FilterLogEventsCommand, DescribeLogStreamsCommand } from '@aws-sdk/client-cloudwatch-logs';
1
+ import { CloudWatchLogsClient, FilterLogEventsCommand, DescribeLogStreamsCommand, DescribeLogGroupsCommand } from '@aws-sdk/client-cloudwatch-logs';
2
2
  import color from 'picocolors';
3
3
  import { trackEvent, flushTelemetry, trackSuccess, trackFailure, setActiveCommandName, resetActiveCommandName } from '../core/telemetry.js';
4
4
  import { parseFlags, normalizeOptions, normalizeArgv } from '../utils/args.js';
@@ -167,9 +167,22 @@ export function buildLogStreamName(containerName, taskId, prefix = 'ecs') {
167
167
  return `${prefix}/${containerName}/${taskId}`;
168
168
  }
169
169
 
170
- function printMissingLogGroupGuidance(logGroup, service, region) {
170
+ async function printMissingLogGroupGuidance(logGroup, service, region, logsClient) {
171
171
  const prefix = `/${String(logGroup).split('/').filter(Boolean).slice(0, 2).join('/')}/`;
172
172
  console.log(color.yellow(`\n⚠ No log group found for "${service}" (expected ${logGroup}).`));
173
+ try {
174
+ const resp = await logsClient.send(new DescribeLogGroupsCommand({ logGroupNamePrefix: prefix, limit: 10 }));
175
+ const names = (resp?.logGroups || []).map((group) => group.logGroupName).filter(Boolean).slice(0, 10);
176
+ if (names.length > 0) {
177
+ console.log(color.dim('Matching groups:'));
178
+ for (const name of names) console.log(color.dim(` ${name}`));
179
+ } else {
180
+ console.log(color.dim(`No log groups match "${prefix}".`));
181
+ }
182
+ return;
183
+ } catch {
184
+ // Listing failed: fall back to the CLI pointer below.
185
+ }
173
186
  console.log(color.dim(`List matching groups with: aws logs describe-log-groups --log-group-name-prefix "${prefix}" --region ${region}`));
174
187
  }
175
188
 
@@ -268,7 +281,7 @@ async function runLogsMain(input = {}) {
268
281
  } catch (verifyError) {
269
282
  if (isAuthError(verifyError)) throw verifyError;
270
283
  if (isNotFoundError(verifyError)) {
271
- printMissingLogGroupGuidance(logGroup, service, region);
284
+ await printMissingLogGroupGuidance(logGroup, service, region, logsClient);
272
285
  return { logs: [], logGroup, region, service };
273
286
  }
274
287
  // Any other verification failure is non-fatal: the fetch below is authoritative.
@@ -344,7 +357,7 @@ async function runLogsMain(input = {}) {
344
357
  return { logs: [], logGroup, region, service };
345
358
  }
346
359
  if (isNotFoundError(error)) {
347
- printMissingLogGroupGuidance(logGroup, service, region);
360
+ await printMissingLogGroupGuidance(logGroup, service, region, logsClient);
348
361
  return { logs: [], logGroup, region, service };
349
362
  }
350
363
 
@@ -63,6 +63,21 @@ export async function discoverEligibleRevisions(ecsClient, family, currentRevNum
63
63
  return discoverWithAdapter(createEcsRevisionAdapter(ecsClient), family, currentRevNum);
64
64
  }
65
65
 
66
+ // Failure-path hint naming the revisions the user can actually pick,
67
+ // listed in-flow through the same seam Case B selects from. Falls back
68
+ // to the CLI pointer when discovery itself fails.
69
+ async function eligibleRevisionsHint(ecsClient, family, currentRevNum, region) {
70
+ const fallback = 'List recent revisions with: aws ecs list-task-definitions --family-prefix ' + family + ` --region ${region}\n`;
71
+ try {
72
+ const eligible = await discoverEligibleRevisions(ecsClient, family, currentRevNum);
73
+ if (eligible.length === 0) return fallback;
74
+ const revs = eligible.slice(0, 5).map((arn) => String(arn).split(':').pop());
75
+ return `Eligible revisions (newest first): ${revs.join(', ')}\n`;
76
+ } catch {
77
+ return fallback;
78
+ }
79
+ }
80
+
66
81
  // Programmatic entry wrapper: stamps cli_command for telemetry on every
67
82
  // invocation path, including direct imports that bypass bin/cli.js and MCP.
68
83
  export async function runRollback(input = {}) {
@@ -195,7 +210,7 @@ async function runRollbackMain(input = {}) {
195
210
  s.stop(color.red('Revision not found.'));
196
211
  return failCommand({
197
212
  message: `\n✖ Task definition "${targetInput}" was not found in family "${family}".`,
198
- hint: 'List recent revisions with: aws ecs list-task-definitions --family-prefix ' + family + ` --region ${region}\n`,
213
+ hint: await eligibleRevisionsHint(ecsClient, family, currentRevNum, region),
199
214
  event: 'rollback_run',
200
215
  telemetry: { projectName, error_code: 'REVISION_NOT_FOUND' },
201
216
  reason: 'revision-not-found',
@@ -212,7 +227,7 @@ async function runRollbackMain(input = {}) {
212
227
  s.stop(color.red('Foreign task definition family.'));
213
228
  return failCommand({
214
229
  message: `\n✖ Task definition "${targetInput}" belongs to family "${descResp.taskDefinition.family ?? 'unknown'}", not "${family}". Rollback stays within the service's own family.`,
215
- hint: 'List recent revisions with: aws ecs list-task-definitions --family-prefix ' + family + ` --region ${region}\n`,
230
+ hint: await eligibleRevisionsHint(ecsClient, family, currentRevNum, region),
216
231
  event: 'rollback_run',
217
232
  telemetry: { projectName, error_code: 'REVISION_FAMILY_MISMATCH' },
218
233
  reason: 'revision-family-mismatch',
@@ -368,7 +368,7 @@ async function handleSecretsCatch({
368
368
  // "Failed to push" lies about what succeeded.
369
369
  const postVaultHints = {
370
370
  'secret_keys.json write': ' Re-run the push to regenerate terraform/secret_keys.json — the vault already holds the new values.\n',
371
- 'ECS restart': ' Trigger the restart by hand (aws ecs update-service --force-new-deployment) or deploy — the vault already holds the new values.\n',
371
+ 'ECS restart': ' Re-run the push to retry the restart (or trigger it by hand: aws ecs update-service --force-new-deployment) — the vault already holds the new values.\n',
372
372
  };
373
373
  const failureMessage = postVaultStep === null
374
374
  ? `❌ Failed to ${verb} secrets: ${error.message}`
@@ -13,6 +13,7 @@ export const ERROR_CODES = [
13
13
  'OIDC_PROVIDER_CONFLICT',
14
14
  'CLOUDFRONT_IN_PROGRESS',
15
15
  'BUCKET_NOT_EMPTY',
16
+ 'SECRET_PENDING_DELETION',
16
17
  ];
17
18
 
18
19
  // AWS AlreadyExists family: every create-path "reality has what state
@@ -34,6 +35,40 @@ export function countRecreateCollisions(stderr) {
34
35
  return stderr.match(new RegExp(RECREATE_COLLISION_RE.source, 'gi'))?.length ?? 0;
35
36
  }
36
37
 
38
+ const COLLISION_NAME_RE = /creating [A-Za-z][A-Za-z0-9 ]*?\(([^)]+)\)/;
39
+ const COLLISION_WITH_RE = /^\s*[│|]?\s*with ([A-Za-z0-9_."[\]-]+),/;
40
+ const COLLISION_ERROR_RE = /^\s*[╷╵│]?\s*Error:/;
41
+
42
+ // Guided-import pairs for recreate collisions: each AlreadyExists-style
43
+ // marker carries the provider name in parens, and the resource address
44
+ // follows on a `with <addr>,` context line. Markers without either are
45
+ // skipped — the caller falls back to the manual runbook. Capped so a
46
+ // pathological stderr cannot queue unbounded imports.
47
+ export function parseCollisionImports(stderr, limit = 20) {
48
+ if (typeof stderr !== 'string' || stderr === '') return [];
49
+ const lines = stderr.split('\n');
50
+ const pairs = [];
51
+ const seen = new Set();
52
+ for (let i = 0; i < lines.length && pairs.length < limit; i++) {
53
+ if (!RECREATE_COLLISION_RE.test(lines[i])) continue;
54
+ const nameMatch = COLLISION_NAME_RE.exec(lines[i]);
55
+ if (!nameMatch) continue;
56
+ for (let j = i + 1; j < Math.min(i + 9, lines.length); j++) {
57
+ if (COLLISION_ERROR_RE.test(lines[j])) break;
58
+ const withMatch = COLLISION_WITH_RE.exec(lines[j]);
59
+ if (withMatch) {
60
+ const address = withMatch[1].replace(/"/g, '');
61
+ if (!seen.has(address)) {
62
+ seen.add(address);
63
+ pairs.push({ address, id: nameMatch[1] });
64
+ }
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ return pairs;
70
+ }
71
+
37
72
  // Pure classifier over stderr text. Returns `{ code, lockId }` or null.
38
73
  export function classifyTerraformError(stderr, ctx = {}) {
39
74
  const text = typeof stderr === 'string' ? stderr : '';
@@ -49,6 +84,13 @@ export function classifyTerraformError(stderr, ctx = {}) {
49
84
  if (RECREATE_COLLISION_RE.test(text)) {
50
85
  return { code: 'RECREATE_COLLISION', lockId: null };
51
86
  }
87
+ // Re-apply inside the Secrets Manager recovery window: the committed
88
+ // suffix reuses the secret name while deletion is still pending.
89
+ // Disjoint from the collision family ("already scheduled for
90
+ // deletion" carries no AlreadyExists marker).
91
+ if (/scheduled for deletion/i.test(text)) {
92
+ return { code: 'SECRET_PENDING_DELETION', lockId: null };
93
+ }
52
94
  if (/NoSuchBucket/i.test(text) || /does not exist/i.test(text)) {
53
95
  return { code: 'STATE_BUCKET_MISSING', lockId: null };
54
96
  }
@@ -116,6 +158,11 @@ const RUNBOOKS = {
116
158
  headless: 'Re-run destroy — drained buckets usually read empty within a minute; if it persists, empty the bucket by hand in the AWS Console.',
117
159
  interactive: 'Re-run destroy — drained buckets usually read empty within a minute; if it persists, empty the bucket by hand in the AWS Console.',
118
160
  },
161
+ SECRET_PENDING_DELETION: {
162
+ message: 'A Secrets Manager secret is scheduled for deletion.',
163
+ headless: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import <address> <id>) or force-delete without recovery and re-apply + push — grada never force-deletes, the window protects against accidental loss.',
164
+ interactive: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import <address> <id>) or force-delete without recovery and re-apply + push — grada never force-deletes, the window protects against accidental loss.',
165
+ },
119
166
  };
120
167
 
121
168
  // Returns `{ message, hint }` for a named code, or null for unknown
@@ -38,7 +38,7 @@ This infrastructure provisions a highly available Application Load Balancer (ALB
38
38
  ```
39
39
 
40
40
  3. **Automated CI/CD:**
41
- Push this repository to GitHub. Your deployment pipeline uses the official [Grada GitHub Action](https://github.com/marketplace/actions/deploy-stack-aws-fargate-terraform-deploy). Every push to `{{DEPLOY_BRANCH}}` will automatically run your infrastructure changes, build your container, and deploy your application.
41
+ Push this repository to GitHub. Your deployment pipeline uses the official [Grada GitHub Action](https://github.com/marketplace/actions/deploy-stack-aws-fargate-terraform-deploy). Every push to `{{DEPLOY_BRANCH}}` will automatically build your container, run your infrastructure changes, and deploy your application.
42
42
 
43
43
  ### 📟 Day-2 Operations
44
44
  Once live, manage the stack without opening the AWS console:
@@ -63,7 +63,7 @@ Run the automated teardown command from the root of your project:
63
63
  ```bash
64
64
  npx grada-run destroy
65
65
  ```
66
- *Type `yes` when prompted. This will execute a safe Terraform teardown of your Load Balancer, ECS cluster, and networking components. You will then be asked whether to also empty and delete your remote S3 state bucket (kept by default, so you can re-provision later with `npx grada-run apply`).*
66
+ *Confirm when prompted. This will execute a safe Terraform teardown of your Load Balancer, ECS cluster, and networking components. You will then be asked whether to also empty and delete your remote S3 state bucket (kept by default, so you can re-provision later with `npx grada-run apply`).*
67
67
 
68
68
  ## ⚠️ Critical Application Prerequisites
69
69