Skip to content

Commit

Permalink
feat(aws-route53-targets): aws-apigatewayv2 target
Browse files Browse the repository at this point in the history
  • Loading branch information
johanneswuerbach committed Sep 4, 2020
1 parent 4de68c0 commit 783137f
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 5 deletions.
15 changes: 11 additions & 4 deletions packages/@aws-cdk/aws-route53-targets/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ This library contains Route53 Alias Record targets for:
// or - route53.RecordTarget.fromAlias(new alias.ApiGatewayDomain(domainName)),
});
```
* API Gateway V2 custom domains
```ts
new route53.ARecord(this, 'AliasRecord', {
zone,
target: route53.RecordTarget.fromAlias(new alias.ApiGatewayv2Domain(domainName)),
});
```
* CloudFront distributions
```ts
new route53.ARecord(this, 'AliasRecord', {
Expand Down Expand Up @@ -55,17 +62,17 @@ For example, if the Amazon-provided DNS for the load balancer is `ALB-xxxxxxx.us
```
* S3 Bucket Website:

**Important:** The Bucket name must strictly match the full DNS name.
See [the Developer Guide](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/getting-started.html) for more info.
**Important:** The Bucket name must strictly match the full DNS name.
See [the Developer Guide](https://docs.aws.amazon.com/Route53/latest/DeveloperGuide/getting-started.html) for more info.
```ts
const [recordName, domainName] = ['www', 'example.com'];
const bucketWebsite = new Bucket(this, 'BucketWebsite', {
bucketName: [recordName, domainName].join('.'), // www.example.com
publicReadAccess: true,
websiteIndexDocument: 'index.html',
});
const zone = HostedZone.fromLookup(this, 'Zone', {domainName}); // example.com
new route53.ARecord(this, 'AliasRecord', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as route53 from '@aws-cdk/aws-route53';
* Defines an API Gateway domain name as the alias target.
*
* Use the `ApiGateway` class if you wish to map the alias to an REST API with a
* domain name defined throug the `RestApiProps.domainName` prop.
* domain name defined through the `RestApiProps.domainName` prop.
*/
export class ApiGatewayDomain implements route53.IAliasRecordTarget {
constructor(private readonly domainName: apig.IDomainName) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as apigv2 from '@aws-cdk/aws-apigatewayv2';
import * as route53 from '@aws-cdk/aws-route53';

/**
* Defines an API Gateway V2 domain name as the alias target.
*/
export class ApiGatewayv2Domain implements route53.IAliasRecordTarget {
constructor(private readonly domainName: apigv2.IDomainName) { }

public bind(_record: route53.IRecordSet): route53.AliasRecordTargetConfig {
return {
dnsName: this.domainName.regionalDomainName,
hostedZoneId: this.domainName.regionalHostedZoneId,
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-route53-targets/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './api-gateway-domain-name';
export * from './api-gatewayv2-domain-name';
export * from './bucket-website-target';
export * from './classic-load-balancer-target';
export * from './cloudfront-target';
Expand Down
2 changes: 2 additions & 0 deletions packages/@aws-cdk/aws-route53-targets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
},
"dependencies": {
"@aws-cdk/aws-apigateway": "0.0.0",
"@aws-cdk/aws-apigatewayv2": "0.0.0",
"@aws-cdk/aws-cloudfront": "0.0.0",
"@aws-cdk/aws-cognito": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
Expand All @@ -85,6 +86,7 @@
"homepage": "https://github.com/aws/aws-cdk",
"peerDependencies": {
"@aws-cdk/aws-apigateway": "0.0.0",
"@aws-cdk/aws-apigatewayv2": "0.0.0",
"@aws-cdk/aws-cloudfront": "0.0.0",
"@aws-cdk/aws-cognito": "0.0.0",
"@aws-cdk/aws-ec2": "0.0.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect as expectStack, haveResource } from '@aws-cdk/assert';
import * as apigwv2 from '@aws-cdk/aws-apigatewayv2';
import * as acm from '@aws-cdk/aws-certificatemanager';
import * as route53 from '@aws-cdk/aws-route53';
import { Stack } from '@aws-cdk/core';
import * as targets from '../lib';

test('targets.ApiGatewayv2Domain can be used to directly reference a domain', () => {
// GIVEN
const stack = new Stack();
const domainName = 'example.com';
const cert = new acm.Certificate(stack, 'cert', { domainName });
const dn = new apigwv2.DomainName(stack, 'DN', {
domainName,
certificate: cert,
});
const zone = new route53.HostedZone(stack, 'zone', {
zoneName: 'example.com',
});

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

// THEN
expectStack(stack).to(haveResource('AWS::Route53::RecordSet', {
Name: 'example.com.',
Type: 'A',
AliasTarget: {
DNSName: {
'Fn::GetAtt': [
'domainFBFFA2F6',
'RegionalDomainName',
],
},
HostedZoneId: {
'Fn::GetAtt': [
'domainFBFFA2F6',
'RegionalHostedZoneId',
],
},
},
HostedZoneId: {
Ref: 'zoneEB40FF1E',
},
}));
});

0 comments on commit 783137f

Please sign in to comment.