grada-run 0.49.5 → 0.49.7

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.7",
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';
@@ -708,6 +708,7 @@ async function applyStackMain(input = {}) {
708
708
  const classified = classifyTerraformError(errString, { postRecovery: options._postRecovery === true });
709
709
  const code = structuredCode || classified?.code || null;
710
710
  const lockId = error.lockId || classified?.lockId || null;
711
+ const secretName = classified?.secretName || null;
711
712
 
712
713
  if (code === 'STATE_BUCKET_MISSING' && options._postRecovery === true) {
713
714
  // The bucket is still missing after a recreate: refuse instead
@@ -797,12 +798,54 @@ async function applyStackMain(input = {}) {
797
798
  });
798
799
  return failCommand({ exitCode: 1 });
799
800
  }
801
+ // Guided import first: stderr names the collided resource
802
+ // and (for most families) its provider id — import in-flow
803
+ // instead of hand-typing. Secrets are excluded (import needs
804
+ // the ARN, not derivable from stderr); declined, failed, or
805
+ // unparseable markers fall through to the manual runbook.
806
+ let collisionImportError = null;
807
+ if (options._collisionImported !== true) {
808
+ const pairs = parseCollisionImports(errString)
809
+ .filter((pair) => !pair.address.startsWith('aws_secretsmanager_secret.'));
810
+ if (pairs.length > 0) {
811
+ const refused = await refuseIfNonInteractive(options, {
812
+ message: '\n✖ apply needs approval to import collided resources into state.',
813
+ hint: ' Re-run with --auto-approve to import non-interactively.\n',
814
+ event: 'infrastructure_applied',
815
+ telemetry: { projectName: actualProjectName },
816
+ errorCode: 'COLLISION_IMPORT_NON_INTERACTIVE',
817
+ });
818
+ if (refused) return refused;
819
+ let proceed = shouldAutoApprove(options);
820
+ if (!proceed) {
821
+ const confirmed = await confirm({
822
+ message: `Import ${pairs.length} collided resource${pairs.length === 1 ? '' : 's'} into state and retry?`,
823
+ initialValue: true,
824
+ });
825
+ proceed = !isCancel(confirmed) && confirmed === true;
826
+ }
827
+ if (proceed) {
828
+ try {
829
+ for (const pair of pairs) {
830
+ await runTerraform(['import', pair.address, pair.id], tfDir, null, `Importing ${pair.address}`);
831
+ }
832
+ } catch (error) {
833
+ collisionImportError = error;
834
+ }
835
+ if (collisionImportError === null) {
836
+ trackEvent('collision_resources_imported', { projectName: actualProjectName, count: pairs.length });
837
+ log.message(color.cyan('Imported collided resources — retrying apply...'));
838
+ return applyStack({ ...options, _skipPreview: true, _collisionImported: true });
839
+ }
840
+ }
841
+ }
842
+ }
800
843
  trackEvent('bucket_recovery_probed', { projectName: actualProjectName, collisions });
801
844
  await flushTelemetry();
802
845
  const runbook = getRunbook('RECREATE_COLLISION', { headless: shouldAutoApprove(options) });
803
846
  return failCommand({
804
847
  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`,
848
+ 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
849
  event: 'infrastructure_applied',
807
850
  telemetry: { projectName: actualProjectName, error_code: 'RECREATE_COLLISION', ...costProps },
808
851
  errorCode: 'RECREATE_COLLISION',
@@ -862,14 +905,51 @@ async function applyStackMain(input = {}) {
862
905
  if (adoptUpsertError) {
863
906
  log.message(color.dim(` (Automatic adoption failed: ${adoptUpsertError.message})`));
864
907
  }
865
- } else if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE') {
908
+ } else if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE' || code === 'SECRET_PENDING_DELETION') {
866
909
  // Named but flow-neutral: runbook guidance with a coded
867
910
  // failure. (RECREATE_COLLISION has its dedicated branch
868
911
  // above; deletion protection's destroy flow lives in
869
912
  // destroy.js.)
870
- const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId });
913
+ let reconfigureError = null;
914
+ if (code === 'BACKEND_RECONFIGURE_REQUIRED' && options._backendReconfigured !== true) {
915
+ // Offer the reconfigure in-flow instead of shelling the
916
+ // user out to raw terraform; declined or failed runs
917
+ // fall through to the runbook below.
918
+ const refused = await refuseIfNonInteractive(options, {
919
+ message: '\n✖ apply needs approval to reconfigure the Terraform backend.',
920
+ hint: ' Re-run with --auto-approve to reconfigure non-interactively.\n',
921
+ event: 'infrastructure_applied',
922
+ telemetry: { projectName: actualProjectName },
923
+ errorCode: 'BACKEND_RECONFIGURE_NON_INTERACTIVE',
924
+ });
925
+ if (refused) return refused;
926
+ let proceed = shouldAutoApprove(options);
927
+ if (!proceed) {
928
+ const confirmed = await confirm({
929
+ message: 'Reconfigure the Terraform backend (init -reconfigure) and retry?',
930
+ initialValue: true,
931
+ });
932
+ proceed = !isCancel(confirmed) && confirmed === true;
933
+ }
934
+ if (proceed) {
935
+ try {
936
+ await runTerraform(['init', '-reconfigure'], tfDir, null, 'Reconfiguring backend');
937
+ } catch (error) {
938
+ reconfigureError = error;
939
+ }
940
+ if (reconfigureError === null) {
941
+ trackEvent('backend_reconfigured', { projectName: actualProjectName });
942
+ log.message(color.cyan('Reconfigured the backend — retrying apply...'));
943
+ return applyStack({ ...options, _skipPreview: true, _backendReconfigured: true });
944
+ }
945
+ }
946
+ }
947
+ const runbook = getRunbook(code, { headless: shouldAutoApprove(options), lockId, secretName });
871
948
  log.error(color.red(`✖ ${runbook.message}`));
872
949
  log.message(` ${runbook.hint}\n`);
950
+ if (reconfigureError) {
951
+ log.message(color.dim(` (Automatic reconfigure failed: ${reconfigureError.message})`));
952
+ }
873
953
  log.message(color.dim(errString.split('\n').slice(0, 8).join('\n')));
874
954
  } else {
875
955
  // 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, readTerraformProjectName, readResourceSuffix } 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,42 @@ 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, vpcName) {
63
+ // Preview workspaces share the GradaProject tag, so the project
64
+ // filter alone matches every preview VPC too — the exact prod
65
+ // Name narrows to the destroy target. Without a readable tree
66
+ // there is no Name, and the single-VPC length check below keeps
67
+ // the legacy behavior.
68
+ const filters = [
69
+ { Name: 'tag:ManagedBy', Values: ['grada'] },
70
+ { Name: 'tag:GradaProject', Values: [projectName] },
71
+ ];
72
+ if (vpcName) filters.push({ Name: 'tag:Name', Values: [vpcName] });
73
+ const { Vpcs = [] } = await ec2.send(new DescribeVpcsCommand({ Filters: filters }));
74
+ if (Vpcs.length !== 1 || !Vpcs[0].VpcId) return null;
75
+ const vpcId = Vpcs[0].VpcId;
76
+ const [niResp, sgResp] = await Promise.all([
77
+ ec2.send(new DescribeNetworkInterfacesCommand({ Filters: [{ Name: 'vpc-id', Values: [vpcId] }] })),
78
+ ec2.send(new DescribeSecurityGroupsCommand({ Filters: [{ Name: 'vpc-id', Values: [vpcId] }] })),
79
+ ]);
80
+ const owned = (tags = []) => tags.some((tag) => tag.Key === 'ManagedBy' && tag.Value === 'grada')
81
+ && tags.some((tag) => tag.Key === 'GradaProject' && tag.Value === projectName);
82
+ return {
83
+ vpcId,
84
+ enis: (niResp.NetworkInterfaces || []).map((ni) => ({
85
+ id: ni.NetworkInterfaceId,
86
+ detail: ni.Attachment?.InstanceId || ni.Description || ni.InterfaceType || 'attached',
87
+ owned: owned(ni.TagSet),
88
+ })),
89
+ sgs: (sgResp.SecurityGroups || []).map((sg) => ({
90
+ id: sg.GroupId,
91
+ detail: sg.GroupName || '',
92
+ owned: owned(sg.Tags),
93
+ })),
94
+ };
95
+ }
96
+
97
+ export async function handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error, region }) {
61
98
  const actualProjectName = path.basename(process.cwd());
62
99
  const errString = String(error.message || error);
63
100
  const structuredCode = typeof error.code === 'string' && ERROR_CODES.includes(error.code) ? error.code : null;
@@ -138,6 +175,82 @@ export async function handleDestroyTerraformError({ options, tfDirPath, attemptD
138
175
  return { retried: true };
139
176
  }
140
177
 
178
+ if (code === 'DESTROY_DEPENDENCY_VIOLATION') {
179
+ // Enumerate the blockers (read-only, always safe) so the failure
180
+ // names attachments instead of gesturing at the console.
181
+ // Grada-owned security groups can go in-flow; ENIs and foreign
182
+ // attachments are report-only — detaching live interfaces is
183
+ // never automatic. Any discovery failure falls through to the
184
+ // plain runbook below.
185
+ let blockers = null;
186
+ let ec2 = null;
187
+ try {
188
+ ec2 = resolveClient(options.ec2Client, EC2Client, { region: region ?? process.env.AWS_REGION ?? 'us-east-2' });
189
+ // Rendered names beat the directory basename: the TF
190
+ // project + suffix pin the exact prod VPC Name, and
191
+ // unreadable trees fall back to basename matching.
192
+ let tagProject = actualProjectName;
193
+ let vpcName = null;
194
+ try {
195
+ const tfProject = readTerraformProjectName();
196
+ if (tfProject) {
197
+ tagProject = tfProject;
198
+ vpcName = `${tfProject}${readResourceSuffix()}-vpc`;
199
+ }
200
+ } catch {
201
+ // fall through with basename matching
202
+ }
203
+ blockers = await describeDependencyBlockers(ec2, tagProject, vpcName);
204
+ } catch {
205
+ blockers = null;
206
+ }
207
+ if (blockers !== null) {
208
+ const ownedSgs = blockers.sgs.filter((sg) => sg.owned);
209
+ if (ownedSgs.length > 0) {
210
+ let proceed = shouldAutoApprove(options);
211
+ if (!proceed && !resolveHeadless(normalizeOptions(options))) {
212
+ const confirmed = await confirm({
213
+ message: `Delete ${ownedSgs.length} grada-owned security group${ownedSgs.length === 1 ? '' : 's'} blocking VPC deletion and retry?`,
214
+ initialValue: true,
215
+ });
216
+ proceed = !isCancel(confirmed) && confirmed === true;
217
+ }
218
+ if (proceed) {
219
+ try {
220
+ for (const sg of ownedSgs) {
221
+ await ec2.send(new DeleteSecurityGroupCommand({ GroupId: sg.id }));
222
+ }
223
+ } catch {
224
+ return namedFail('DESTROY_DEPENDENCY_VIOLATION');
225
+ }
226
+ trackEvent('dependency_blockers_removed', { projectName: actualProjectName, count: ownedSgs.length });
227
+ try {
228
+ await attemptDestroy();
229
+ } catch {
230
+ return namedFail('DESTROY_DEPENDENCY_VIOLATION');
231
+ }
232
+ return { retried: true };
233
+ }
234
+ }
235
+ const lines = [
236
+ ...blockers.enis.map((ni) => ` ENI ${ni.id} (${ni.detail})${ni.owned ? '' : ' — foreign, remove by hand'}`),
237
+ ...blockers.sgs.map((sg) => ` SG ${sg.id} (${sg.detail})${sg.owned ? '' : ' — foreign, remove by hand'}`),
238
+ ];
239
+ const runbook = getRunbook('DESTROY_DEPENDENCY_VIOLATION', { headless: shouldAutoApprove(options), lockId });
240
+ return {
241
+ retried: false,
242
+ result: failCommand({
243
+ 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`,
244
+ useErrorStream: true,
245
+ event: 'infrastructure_destroyed',
246
+ telemetry: { projectName: actualProjectName, error_code: 'DESTROY_DEPENDENCY_VIOLATION' },
247
+ errorCode: 'DESTROY_DEPENDENCY_VIOLATION',
248
+ reason: 'destroy-dependency-violation',
249
+ }),
250
+ };
251
+ }
252
+ }
253
+
141
254
  if (code === 'BACKEND_RECONFIGURE_REQUIRED' || code === 'DESTROY_DEPENDENCY_VIOLATION'
142
255
  || code === 'CLOUDFRONT_IN_PROGRESS' || code === 'DELETION_PROTECTION_ACTIVE') {
143
256
  return namedFail(code);
@@ -280,6 +393,18 @@ async function destroyStackMain(input = {}) {
280
393
  if (refused) return refused;
281
394
  }
282
395
 
396
+ // Secrets enter the 30-day recovery window on destroy: re-applying
397
+ // inside the window fails until the secret is restored or
398
+ // force-deleted (see the apply runbook). Warn in both modes —
399
+ // informational only, the confirm below still gates.
400
+ try {
401
+ if (fsSync.existsSync(path.join(tfDirPath, 'secrets.tf'))) {
402
+ 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.'));
403
+ }
404
+ } catch {
405
+ // Unreadable tree: the confirm below still gates.
406
+ }
407
+
283
408
  const proceed = shouldAutoApprove(options) ? true : await confirm({
284
409
  message: dbNeedsProtection
285
410
  ? 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 +433,7 @@ async function destroyStackMain(input = {}) {
308
433
  try {
309
434
  await runInit();
310
435
  } catch (error) {
311
- const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy: runInit, error });
436
+ const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy: runInit, error, region });
312
437
  if (!handled.retried) return handled.result;
313
438
  }
314
439
 
@@ -672,7 +797,7 @@ async function destroyStackMain(input = {}) {
672
797
  s.stop('AWS compute resources destroyed.');
673
798
  } catch (error) {
674
799
  s.stop(color.red('❌ Terraform destroy failed.'));
675
- const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error });
800
+ const handled = await handleDestroyTerraformError({ options, tfDirPath, attemptDestroy, error, region });
676
801
  if (!handled.retried) return handled.result;
677
802
  }
678
803
 
@@ -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,14 @@ 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
+ const nameMatch = /Secrets Manager Secret \(([^)]+)\)/.exec(text);
93
+ return { code: 'SECRET_PENDING_DELETION', lockId: null, secretName: nameMatch ? nameMatch[1] : null };
94
+ }
52
95
  if (/NoSuchBucket/i.test(text) || /does not exist/i.test(text)) {
53
96
  return { code: 'STATE_BUCKET_MISSING', lockId: null };
54
97
  }
@@ -116,18 +159,26 @@ const RUNBOOKS = {
116
159
  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
160
  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
161
  },
162
+ SECRET_PENDING_DELETION: {
163
+ message: 'A Secrets Manager secret is scheduled for deletion.',
164
+ headless: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import aws_secretsmanager_secret.app_secrets <arn>) or force-delete without recovery (aws secretsmanager delete-secret --secret-id <name> --force-delete-without-recovery) and re-apply + push — fetch the ARN via aws secretsmanager describe-secret --secret-id <name> --query ARN --output text. Grada never force-deletes: the window protects against accidental loss.',
165
+ interactive: 'Restore it (aws secretsmanager restore-secret --secret-id <name>, then cd terraform && terraform import aws_secretsmanager_secret.app_secrets <arn>) or force-delete without recovery (aws secretsmanager delete-secret --secret-id <name> --force-delete-without-recovery) and re-apply + push — fetch the ARN via aws secretsmanager describe-secret --secret-id <name> --query ARN --output text. Grada never force-deletes: the window protects against accidental loss.',
166
+ },
119
167
  };
120
168
 
121
169
  // Returns `{ message, hint }` for a named code, or null for unknown
122
170
  // codes. `headless` selects the variant; `lockId` fills the interactive
123
171
  // lock runbook. Resource identifiers stay in the rendered hint (stdout),
124
172
  // never in telemetry payloads.
125
- export function getRunbook(code, { headless = false, lockId = null } = {}) {
173
+ export function getRunbook(code, { headless = false, lockId = null, secretName = null } = {}) {
126
174
  const runbook = RUNBOOKS[code];
127
175
  if (!runbook) return null;
128
176
  let hint = headless ? runbook.headless : runbook.interactive;
129
177
  if (code === 'STATE_LOCK_HELD' && lockId) {
130
178
  hint = hint.replace(/<lock-id>/g, String(lockId));
131
179
  }
180
+ if (code === 'SECRET_PENDING_DELETION' && secretName) {
181
+ hint = hint.replace(/<name>/g, String(secretName));
182
+ }
132
183
  return { message: runbook.message, hint };
133
184
  }
@@ -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