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-apigatewayv2.CfnApiMapping: Says there is already a Construct with name when there is not #29844

Closed
aj-n opened this issue Apr 15, 2024 · 7 comments
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug. p2

Comments

@aj-n
Copy link

aj-n commented Apr 15, 2024

Describe the bug

I am setting up domainName basePath and api mappings. The APIGateway Rest APIs are outputs of other stacks and they are imported into the current cdk app.
basePath mappings works flawlessly. However, when I try to use the apiMapping, it complains with There is already a Construct with name '${childName'} in ${typeName}

I am only adding it in one place. And the name I give it is definitlely unique.
I tried several methods that give the same error.

Expected Behavior

The ApiMapping and/or Stage constructs get initialized with no errors()

Current Behavior

Fatal error: There is already a Construct with name '${childName'} in ${typeName}

Reproduction Steps

import * as cdk from 'aws-cdk-lib';
import { EndpointType, ApiMapping, CfnApiMapping } from 'aws-cdk-lib/aws-apigatewayv2';
import { 
  aws_apigateway as apigateway,
  aws_certificatemanager as certificatemanager,
} from 'aws-cdk-lib';
import { Construct } from 'constructs';


export class MyStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
 
      // Imports
      const myAPIID = cdk.Fn.importValue(`some-other-exported-api-id`);
      const myAPI = apigateway.RestApi.fromRestApiId(this, "myAPI", myAPIID);
      
      // Init of domain name
      const domainNameAsString = `......`;
      const CertificateARN = `.......`;
      const certificate = certificatemanager.Certificate.fromCertificateArn(this, "certificate", CertificateARN);
      const subdomain = `.....`;
      
      const domainName = new cdk.aws_apigateway.DomainName(this, `DomainName_${subdomain}`, {
        domainName: domainNameAsString,
        certificate,
        basePath: "/",
        endpointType: EndpointType.REGIONAL,
      });
      
      // Base paths work fine
      domainName.addBasePathMapping(myAPI, {
        basePath: `routeThatWorks`,
        stage: myAPI.deploymentStage,
      });
      
       
      //////// FAILURES In each of these methods
      
      ///////////
      // This fails because `myAPI.deploymentStage` resolves to `undefined`
      const basePath = `some/path`;
      domainName.addApiMapping(myAPI.deploymentStage, {
        basePath,
      });
      
      ///////////
      // I tried using the Cfn2 method:
      new CfnApiMapping(this, `ApiMapping_jumanji2`, {
         apiId: myAPI.restApiId,
         domainName: domainNameAsString,
         stage: `development`,
         apiMappingKey: `some/path`,
      });
      // ERROR:
      //     Error: There is already a Construct with name 'ApiMapping_jumanji2' in MyStack [MyStack]
      
      ///////////
      // I also tried to get the stage via reference to get around the first error but a similar thing occurs
      
      // So, I tried to get it via reference
      const stage = cdk.aws_apigateway.Stage.fromStageAttributes(this, "jumanji", {
        restApi: myAPI,
        stageName: `development`
      });
      domainName.addApiMapping(stage, {
        basePath,
      });
      // ERROR:
      //     Error: There is already a Construct with name 'jumanji' in MyStack [MyStack]
    }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.137.0 (build bb90b4c)

Framework Version

No response

Node.js Version

v18.5.0

OS

OSX Sonoma 14.4.1

Language

TypeScript

Language Version

Typescript (5.4.5)

Other information

No response

@aj-n aj-n added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Apr 15, 2024
@github-actions github-actions bot added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Apr 15, 2024
@aj-n aj-n changed the title aws_apigatway.DomainName.addApiMapping: Says there is already a Construct with name when there is not aws-apigatewayv2.CfnApiMapping: Says there is already a Construct with name when there is not Apr 15, 2024
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 label Apr 15, 2024
@pahud
Copy link
Contributor

pahud commented Apr 18, 2024

The error occurres here:

const id = `Map:${options.basePath ?? 'none'}=>${Names.nodeUniqueId(targetStage.node)}`;

const basePath = `some/path`;
      domainName.addApiMapping(myAPI.deploymentStage, {
        basePath,
      });

This is because myAPI is actually a reference, cdk would have no idea about myAPI.deploymentStage because myAPI is not created by current cdk stack or app.

I think you will need to create the CfnApiMapping yourself.

This is my PoC:

export class MyDemoStack extends Stack {
  constructor(scope: Construct, id: string, props?: StackProps) {
    super(scope, id, props);
 
      // Imports
      const myAPIID = Fn.importValue(`some-other-exported-api-id`);
      const myAPI = agw.RestApi.fromRestApiId(this, "myAPI", myAPIID);

      
      // Init of domain name
      const domainNameAsString = 'foo.com';
      const CertificateARN = `.......`;
      const certificate = acm.Certificate.fromCertificateArn(this, "certificate", CertificateARN);
      const subdomain = `.....`;
      
      const domainName = new agw.DomainName(this, `DomainName_${subdomain}`, {
        domainName: domainNameAsString,
        certificate,
        basePath: "/",
        endpointType: agw.EndpointType.REGIONAL,
      });
      
      // Base paths work fine
      domainName.addBasePathMapping(myAPI, {
        basePath: `routeThatWorks`,
        stage: myAPI.deploymentStage,
      });
      
  
      new apigwv2.CfnApiMapping(this, `ApiMapping_jumanji2`, {
         apiId: myAPI.restApiId,
         domainName: domainNameAsString,
         stage: `development`,
         apiMappingKey: `some/path`,
      });
    }
}

And I can cdk synth or cdk diff without any error so I think it should work. Thoughts?

@pahud pahud added response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. p2 and removed needs-triage This issue or PR still needs to be triaged. labels Apr 18, 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 Apr 20, 2024
@aj-n
Copy link
Author

aj-n commented Apr 22, 2024

@pahud I tried this method as specified in my original submission

The error I got was Error: There is already a Construct with name 'ApiMapping_jumanji2' in MyStack [MyStack]

Even though this is the only time I am creating it

@pahud
Copy link
Contributor

pahud commented Apr 22, 2024

@pahud I tried this method as specified in my original submission

The error I got was Error: There is already a Construct with name 'ApiMapping_jumanji2' in MyStack [MyStack]

Even though this is the only time I am creating it

I guess you probably have some extra code generating the same construct id.

Can you just run my provided PoC above and see if you still have this error?

@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 Apr 22, 2024
@aj-n
Copy link
Author

aj-n commented Apr 23, 2024

Your PoC worked.

I was able to find the issue in my code. One of my deployment environments did in fact double-add it due to a configuration setting causing a loop.
Thank you for your help!

@aj-n aj-n closed this as completed Apr 23, 2024
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.

@aws-cdk-automation
Copy link
Collaborator

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.

@aws aws locked as resolved and limited conversation to collaborators Jul 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug. p2
Projects
None yet
Development

No branches or pull requests

3 participants