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

aws_route53: HostedZone.fromLookup fails in nested stacks #27249

Closed
bencaldwell opened this issue Sep 22, 2023 · 4 comments
Closed

aws_route53: HostedZone.fromLookup fails in nested stacks #27249

bencaldwell opened this issue Sep 22, 2023 · 4 comments
Labels
@aws-cdk/aws-route53 Related to Amazon Route 53 bug This issue is a bug. p1

Comments

@bencaldwell
Copy link

Describe the bug

I think this one has been raised and fixed a few times before e.g. #4651, #25171

I have a pipeline stack with stages, pretty similar to what is described in https://docs.aws.amazon.com/cdk/v2/guide/cdk_pipeline.html

This app requires that I lookup some records in an already existing hosted zone.
I can perform HostedZone.fromLookup in the top level stack. The below works:

...
export class MyPipelineStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props: MyPipelineStackProps) {
    super(scope, id, props);

    const zoneStage = HostedZone.fromLookup(this, 'my-zone', { domainName: 'mydomain.com.au' }) as HostedZone;
   
    // create a pipeline that watches the codecommit repo develop branch
    const pipelineDevelop = new CodePipeline(this, 'pipeline-develop', {
      pipelineName: 'my-pipeline-develop',
      synth: new ShellStep('Synth', {
        input: CodePipelineSource.codeCommit(repo, 'develop'),
        commands: ['npm ci', 'npm run build', 'npx cdk synth']
      }),
    });

    pipelineDevelop.addStage(new MyPipelineAppStage(this, "my-stage", {
      env: envStage,
      domainName: 'mydomain.com.au'
    }));

However, I have a cross account deploy so I need to change environments to lookup a hosted zone in a prod account. So I moved the HostedZone.fromLookup inside my MyPipelineAppStage class, which is called above as a nested stack in the pipelineDevelop.addStage. The below does not work :

export class MyPipelineAppStage extends cdk.Stage {

  lambdaStack: MyLambdaStack;
  zone: HostedZone;

  constructor(scope: Construct, id: string, props: MyPipelineAppStageProps) {
    super(scope, id, props);

    this.zone = HostedZone.fromLookup(this, 'zone', { domainName: props.domainName }) as HostedZone;
    this.lambdaStack= new MyLambdaStack(this, 'blue');

  }

}

The error I get is:

Error: MyPipelineAppStage at 'my-pipeline/my-stage' should be created in the scope of a Stack, but no Stack found

It appears that calling this from the nested Stage stack is breaking the lookup.

Expected Behavior

I expected that HostedZone.fromLookup would work in all stacks, top level and nested.

Current Behavior

HostedZone.fromLookup only works in the top level stack. When I call it in a nested stack, created by pipeline.addStage, it throws an error:

Error: MyPipelineAppStage at 'my-pipeline/my-stage' should be created in the scope of a Stack, but no Stack found

Reproduction Steps

Snippets included in the main description should be enough. The stack looks like:

app

  • pipeline stack
    • stage stack - where the HostedZone.fromLookup is called

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.96.2 (build 3edd240)

Framework Version

No response

Node.js Version

v18.17.1

OS

WSL... but the error happens in the pipeline in AWS account

Language

Typescript

Language Version

5.2.2

Other information

No response

@bencaldwell bencaldwell added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Sep 22, 2023
@github-actions github-actions bot added the @aws-cdk/aws-route53 Related to Amazon Route 53 label Sep 22, 2023
@indrora indrora added p1 and removed needs-triage This issue or PR still needs to be triaged. labels Sep 25, 2023
@indrora
Copy link
Contributor

indrora commented Sep 25, 2023

Hmm... Three times sounds like we need to do a longer investigation into this. Cross-Account stuff is always hard to manage.

Can you produce a Very Minimal (non)working example?

@indrora indrora added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. cross-stack Related to cross-stack resource sharing labels Sep 25, 2023
@peterwoodworth peterwoodworth removed response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. cross-stack Related to cross-stack resource sharing labels Sep 25, 2023
@peterwoodworth
Copy link
Contributor

@indrora the information given is plenty sufficient to reproduce. If you have any specific questions, feel free to ask

@bencaldwell
Copy link
Author

Agh! My apologies, I've just realised that this problem is user error.
I was creating the hostedZone in a class that extends Stage, which itself only extends Construct, not Stack. So the error message was valid.

@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-route53 Related to Amazon Route 53 bug This issue is a bug. p1
Projects
None yet
Development

No branches or pull requests

3 participants