Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(codepipeline): CrossRegionSupportStack Stack name must match the regular expression #31436

Closed
1 task
jsunico opened this issue Sep 13, 2024 · 8 comments · Fixed by #31967
Closed
1 task

(codepipeline): CrossRegionSupportStack Stack name must match the regular expression #31436

jsunico opened this issue Sep 13, 2024 · 8 comments · Fixed by #31967
Assignees
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline bug This issue is a bug. effort/small Small work item – less than a day of effort p1

Comments

@jsunico
Copy link

jsunico commented Sep 13, 2024

Describe the Bug

I have a State Machine from another region that is being imported to CDK via StateMachine.fromStateMachineArn. When this state machine is added as cross-region StepFunctionInvokeAction to the pipeline an error is thrown.

Regression Issue

  • Select this option if this issue appears to be a regression.

Last Known Working CDK Version

No response

Expected Behavior

Cross-region action can be added to the pipeline.

Current Behavior

Error: Stack name must match the regular expression: /^[A-Za-z][A-Za-z0-9-]*$/, got '${Token[TOKEN.840]}-support-eu-west-1'
    at new Stack (node_modules/aws-cdk-lib/core/lib/stack.js:1:2505)
    at new CrossRegionSupportStack (node_modules/aws-cdk-lib/aws-codepipeline/lib/private/cross-region-support-stack.js:1:1798)
    at Pipeline.createSupportResourcesForRegion (node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:10279)
    at Pipeline.obtainCrossRegionSupportFor (node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:9122)
    at Pipeline.ensureReplicationResourcesExistFor (node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:8644)
    at Pipeline._attachActionToPipeline (node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:7708)
    at Stage.attachActionToPipeline (node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:3087)
    at Stage.addAction (node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:1716)
    at new Stage (node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:678)
    at Pipeline.addStage (node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:6662)

Reproduction Steps

  • Import a state machine from another region using StateMachine.fromStateMachineArn
  • Add StepFunctionInvokeAction to a stage in the pipeline and use the imported state machine.

Possible Solution

NA

Additional Information/Context

No response

CDK CLI Version

2.158.0

Framework Version

No response

Node.js Version

18.20.4

OS

WSL Ubuntu 20

Language

TypeScript

Language Version

~4.6.4

Other information

No response

@jsunico jsunico added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 13, 2024
@github-actions github-actions bot added the @aws-cdk/aws-codepipeline Related to AWS CodePipeline label Sep 13, 2024
@khushail
Copy link
Contributor

khushail commented Sep 13, 2024

Hey @jsunico , thanks for reaching out.
From the initial error check, it looks like the stack name given by you, is the issue, not the code.

throw new Error(`Stack name must match the regular expression: ${VALID_STACK_NAME_REGEX.toString()}, got '${this.stackName}'`);

Found somewhat similar issue in the past - #23098
It would be more helpful if you could share the minimal repro code with the stack name you are using.

@khushail khushail added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. and removed needs-triage This issue or PR still needs to be triaged. labels Sep 13, 2024
@khushail khushail self-assigned this Sep 13, 2024
@khushail khushail added the p2 label Sep 13, 2024
Copy link

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Sep 15, 2024
@jsunico
Copy link
Author

jsunico commented Sep 16, 2024

Hi @khushail, so I tried to reproduce it in a new project here: https://github.com/jsunico/aws-cdk-repro-cross-region-action-in-pipeline

I started off with just a single stack which is generated by cdk init. I didn't experience the issue. I then switch to nested stack to align it with our project setup and was able to reproduce the issue.

This is the error you'll get when running npx cdk synth

Error: Stack name must match the regular expression: /^[A-Za-z][A-Za-z0-9-]*$/, got '${Token[TOKEN.21]}-support-service'
    at new Stack (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/core/lib/stack.js:1:4798)
    at new CrossRegionSupportStack (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/cross-region-support-stack.js:1:1984)
    at Pipeline.createSupportResourcesForRegion (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:14004)
    at Pipeline.obtainCrossRegionSupportFor (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:12840)
    at Pipeline.ensureReplicationResourcesExistFor (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:12360)
    at Pipeline._attachActionToPipeline (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:11358)
    at Stage.attachActionToPipeline (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:3313)
    at Stage.addAction (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:1928)
    at new Stage (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/private/stage.js:1:888)
    at Pipeline.addStage (github-issues/pipeline-cross-region/node_modules/aws-cdk-lib/aws-codepipeline/lib/pipeline.js:1:9264)

From the error above, it seems that the method below is not getting the correct pipelineStackName. In a single stack setup, it can successfully get the stack name but in a nested stack setup it is returning ${Token[TOKEN.21]}.

https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-codepipeline/lib/private/cross-region-support-stack.ts#L153-L155

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels Sep 16, 2024
@khushail khushail added needs-reproduction This issue needs reproduction. investigating This issue is being investigated and/or work is in progress to resolve the issue. and removed needs-reproduction This issue needs reproduction. labels Sep 16, 2024
@khushail
Copy link
Contributor

khushail commented Sep 18, 2024

Hi @jsunico , thanks for sharing the repro code and your investigation.

I found various related issues around this one ,sharing few of those -
#6190
#27746
#5029
#22223
PR - #4745 , #6419

Although relative PRs have been merged but this CrossAccountRegion deployment is causing the issue. I faced the sImilar issue when tried to deploy the code shared by you. I checked all the Pipeline related guidelines, still getting this issue -
Screenshot 2024-09-18 at 12 13 59 PM

This surely seems a bug.

https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-codepipeline/lib/private/cross-region-support-stack.ts#L153-L155

I am marking it as P1, requesting further action/inputs from the core team.

@khushail khushail added p1 effort/small Small work item – less than a day of effort and removed p2 investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Sep 18, 2024
@khushail khushail removed their assignment Sep 18, 2024
@godwingrs22 godwingrs22 self-assigned this Sep 24, 2024
@godwingrs22 godwingrs22 removed their assignment Oct 21, 2024
@GavinZZ GavinZZ self-assigned this Oct 29, 2024
@GavinZZ
Copy link
Contributor

GavinZZ commented Oct 29, 2024

@jsunico I took a deep dive into the code and I see the main source of issue is with the nested stack you used. When nested stack is used, the stack name becomes a Token representation and does not resolve until deployment. This would then fail in the check in

throw new Error(`Stack name must match the regular expression: ${VALID_STACK_NAME_REGEX.toString()}, got '${this.stackName}'`);
because Tokenized value will fail the regex check.

I'd like to understand is there any specific reason you want to use Nested stack in this case. If you change PipelineCrossRegionStack in your example to be a standard cdk.Stack (instead of cdk.NestedStack), it would not fail and should allow you to continue. Let me know if this workaround works for you or you're specifically using nested stack for some purpose.

@jsunico
Copy link
Author

jsunico commented Oct 31, 2024

Hi @GavinZZ, thanks for taking a deeper look into this issue.

We do deploy big applications using CDK and we tend to break down or group resources using NestedStacks. We would have a root stack and a few nested stacks like (network-layer, application-layer, data-layer, pipeline-layer).

In my understanding this is the best practice in managing multiple stacks within CDK. Could you please point me to the correct documentation if I have misunderstood? Thank you.

@GavinZZ
Copy link
Contributor

GavinZZ commented Nov 1, 2024

@jsunico There isn't any documentation around best practices of using nested stack in CDK. According to CFN documentation, one of the key benefits of using nested stack is As your infrastructure grows, common patterns can emerge in which you declare the same components in multiple templates. You can separate out these common components and create dedicated templates for them. Then, use the resource in your template to reference other templates, creating nested stacks. Of course, nested stack can also helps with the 500 resource limit issue in a given stack.

From CDK's perspective, I don't see much benefits of using nested stack. If a stack is anywhere close to 500 resource limits, the stack should be split into regular sub-stacks instead of using nested stacks as the solution given how you can simply export a resource and reference the resource from a different stack easily in CDK, like the following:

const stack1 = new StackThatProvidesABucket(app, 'Stack1' , { env: prod });

// stack2 will take a property { bucket: IBucket }
const stack2 = new StackThatExpectsABucket(app, 'Stack2', {
  bucket: stack1.bucket,
  env: prod
});

@mergify mergify bot closed this as completed in #31967 Nov 7, 2024
@mergify mergify bot closed this as completed in a09ccdb Nov 7, 2024
Copy link

github-actions bot commented Nov 7, 2024

Comments on closed issues and PRs are hard for our team to see.
If you need help, please open a new issue that references this one.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 7, 2024
Leo10Gama pushed a commit to Leo10Gama/aws-cdk that referenced this issue Nov 13, 2024
…ame is unresolved token (aws#31967)

### Issue # (if applicable)

Closes aws#31436.

### Reason for this change

When nested stack is used with CodePipeline, the stack name is actually a token which will fail regex check. We can't simply add a `Token.isUnresolved` check before the regex check because stack name must be a resolved string otherwise the template file would include `Token[xxx]`.


### Description of changes

The only feasible fix would be allowing users to override the nested stack name if users provide one. Currently nested stack names are auto-generated and are a token value. Allow users to specify the stack name.

### Description of how you validated changes

New integ tests pass. Existing tests pass.

### Checklist
- [ ] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-codepipeline Related to AWS CodePipeline bug This issue is a bug. effort/small Small work item – less than a day of effort p1
Projects
None yet
4 participants