Skip to content

Comments

feat: add custom resource cdk stack parser and transformer#14298

Merged
iliapolo merged 13 commits intogen2-migrationfrom
sanjrkmr/custom-resource-cdk-stack-parser-and-transformer
Oct 28, 2025
Merged

feat: add custom resource cdk stack parser and transformer#14298
iliapolo merged 13 commits intogen2-migrationfrom
sanjrkmr/custom-resource-cdk-stack-parser-and-transformer

Conversation

@sanjanaravikumar-az
Copy link

This PR introduces enhancements to the Amplify CLI Gen2 migration tooling, focusing on custom resource transformation capabilities.

Changes-

Scanner: Traverses the amplify/backend/custom/ directory to discover all custom resources and
locate their cdk-stack.ts files. It provides the initial inventory of resources that need to be
migrated from Gen1 to Gen2.

Parser: Uses TypeScript AST to analyze Gen1 CDK stack files and extract structured information
including imports, class names, constructor bodies, and outputs. It also detects specific Gen1
patterns like CfnParameters, AmplifyHelpers calls, and CDK function references that require
transformation.

Transformer: Converts Gen1 code patterns to Gen2 equivalents using a hybrid approach of TypeScript
AST manipulation and string replacement fallbacks. It handles complex transformations like replacing
cdk.Fn.ref('env') with process.env.AMPLIFY_ENV and AmplifyHelpers.getProjectInfo() with environment
variable access.

Generator: Creates Gen2-compatible resource.ts files and backend integration code from the
transformed data. It filters out Gen1-specific imports, adds required Gen2 imports like constructs,
and generates proper class structures with backend.ts integration code.

Unit Tests:

  • Scanner: Custom resource discovery and file system operations

  • Parser: CDK pattern detection and AST analysis

  • Transformer: AST and string-based code transformations

  • Generator: Gen2 file generation and backend integration

  • PR description included

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

@sanjanaravikumar-az sanjanaravikumar-az requested a review from a team as a code owner October 24, 2025 20:21
const parsed = await this.parser.parseStack(resource.cdkStackPath);

// Detect patterns
const patterns = this.patternDetector.detectPatterns(parsed.constructorBody);

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable patterns.

Copilot Autofix

AI 4 months ago

To fix the problem, remove the unused variable declaration.
Specifically, delete the line:

const patterns = this.patternDetector.detectPatterns(parsed.constructorBody);

found on line 39 in packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/index.ts.
No other code depends on this variable, so its removal will not affect functionality.
No imports nor method definitions need adjustment, as only a single line should be removed.


Suggested changeset 1
packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/index.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/gen2-migration/codegen-custom-resources/index.ts b/packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/index.ts
--- a/packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/index.ts
+++ b/packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/index.ts
@@ -36,7 +36,6 @@
       const parsed = await this.parser.parseStack(resource.cdkStackPath);
 
       // Detect patterns
-      const patterns = this.patternDetector.detectPatterns(parsed.constructorBody);
 
       // Extract outputs
       const outputs = this.patternDetector.extractCfnOutputs(parsed.constructorBody);
EOF
@@ -36,7 +36,6 @@
const parsed = await this.parser.parseStack(resource.cdkStackPath);

// Detect patterns
const patterns = this.patternDetector.detectPatterns(parsed.constructorBody);

// Extract outputs
const outputs = this.patternDetector.extractCfnOutputs(parsed.constructorBody);
Copilot is powered by AI and may make mistakes. Always verify output.
@@ -0,0 +1,40 @@
import * as fs from 'fs-extra';

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused import fs.

Copilot Autofix

AI 4 months ago

To resolve the issue, we should remove the unused import statement import * as fs from 'fs-extra'; from the top of the file. This change is limited to a single line at the start of the file and will not affect any functionality since fs from fs-extra is not referenced anywhere in this file. No other code changes or dependency adjustments are needed.

Suggested changeset 1
packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/scanner/custom-resource-scanner.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/gen2-migration/codegen-custom-resources/scanner/custom-resource-scanner.ts b/packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/scanner/custom-resource-scanner.ts
--- a/packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/scanner/custom-resource-scanner.ts
+++ b/packages/amplify-cli/src/commands/gen2-migration/codegen-custom-resources/scanner/custom-resource-scanner.ts
@@ -1,4 +1,3 @@
-import * as fs from 'fs-extra';
 import { existsSync, promises as fsPromises } from 'fs';
 import * as path from 'path';
 import { CustomResource } from '../types';
EOF
@@ -1,4 +1,3 @@
import * as fs from 'fs-extra';
import { existsSync, promises as fsPromises } from 'fs';
import * as path from 'path';
import { CustomResource } from '../types';
Copilot is powered by AI and may make mistakes. Always verify output.
@iliapolo iliapolo merged commit 67f3e7a into gen2-migration Oct 28, 2025
3 checks passed
@iliapolo iliapolo deleted the sanjrkmr/custom-resource-cdk-stack-parser-and-transformer branch October 28, 2025 14:02
iankhou pushed a commit that referenced this pull request Oct 28, 2025
* chore: scaffolding migration

* chore: gen2 migration

* chore: add codegen-custom-resources directory

* chore: mid work

* chore: remove non-essential files from tracking

* chore: remove additional gen2-migration command files from tracking

* chore: remove remaining non-essential files from tracking

* chore: remove migration plan documentation from tracking

* chore: remove integration test files from tracking

---------

Co-authored-by: Eli Polonsky <epolon@amazon.com>
Co-authored-by: sanjanaravikumar-az <sanjrkmr@amazon.com>

feat: wip

feat: wip2

feat: fixed storage issue

fix: setup file for jest
iankhou pushed a commit that referenced this pull request Oct 28, 2025
* chore: scaffolding migration

* chore: gen2 migration

* chore: add codegen-custom-resources directory

* chore: mid work

* chore: remove non-essential files from tracking

* chore: remove additional gen2-migration command files from tracking

* chore: remove remaining non-essential files from tracking

* chore: remove migration plan documentation from tracking

* chore: remove integration test files from tracking

---------

Co-authored-by: Eli Polonsky <epolon@amazon.com>
Co-authored-by: sanjanaravikumar-az <sanjrkmr@amazon.com>

feat: wip

feat: wip2

feat: fixed storage issue

fix: setup file for jest
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.

2 participants