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: DomainName.name is no longer passed as value to ARecord #16829

Closed
paulie4 opened this issue Oct 6, 2021 · 10 comments
Closed
Assignees
Labels
@aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug.

Comments

@paulie4
Copy link

paulie4 commented Oct 6, 2021

What is the problem?

We used to be able to create a route53.ARecord with a recordName referencing an apigw2.DomainName.name, but that stopped working after CDK version 1.123.0 (I haven't tested 1.124.0 and 1.125.0, but it definitely stopped working in 1.126.0).

The problem is that apigw2.DomainName.name used to be passed as a value, so the route53.ARecord code could tell that the domain name was a subdomain of the Hosted Zone name, but now that the name is passed as a reference, it can't, so instead of the ARecord being for subdomain.hosted.zone.com, it does it for subdomain.hosted.zone.com.hosted.zone.com.

Reproduction Steps

import aws_cdk.aws_route53_targets as targets
from aws_cdk import core as cdk
from aws_cdk import aws_apigatewayv2 as apigw2
from aws_cdk import aws_certificatemanager as acm
from aws_cdk import aws_route53 as route53

HOSTED_ZONE_NAME = 'hosted.zone.com'
ACCOUNT = '123456789'
REGION = 'us-east-2'


class CdkTestDomnameRoute53Stack(cdk.Stack):
    def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        hostedZone = route53.HostedZone.from_lookup(self, "HZONE", domain_name=HOSTED_ZONE_NAME)

        cert = acm.DnsValidatedCertificate(
            self, 'WildcardCertificate',
            hosted_zone=hostedZone,
            domain_name=f'*.{HOSTED_ZONE_NAME}',
            validation=acm.CertificateValidation.from_dns(hosted_zone=hostedZone)
        )

        dn = apigw2.DomainName(
            self, 'DomainName',
            domain_name=f'subdomain.{HOSTED_ZONE_NAME}',
            certificate=cert
        )

        a_record = route53.ARecord(
            self, 'DnsAlias',
            record_name=dn.name,
            zone=hostedZone,
            target=route53.RecordTarget.from_alias(targets.ApiGatewayv2DomainProperties(
                dn.regional_domain_name, dn.regional_hosted_zone_id
            ))
        )

        cdk.CfnOutput(self, 'OutputDomainName', value=dn.name)
        cdk.CfnOutput(self, 'OutputDnsAlias', value=a_record.domain_name)


app = cdk.App()
env = cdk.Environment(account=ACCOUNT, region=REGION)
CdkTestDomnameRoute53Stack(app, 'CdkTestDomnameRoute53Stack', env=env)

app.synth()

What did you expect to happen?

Both OutputDomainName and OutputDnsAlias should both have the same value.

What actually happened?

Instead of subdomain.hosted.zone.com, the ARecord's domain name is subdomain.hosted.zone.com.hosted.zone.com.

CDK CLI Version

1.126.0 (build f004e1a)

Framework Version

No response

Node.js Version

v14.16.0

OS

Windows

Language

Python

Language Version

Python 3.9.6

Other information

No response

@paulie4 paulie4 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Oct 6, 2021
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2 Related to Amazon API Gateway v2 label Oct 6, 2021
@peterwoodworth
Copy link
Contributor

peterwoodworth commented Oct 6, 2021

Looks like this is the change that broke you, the change to use the ref here was intentional. I wonder if @nija-at considered cases like these

@peterwoodworth peterwoodworth removed the needs-triage This issue or PR still needs to be triaged. label Oct 6, 2021
@ppena-LiveData
Copy link

Oh yeah, ensuring proper dependency ordering is definitely important... Would it make sense for the ARecord code to notice that the reference is for a DomainName and then dig down to see what its domain name is to decide if it already includes the Hosted Zone record name or not?

@nija-at
Copy link
Contributor

nija-at commented Oct 7, 2021

Hi,

Thanks for filing this bug. Unfortunately, we had to change this from a value to a reference. Changing this back would break a few more users who are now depending on the new 'Depends On' behavior.

Instead, I suggest not using domain.name reference for your Route53 record and instead passing the original value you expect there.

@nija-at nija-at added the closing-soon This issue will automatically close in 4 days unless further comments are made. label Oct 7, 2021
@ppena-LiveData
Copy link

Instead of changing it back to value from a reference, would it be hard for the ARecord code to notice that the reference is for a DomainName and then dig down to see what its domain name is to decide if it already includes the Hosted Zone record name or not?

@github-actions github-actions bot removed the closing-soon This issue will automatically close in 4 days unless further comments are made. label Oct 12, 2021
@nija-at
Copy link
Contributor

nija-at commented Oct 14, 2021

@ppena-LiveData - Since the value is a CloudFormation Ref, it would be not be possible for the CDK route53 module to determine the final value until deployment, hence, unable to perform this evaluation.

@njlynch - correct me if my statement is incorrect.

@njlynch
Copy link
Contributor

njlynch commented Oct 14, 2021

Yes, and no.

The utility function that processes the given domain name doesn't do any special-casing or handling of Tokens. We could simply take Tokens at their face value, but that may be prone to introducing errors, as the domain name must be a fully-qualified domain name, including trailing ..

The somewhat unintended side-effect is that the function checks for the trailing ., and if present, accepts the value as-is:

if (providedName.endsWith('.')) {
return providedName;

This means if you suffix your domain name (e.g,. '${dn.regional_domain_name}.'), it won't append the hosted zone name to it, and you'll get the result you expect.

@nija-at
Copy link
Contributor

nija-at commented Oct 14, 2021

@njlynch - even if the function did handle the token case correctly, since the token resolves to a CloudFormation Ref, it would still not be able to check if the domain name includes the hosted zone. Not until during deployment, no?

@njlynch
Copy link
Contributor

njlynch commented Oct 28, 2021

Correct. We'd need to define behavior such that the Token is just absolutely trusted (as I mentioned above, potentially error-prone), or build something as complex as a custom resource to validate.

@nija-at
Copy link
Contributor

nija-at commented Oct 28, 2021

@ppena-LiveData - looks like this is not achievable trivially. As suggested above, the best option is to pass the domain name directly rather than reference it via domain.name.

@nija-at nija-at closed this as completed Oct 28, 2021
@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-apigatewayv2 Related to Amazon API Gateway v2 bug This issue is a bug.
Projects
None yet
Development

No branches or pull requests

5 participants