Skip to content

Conversation

@sanjanaravikumar-az
Copy link

@sanjanaravikumar-az sanjanaravikumar-az commented Nov 7, 2025

Initial problem: Previously the migrated code contained AMPLIFY_GEN_1_ENV_NAME = process.env.AMPLIFY_ENV which should be changed to branchName = process.env.AWS_BRANCH. Also any env references would be cdk.Fn.ref('env') which does not suit the gen2 format.

There are patterns like cdk.Fn.ref('env') or AmplifyHelpers.getProjectInfo().envName which need to be transformed to branchName, and we also need to remove the import related to this. Also before, when we migrate the gen1 app to gen2, you get this line of code at the top which is like AMPLIFY_GEN_1_ENV_NAME = process.env.AMPLIFY_ENV which we replace with branchName = process.env.AWS_BRANCH.

The code scans the gen1 app for env variables and references and replaces them with the proper gen2 branch names and references. This is because gen2 apps only use branch names and have no concept of environments.

Changes:

  • Add AmplifyHelperTransformer to handle standalone cdk.Fn.ref('env') calls
  • Update CodeTransformer to replace cdk.Fn.ref('env') with branchName variable
  • Update command handlers to generate const branchName = process.env.AWS_BRANCH ?? "sandbox". The AWS_BRANCH is used to get the branch name of the current build.
  • Keep environment variable setting as AMPLIFY_GEN_1_ENV_NAME for compatibility

Generated CDK stack files will now use:

  • const branchName = process.env.AWS_BRANCH ?? "sandbox";
  • All cdk.Fn.ref('env') replaced with branchName
  • All AmplifyHelpers.getProjectInfo().envName replaced with branchName

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 November 7, 2025 19:47
@sanjanaravikumar-az sanjanaravikumar-az force-pushed the sanjrkmr/aws-branch-env-variable-replacement branch 5 times, most recently from aafa1b2 to 75ea0f5 Compare November 9, 2025 19:17
Copy link
Contributor

@kaizencc kaizencc left a comment

Choose a reason for hiding this comment

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

there's a few things i would like to see before i can give this a proper review:

  • the PR description is missing a "why" or a link to an issue where the reviewer can gain context on the motivation behind the PR.
  • the PR description "changes" section (or, alternatively, you could review your own PR and add relevant inline comments) does not include deletion of code-transformers.ts and ast-code-transformer.ts and why that is necessary
  • yarn.lock file should not change if there was no update to dependencies in this PR

the main thing is providing necessary context for a smooth review though. currently any reviewer of your PR needs to have context supplied elsewhere and I just don't have that context yet

undefined,
undefined,
factory.createIdentifier('process.env.AMPLIFY_GEN_1_ENV_NAME ?? "sandbox"'),
factory.createIdentifier('process.env.AWS_BRANCH ?? "sandbox"'),
Copy link

Choose a reason for hiding this comment

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

I had to check the docs to understand this, because on the surface, it just looks like a rename. You mention this change in your PR description, but it's always helpful to also indicate why, and provide a reference.

https://docs.aws.amazon.com/amplify/latest/userguide/environment-variables.html#amplify-console-environment-variables

Choose a reason for hiding this comment

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

noted, i'll add this in the PR description so its more clear.

Copy link

@iankhou iankhou left a comment

Choose a reason for hiding this comment

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

Remove yarn.lock file changes, provide a clearer explanation in the PR description. Right now it tells me "what," but I want to know "why."

Copy link

Choose a reason for hiding this comment

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

Don't modify this unless you're modifying dependencies in package.json.

Choose a reason for hiding this comment

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

got it, it wasn't supposed to be there originally, i've deleted it now.

@sanjanaravikumar-az sanjanaravikumar-az changed the base branch from gen2-migration to sanjrkmr/aws-branch-env-variable-clean November 14, 2025 17:14
@sanjanaravikumar-az sanjanaravikumar-az changed the base branch from sanjrkmr/aws-branch-env-variable-clean to sanjrkmr/custom-resource-cdk-stack-parser-and-transformer November 14, 2025 17:15
@sanjanaravikumar-az sanjanaravikumar-az changed the base branch from sanjrkmr/custom-resource-cdk-stack-parser-and-transformer to gen2-migration November 14, 2025 17:15

Choose a reason for hiding this comment

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

this file is unnecessary because i've added basically the same functionality in amplify-helper-transformer.ts, and it is more efficient.

Choose a reason for hiding this comment

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

this is removed for pretty much the same reason as packages/amplify-cli/src/tests/commands/gen2-migration/codegen-custom-resources/transformer/ast-code-transformer.test.ts. its duplicate code that isnt needed anymore.

sanjanaravikumar-az added 4 commits November 14, 2025 15:39
- Add missing custom-resource-migrator.ts file
- Fix DataDefinitionFetcher constructor call to use correct number of arguments
…cess.env.AMPLIFY_ENV

The test was expecting the wrong replacement value. The code transformer
correctly replaces cdk.Fn.ref('env') with branchName, not process.env.AMPLIFY_ENV.
- Add amplify-helper-transformer.ts with branchName transformation
- Transform cdk.Fn.ref('env') and Fn.ref('env') to branchName
- Transform AmplifyHelpers.getProjectInfo().envName to branchName
- Add branchName declaration using process.env.AWS_BRANCH ?? 'sandbox'
- Remove AmplifyHelpers.getProjectInfo() replacement from command-handlers
- Remove in-place Gen1 file transformation to preserve original files
@sanjanaravikumar-az sanjanaravikumar-az force-pushed the sanjrkmr/aws-branch-env-variable-replacement branch from 96ad63c to 019426e Compare November 14, 2025 20:42
@sanjanaravikumar-az
Copy link
Author

@kaizencc I've tried to add a bit more context for the reason behind this change, and I've updated the project board as well as per your suggestion, so hopefully i was able to provide a little more background. I've also added inline comments for the reasoning behind deleting the files I removed, and updated the yarn.lock file so there are no changes to it.

  • the PR description is missing a "why" or a link to an issue where the reviewer can gain context on the motivation behind the PR.
  • the PR description "changes" section (or, alternatively, you could review your own PR and add relevant inline comments) does not include deletion of code-transformers.ts and ast-code-transformer.ts and why that is necessary
  • yarn.lock file should not change if there was no update to dependencies in this PR
    the main thing is providing necessary context for a smooth review though. currently any reviewer of your PR needs to have context supplied elsewhere and I just don't have that context yet

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