Search code examples
amazon-route53aws-cdk

AWS CDK: How can I create an alias of a record in the same hosted zone?


I am trying to create an alias in my hosted zone for a non-www to point to a www record.

In console, I just create an A record, set it as an alias, and choose Alias to another record in this hosted zone but I don't see a way to do this in the CDK.

This won't work as it's not an IP address:

    // Add non-www alias for domain
    const _nonWwwARecord = new ARecord(this, 'non-www-to-www', {
      zone: _prodHostedZone,
      target: RecordTarget.fromValues(`www.${DOMAIN}`),
      ttl: Duration.minutes(5),
    });

I get:

[Invalid Resource Record: 'FATAL problem: ARRDATAIllegalIPv4Address (Value is not a valid IPv4 address)

If I attempt to use RecordTarget from @aws-cdk/aws-route53-targets I don't see any options for a record in the same hosted zone.

Not sure why this is so confusing but I cannot seem to get past this...

It may help to understand that the www record is set by ApplicationLoadBalancedFargateService.


Solution

  • The package @aws-cdk/aws-rout53-targets currently doesn't include a target for another Route 53 record.

    In the meantime you can unblock yourself with a custom implementation for bind():

    // Create a A record that is an alias for `otherRecord`
    new ARecord(this, 'Alias', {
      zone: myZone,
      target: RecordTarget.fromAlias({
        bind: () => ({
          dnsName: otherRecord.domainName,
          hostedZoneId: myZone.hostedZoneId,
        }),
      }),
    });