Skip to content

feat(gen2-migration): cfn drift detection#14303

Merged
iliapolo merged 14 commits intogen2-migrationfrom
drift-detection
Nov 7, 2025
Merged

feat(gen2-migration): cfn drift detection#14303
iliapolo merged 14 commits intogen2-migrationfrom
drift-detection

Conversation

@9pace
Copy link
Copy Markdown

@9pace 9pace commented Oct 28, 2025

Description of changes

Issue #, if available

Description of how you validated changes

Checklist

  • PR description included
  • yarn test passes
  • Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)
  • New AWS SDK calls or CloudFormation actions have been added to relevant test and service IAM policies
  • Pull request labels are added

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@9pace 9pace marked this pull request as ready for review November 3, 2025 19:56
@9pace 9pace requested a review from a team as a code owner November 3, 2025 19:56
// Parse and format messages based on their content
if (msg.includes('Found') && msg.includes('nested')) {
// Extract indentation, count, and type
const indent = msg.match(/^(\s*)/)?.[1] || '';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable indent.

Copilot Autofix

AI 6 months ago

To fix the issue, we should remove the unused variable declaration at line 133 which reads the indentation from the incoming message string: const indent = msg.match(/^(\s*)/)?.[1] || '';. This statement is unnecessary and can be safely deleted. The rest of the block will continue to work correctly, as none of the output or logic depends on indent. Only this single variable declaration (the line itself) should be changed; all surrounding code should be left intact.


Suggested changeset 1
packages/amplify-cli/src/commands/drift.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/amplify-cli/src/commands/drift.ts b/packages/amplify-cli/src/commands/drift.ts
--- a/packages/amplify-cli/src/commands/drift.ts
+++ b/packages/amplify-cli/src/commands/drift.ts
@@ -130,7 +130,6 @@
         // Parse and format messages based on their content
         if (msg.includes('Found') && msg.includes('nested')) {
           // Extract indentation, count, and type
-          const indent = msg.match(/^(\s*)/)?.[1] || '';
           const count = msg.match(/Found (\d+)/)?.[1] || '0';
           const level = msg.match(/level (\d+)/)?.[1];
 
EOF
@@ -130,7 +130,6 @@
// Parse and format messages based on their content
if (msg.includes('Found') && msg.includes('nested')) {
// Extract indentation, count, and type
const indent = msg.match(/^(\s*)/)?.[1] || '';
const count = msg.match(/Found (\d+)/)?.[1] || '0';
const level = msg.match(/level (\d+)/)?.[1];

Copilot is powered by AI and may make mistakes. Always verify output.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot is right.

// Extract indentation, count, and type
const indent = msg.match(/^(\s*)/)?.[1] || '';
const count = msg.match(/Found (\d+)/)?.[1] || '0';
const level = msg.match(/level (\d+)/)?.[1];

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable level.

Copilot Autofix

AI 6 months ago

To resolve the unused variable warning, delete the declaration and assignment of the level variable on line 135 within the createPrintObject function. Do not alter the rest of the logic: keep processing indent and count as before, since they are being used. Remove only level and its assignment, ensuring that indentation and code layout are preserved.


Suggested changeset 1
packages/amplify-cli/src/commands/drift.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/amplify-cli/src/commands/drift.ts b/packages/amplify-cli/src/commands/drift.ts
--- a/packages/amplify-cli/src/commands/drift.ts
+++ b/packages/amplify-cli/src/commands/drift.ts
@@ -132,7 +132,6 @@
           // Extract indentation, count, and type
           const indent = msg.match(/^(\s*)/)?.[1] || '';
           const count = msg.match(/Found (\d+)/)?.[1] || '0';
-          const level = msg.match(/level (\d+)/)?.[1];
 
           // Always show "Found X nested stack(s)" without indentation
           printer.info(chalk.gray(`Found ${chalk.yellow(count)} nested stack(s)`));
EOF
@@ -132,7 +132,6 @@
// Extract indentation, count, and type
const indent = msg.match(/^(\s*)/)?.[1] || '';
const count = msg.match(/Found (\d+)/)?.[1] || '0';
const level = msg.match(/level (\d+)/)?.[1];

// Always show "Found X nested stack(s)" without indentation
printer.info(chalk.gray(`Found ${chalk.yellow(count)} nested stack(s)`));
Copilot is powered by AI and may make mistakes. Always verify output.
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here

const exitCode = await detector.detect(options);

if (exitCode !== 0) {
process.exitCode = exitCode;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer throwing an error (AmplifyError?) to make it clearer to the user the command failed

Comment thread package.json
},
"packageManager": "yarn@3.5.0",
"resolutions": {
"@aws-sdk/client-cloudformation": "^3.624.0",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

import chalk from 'chalk';
import { detectStackDriftRecursive, type DriftDisplayFormat } from './drift-detection';
import { CloudFormationService, AmplifyConfigService, FileService, DriftFormatter } from './drift-detection/services';
import type { CloudFormationClient } from '@aws-sdk/client-cloudformation';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unused

import { $TSContext, AmplifyError } from '@aws-amplify/amplify-cli-core';
import { printer } from '@aws-amplify/amplify-prompts';
import chalk from 'chalk';
import { detectStackDriftRecursive, type DriftDisplayFormat } from './drift-detection';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

DriftDisplayFormat unused


// Display initial status
printer.info('');
printer.info(chalk.cyan.bold(`Started Drift Detection for Project: ${projectName}`));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should also say which environment we are checking

}
}
} catch (e) {
// If parsing fails, log and use the original value
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we are good with both a name and an ARN - why choose over the other?


// Validate the extracted name
if (!nestedStackName || nestedStackName === nestedStack.PhysicalResourceId) {
logger.logInfo({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why validate if we are just logging?

}

// Validate the extracted name
if (!nestedStackName || nestedStackName === nestedStack.PhysicalResourceId) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use nestedStack.PhysicalResourceId and call it a day? what scenario are you trying to protect against?

}
}

logger.logInfo({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When are these being printed to screen? with --verbose?

message: `detectStackDriftRecursive.nestedStack: ${nestedStack.LogicalResourceId}, ${nestedResults.rootStackDrifts.StackResourceDrifts?.length} direct resources, ${nestedResults.nestedStackDrifts.size} sub-stacks`,
});
} catch (error: any) {
// Log error but continue checking other nested stacks
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah but again in the context of migration I would prefer this to fail - to be extra safe.

@iliapolo iliapolo merged commit 8366503 into gen2-migration Nov 7, 2025
4 checks passed
@iliapolo iliapolo deleted the drift-detection branch November 7, 2025 13:41
@9pace 9pace restored the drift-detection branch November 12, 2025 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants