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

feat(route53-targets): add AppSync route53 target #31976

Merged
merged 12 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
});

ScottRobinson03 marked this conversation as resolved.
Show resolved Hide resolved
* 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 {
ScottRobinson03 marked this conversation as resolved.
Show resolved Hide resolved
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',
},
});
});
Loading