Skip to content

aws-route53-targets: Add Global Accelerator target for Route53 alias #12839

@luketn

Description

@luketn

Add a new Route53 alias target implementation for Global Accelerator.

Use Case

You can create a Global Accelerator with CDK as documented here:
https://docs.aws.amazon.com/cdk/api/latest/docs/aws-globalaccelerator-readme.html

However when you try to create a Route53 alias to the Global Accelerator as discussed here:
"To use your custom domain name with Global Accelerator when you use Route 53 as your DNS service, you create an alias record that points your custom domain name to the DNS name assigned to your accelerator. An alias record is a Route 53 extension to DNS."
https://docs.aws.amazon.com/global-accelerator/latest/dg/dns-addressing-custom-domains.mapping-your-custom-domain.html

There is CloudFormation support for it as discussed here:
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-route53-aliastarget-1.html

But no CDK support.

Proposed Solution

Add a new CDK alias target class 'GlobalAcceleratorTarget', which simplifies creating an alias target to a Global Accelerator.

The Hosted Zone should be set as per the documentation here:
"Amazon Route 53 Hosted Zone ID: Z2BJ6XQ5FK7U4H"
https://docs.aws.amazon.com/general/latest/gr/global_accelerator.html

The Global Accelerator could be passed in the constructor, either as an object or a string (the DNS).

Working implementation:

import {IRecordSet, IAliasRecordTarget, AliasRecordTargetConfig} from "@aws-cdk/aws-route53";
import {IAccelerator} from "@aws-cdk/aws-globalaccelerator";

export class GlobalAcceleratorTarget implements IAliasRecordTarget {
    /**
     * The hosted zone Id if using an alias record in Route53.
     * This value never changes.
     * Ref: https://docs.aws.amazon.com/general/latest/gr/global_accelerator.html
     */
    public static readonly GLOBAL_ACCELERATOR_ZONE_ID = 'Z2BJ6XQ5FK7U4H';

    /**
     * Create an Alias Target for a Global Accelerator.
     *
     * If passing a string value, it must be a valid DNS name for an existing Global Accelerator. e.g. xyz.awsglobalaccelerator.com
     * If passing an instance of an accelerator created within CDK, the accelerator.dnsName property will be used as the target.
     */
    constructor(private readonly accelerator: string | IAccelerator) {
    }

    bind(record: IRecordSet): AliasRecordTargetConfig {
        let acceleratorDomainName;
        if (typeof this.accelerator === "string") {
            acceleratorDomainName = this.accelerator;
        } else {
            acceleratorDomainName = this.accelerator.dnsName;
        }
        return {
            hostedZoneId: GlobalAcceleratorTarget.GLOBAL_ACCELERATOR_ZONE_ID,
            dnsName: acceleratorDomainName
        };
    }
}

This is a 🚀 Feature Request

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions