Skip to content

Commit

Permalink
feat(route53-targets): Add AppSync target
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottRobinson03 committed Nov 1, 2024
1 parent 4e715b8 commit a3d6535
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/aws-cdk-lib/aws-route53-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ This library contains Route53 Alias Record targets for:
});
```

* AppSync custom domains

```ts
import * as appsync from 'aws-cdk-lib/aws-appsync';

declare const zone: route53.HostedZone;
declare const graphqlApi: appsync.GraphqlApi;

new route53.ARecord(this, 'AliasRecord', {
zone,
target: route53.RecordTarget.fromAlias(new targets.AppSync(graphqlApi))
});

* CloudFront distributions

```ts
Expand Down
23 changes: 23 additions & 0 deletions packages/aws-cdk-lib/aws-route53-targets/lib/appsync-target.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { CloudFrontTarget } from './cloudfront-target';
import { GraphqlApi } from '../../aws-appsync';
import {
AliasRecordTargetConfig,
IAliasRecordTarget,
IHostedZone,
IRecordSet,
} from '../../aws-route53';

/**
* Defines an AppSync Graphql API as the alias target. Requires that the domain
* name will be defined through `GraphqlApiProps.domainName`.
*/
export class AppSync implements IAliasRecordTarget {
constructor(private readonly graphqlApi: GraphqlApi) {}

public bind(_record: IRecordSet, _zone?: IHostedZone): AliasRecordTargetConfig {
return {
dnsName: this.graphqlApi.appSyncDomainName,
hostedZoneId: CloudFrontTarget.getHostedZoneId(this.graphqlApi),
};
}
}
1 change: 1 addition & 0 deletions packages/aws-cdk-lib/aws-route53-targets/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './api-gateway-domain-name';
export * from './api-gatewayv2-domain-name';
export * from './appsync-domain-target';
export * from './bucket-website-target';
export * from './elastic-beanstalk-environment-target';
export * from './classic-load-balancer-target';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { join } from 'path';
import { Template } from '../../assertions';
import * as appsync from '../../aws-appsync';
import * as acm from '../../aws-certificatemanager';
import * as route53 from '../../aws-route53';
import { Stack } from '../../core';
import * as targets from '../lib';

test('targets.AppSync can be used to the default domain of an AppSync GraphqlApi', () => {
// GIVEN
const stack = new Stack();
const cert = new acm.Certificate(stack, 'cert', { domainName: 'example.com' });
const api = new appsync.GraphqlApi(stack, 'api', {
definition: appsync.Definition.fromFile(join(__dirname, '..', '..', 'aws-appsync', 'test', 'appsync.test.graphql')),
domainName: {
domainName: 'example.com',
certificate: cert,
},
name: 'api',
});
const zone = new route53.HostedZone(stack, 'zone', {
zoneName: 'example.com',
});

// WHEN
new route53.ARecord(stack, 'A', {
zone,
target: route53.RecordTarget.fromAlias(new targets.AppSync(api)),
});

// THEN
Template.fromStack(stack).hasResourceProperties('AWS::Route53::RecordSet', {
Name: 'example.com.',
Type: 'A',
AliasTarget: {
DNSName: {
'Fn::GetAtt': [
'apiDomainNameBBFE36A4',
'AppSyncDomainName',
],
},
HostedZoneId: {
'Fn::FindInMap': [
'AWSCloudFrontPartitionHostedZoneIdMap',
{ Ref: 'AWS::Partition' },
'zoneId',
],
},
},
HostedZoneId: {
Ref: 'zoneEB40FF1E',
},
});
});

0 comments on commit a3d6535

Please sign in to comment.