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.65k stars 3.91k forks source link

route53-targets: Add support for GraphqlApiDomainTarget #26109

Open thesnups opened 1 year ago

thesnups commented 1 year ago

Describe the feature

Implement GraphqlApiDomainTarget in aws-cdk-lib/aws-route53-targets to facilitate easily configuring A records for AppSync APIs with custom domains.

Use Case

https://github.com/aws/aws-cdk/tree/main/packages/aws-cdk-lib/aws-route53-targets is missing an IAliasRecordTarget implementation for AWS AppSync GraphQL APIs. The AppSync docs suggest the use of a CNAME for custom domains, but that is not an option for me because I cannot create a CNAME at the apex of the hosted zone. Also, the Route 53 console UI supports AppSync domains as an alias target, and that should be reflected in CDK.

Proposed Solution

I am currently using the following workaround in my project:

import { GraphqlApi } from 'aws-cdk-lib/aws-appsync';
import {
  AliasRecordTargetConfig,
  IAliasRecordTarget,
  IHostedZone,
  IRecordSet,
} from 'aws-cdk-lib/aws-route53';
import { CloudFrontTarget } from 'aws-cdk-lib/aws-route53-targets';

class GraphqlApiDomainTarget implements IAliasRecordTarget {
  constructor(private readonly graphqlApi: GraphqlApi) {}

  public bind(_record: IRecordSet, _zone?: IHostedZone): AliasRecordTargetConfig {
    return {
      dnsName: this.graphqlApi.appSyncDomainName,
      hostedZoneId: CloudFrontTarget.getHostedZoneId(this.graphqlApi),
    };
  }
}

I based this implementation on the UserPoolDomainTarget, and it seems to work. Unsure if there is a different, preferable approach. Here it is in use:

new ARecord(this, 'GraphqlApiDomainARecord', {
  target: RecordTarget.fromAlias(new GraphqlApiDomainTarget(myGraphqlApi)),
  zone: myHostedZone,
});

Other Information

No response

Acknowledgements

CDK version used

2.85.0

Environment details (OS name and version, etc.)

Mac OS

pahud commented 1 year ago

It makes sense to me. We welcome and appreciate your PR if it's ready. Thank you for your contribution.

fabio-vitali-docebo commented 1 year ago

It also makes sense for multi-region deployment, in the context of a disaster recovery implementation.

ShivamJoker commented 1 year ago

I also had to use the workaround provided by @thesnups. When will we get the built in support for Appsync domains?