aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.6k stars 3.9k forks source link

(@aws-cdk/aws-route53): ARecord with ApiGatewayv2DomainProperties to subdomain not working #17244

Open janschloss opened 2 years ago

janschloss commented 2 years ago

What is the problem?

Hello,

when trying to create an ARecord with an HTTP API alias on a subdomain, the record is created with the general domain instead.

Reproduction Steps

    const apiSubDomain = `api.${this.domain}`;

    const hostedZone = HostedZone.fromLookup(this, "HostedZone", {
      domainName: this.domain,
    });

    const certificate = new Certificate(this, "Certificate", {
      domainName: this.domain,
      subjectAlternativeNames: [apiSubDomain],
      validation: CertificateValidation.fromDns(hostedZone),
    });

    const domainName = new DomainName(this, "DomainName", {
// > Note that this is using the subdomain
      domainName: apiSubDomain,
      certificate: certificate,
    });

    const httpApi = new HttpApi(this, "HttpApi", {
      disableExecuteApiEndpoint: false,
      defaultDomainMapping: {
// > Note that this is using the subdomain
        domainName,
      },
    });

    new ARecord(this, "HttpApiAliasRecord", {
      zone: hostedZone,
      target: RecordTarget.fromAlias(
        new ApiGatewayv2DomainProperties(
          domainName.regionalDomainName,
          domainName.regionalHostedZoneId
        )
      ),
    });

What did you expect to happen?

I expected an A Record like this in Route53: api.example.com | A | Simple | - | alias.....

What actually happened?

An A Record to the general domain was created: example.com | A | Simple | - | alias.....

CDK CLI Version

1.130.0 (build 9c094ae)

Framework Version

No response

Node.js Version

v14.18.0

OS

Windows

Language

Typescript

Language Version

No response

Other information

No response

lukewiwa commented 2 years ago

Yes I think this is where the critical bug is https://github.com/aws/aws-cdk/blob/beaa5b2559b81e799a731cd680831699518de286/packages/%40aws-cdk/aws-cloudfront/lib/distribution.ts#L308

Seems that by default it takes the top level domain rather than one from the "domains" attribute

Solution

I figured out if you put the record name as the subdomain you wish to connect to it should work out ok.

    new route53.ARecord(this, "AliasRecord", {
      zone: hostedZone,
      recordName: "sub.domain.com",
      target: route53.RecordTarget.fromAlias(
        new alias.CloudFrontTarget(distribution)
      )
    });
  }
github-actions[bot] commented 1 year ago

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

janschloss commented 1 year ago

Don't think this is fixed yet

simonloach commented 1 year ago

This is not fixed and very misleading.