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): stack update doesn't work when defining Route53 via AwsCustomResource #28906

Closed
MyNameIsOka opened this issue Jan 29, 2024 · 3 comments
Labels
@aws-cdk/aws-route53 Related to Amazon Route 53 bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@MyNameIsOka
Copy link

Describe the bug

It's basically a continuation of issue #12756
The proposed solution is working for deploying but doesn't work if I need to update the stack or when destroying and re-deploying it.

The error thrown is:

/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/constructs/src/construct.ts:447
      throw new Error(`There is already a Construct with name '${childName}' in ${typeName}${name.length > 0 ? ' [' + name + ']' : ''}`);
            ^
Error: There is already a Construct with name 'CreatedPublicHostedZone' in PublicHostedZoneWithReusableDelegationSet [MyHostedZone]
    at Node.addChild (/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/constructs/src/construct.ts:447:13)
    at new Node (/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/constructs/src/construct.ts:71:17)
    at new Construct (/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/constructs/src/construct.ts:499:17)
    at new Resource (/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/aws-cdk-lib/core/lib/resource.js:1:1309)
    at new Import (/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/aws-cdk-lib/aws-route53/lib/hosted-zone.js:1:1887)
    at Function.fromHostedZoneAttributes (/home/runner/work/my-repo/my-repo/packages/infrastructure/node_modules/aws-cdk-lib/aws-route53/lib/hosted-zone.js:1:2154)
    at PublicHostedZoneWithReusableDelegationSet.asPublicHostedZone (/home/runner/work/my-repo/my-repo/packages/infrastructure/src/my-repo-stack.ts:106:27)
    at new LodzInfraCdkStack (/home/runner/work/my-repo/my-repo/packages/infrastructure/src/my-repo-stack.ts:360:21)
    at Object.<anonymous> (/home/runner/work/my-repo/my-repo/packages/infrastructure/src/my-repo-stack.ts:575:1)
    at Module._compile (node:internal/modules/cjs/loader:1356:14)
Subprocess exited with error 1

Expected Behavior

Being able to update the stack successfully.

Current Behavior

Stack throws the error There is already a Construct with name 'CreatedPublicHostedZone' in PublicHostedZoneWithReusableDelegationSet

Reproduction Steps

Deploy a stack with this resources:

// Allows CDK to use a reusable delegation set. Sourced from:
// https://pablissimo.com/1100/creating-a-route-53-public-hosted-zone-with-a-reusable-delegation-set-id-in-cdk

import {
  AwsCustomResource,
  AwsCustomResourcePolicy,
} from "@aws-cdk/custom-resources";
import { Construct, Fn, Names } from "@aws-cdk/core";
import {
  IPublicHostedZone,
  PublicHostedZone,
  PublicHostedZoneProps,
} from "@aws-cdk/aws-route53";
import { PhysicalResourceId } from "@aws-cdk/custom-resources";

export interface PublicHostedZoneWithReusableDelegationSetProps
  extends PublicHostedZoneProps {
  delegationSetId: string;
}

export class PublicHostedZoneWithReusableDelegationSet extends Construct {
  private publicHostedZone: AwsCustomResource;
  private hostedZoneName: string;

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

    this.hostedZoneName = props.zoneName;

    const normaliseId = (id: string) => id.split("/").slice(-1)[0];
    const normalisedDelegationSetId = normaliseId(props.delegationSetId);

    this.publicHostedZone = new AwsCustomResource(
      this,
      "CreatePublicHostedZone",
      {
        onCreate: {
          service: "Route53",
          action: "createHostedZone",
          parameters: {
            CallerReference: Names.uniqueId(this),
            Name: this.hostedZoneName,
            DelegationSetId: normalisedDelegationSetId,
            HostedZoneConfig: {
              Comment: props.comment,
              PrivateZone: false,
            },
          },
          physicalResourceId: PhysicalResourceId.fromResponse("HostedZone.Id"),
        },
        policy: AwsCustomResourcePolicy.fromSdkCalls({
          resources: AwsCustomResourcePolicy.ANY_RESOURCE,
        }),
      }
    );

    new AwsCustomResource(this, "DeletePublicHostedZone", {
      onDelete: {
        service: "Route53",
        action: "deleteHostedZone",
        parameters: {
          Id: this.publicHostedZone.getResponseField("HostedZone.Id"),
        },
      },
      policy: AwsCustomResourcePolicy.fromSdkCalls({
        resources: AwsCustomResourcePolicy.ANY_RESOURCE,
      }),
    });
  }

  asPublicHostedZone(): IPublicHostedZone {
    return PublicHostedZone.fromHostedZoneAttributes(
      this,
      "CreatedPublicHostedZone",
      {
        hostedZoneId: Fn.select(
          2,
          Fn.split("/", this.publicHostedZone.getResponseField("HostedZone.Id"))
        ),
        zoneName: this.hostedZoneName,
      }
    );
  }
}

Possible Solution

No response

Additional Information/Context

No response

CDK CLI Version

2.122.0

Framework Version

No response

Node.js Version

20.8.0

OS

Ubuntu

Language

TypeScript

Language Version

5.3.3

Other information

No response

@MyNameIsOka MyNameIsOka added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 29, 2024
@github-actions github-actions bot added the @aws-cdk/aws-route53 Related to Amazon Route 53 label Jan 29, 2024
@MyNameIsOka
Copy link
Author

Okay, the reason was that I called asPublicHostedZone twice. They would have the same id and therefore the deployment would fail. I changed the code to this and then the deployment worked:

asPublicHostedZone(uniqueId: string): IPublicHostedZone {
  return PublicHostedZone.fromHostedZoneAttributes(
    this,
    `CreatedPublicHostedZone-${uniqueId}`,
    {
      hostedZoneId: Fn.select(
        2,
        Fn.split(
          "/",
          this.publicHostedZone.getResponseField("HostedZone.Id"),
        ),
      ),
      zoneName: this.hostedZoneName,
    },
  );
}

// ...

const certificate = new cdk.aws_certificatemanager.Certificate(
  this,
  "SiteCertificate",
  {
    domainName: DOMAIN,
    validation: cdk.aws_certificatemanager.CertificateValidation.fromDns(
      hostedZone.asPublicHostedZone("certificate"),
    ),
  },
);

@pahud
Copy link
Contributor

pahud commented Jan 29, 2024

Thank you for the report.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Jan 29, 2024
@pahud pahud closed this as completed Jan 29, 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.

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. effort/medium Medium work item – several days of effort p2
Projects
None yet
Development

No branches or pull requests

2 participants