Skip to content

Commit

Permalink
feat(aws-route53): route53 Alias record support (#1131)
Browse files Browse the repository at this point in the history
Support creating alias records to Application Load Balancers and CloudFront distributions.
  • Loading branch information
clareliguori authored and rix0rrr committed Nov 12, 2018
1 parent c5fd907 commit 72f0124
Show file tree
Hide file tree
Showing 11 changed files with 636 additions and 2 deletions.
10 changes: 9 additions & 1 deletion packages/@aws-cdk/aws-cloudfront/lib/web_distribution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import route53 = require('@aws-cdk/aws-route53');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import { cloudformation } from './cloudfront.generated';
Expand Down Expand Up @@ -476,7 +477,7 @@ interface BehaviorWithOrigin extends Behavior {
*
*
*/
export class CloudFrontWebDistribution extends cdk.Construct {
export class CloudFrontWebDistribution extends cdk.Construct implements route53.IAliasRecordTarget {

/**
* The hosted zone Id if using an alias record in Route53.
Expand Down Expand Up @@ -659,6 +660,13 @@ export class CloudFrontWebDistribution extends cdk.Construct {
this.distributionId = distribution.distributionId;
}

public asAliasRecordTarget(): route53.AliasRecordTargetProps {
return {
hostedZoneId: this.aliasHostedZoneId,
dnsName: this.domainName
};
}

private toBehavior(input: BehaviorWithOrigin, protoPolicy?: ViewerProtocolPolicy) {
let toReturn = {
allowedMethods: this.METHOD_LOOKUP_MAP[input.allowedMethods || CloudFrontAllowedMethods.GET_HEAD],
Expand Down
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-cloudfront/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@aws-cdk/aws-certificatemanager": "^0.15.2",
"@aws-cdk/aws-iam": "^0.15.2",
"@aws-cdk/aws-kms": "^0.15.2",
"@aws-cdk/aws-route53": "^0.15.2",
"@aws-cdk/aws-s3": "^0.15.2",
"@aws-cdk/cdk": "^0.15.2"
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"Resources": {
"HostedZoneDB99F866": {
"Type": "AWS::Route53::HostedZone",
"Properties": {
"Name": "test.public."
}
},
"HostedZoneAlias40D2E006": {
"Type": "AWS::Route53::RecordSet",
"Properties": {
"Name": "_foo.test.public.",
"Type": "A",
"AliasTarget": {
"DNSName": {
"Fn::GetAtt": [
"MyDistributionCFDistributionDE147309",
"DomainName"
]
},
"HostedZoneId": "Z2FDTNDATAQYW2"
},
"HostedZoneId": {
"Ref": "HostedZoneDB99F866"
}
}
},
"Bucket83908E77": {
"Type": "AWS::S3::Bucket"
},
"MyDistributionCFDistributionDE147309": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"CacheBehaviors": [],
"DefaultCacheBehavior": {
"AllowedMethods": [
"GET",
"HEAD"
],
"CachedMethods": [
"GET",
"HEAD"
],
"ForwardedValues": {
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"TargetOriginId": "origin1",
"ViewerProtocolPolicy": "redirect-to-https"
},
"DefaultRootObject": "index.html",
"Enabled": true,
"HttpVersion": "http2",
"IPV6Enabled": true,
"Origins": [
{
"DomainName": {
"Fn::GetAtt": [
"Bucket83908E77",
"DomainName"
]
},
"Id": "origin1",
"S3OriginConfig": {}
}
],
"PriceClass": "PriceClass_100",
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true
}
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import route53 = require('@aws-cdk/aws-route53');
import s3 = require('@aws-cdk/aws-s3');
import cdk = require('@aws-cdk/cdk');
import cloudfront = require('../lib');

const app = new cdk.App();

const stack = new cdk.Stack(app, 'aws-cdk-cloudfront');

const zone = new route53.PublicHostedZone(stack, 'HostedZone', { zoneName: 'test.public' });

const sourceBucket = new s3.Bucket(stack, 'Bucket');

const distribution = new cloudfront.CloudFrontWebDistribution(stack, 'MyDistribution', {
originConfigs: [
{
s3OriginSource: {
s3BucketSource: sourceBucket
},
behaviors : [ {isDefaultBehavior: true}]
}
]
});

new route53.AliasRecord(zone, 'Alias', {
recordName: '_foo',
target: distribution
});

app.run();
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ec2 = require('@aws-cdk/aws-ec2');
import route53 = require('@aws-cdk/aws-route53');
import cdk = require('@aws-cdk/cdk');
import { cloudformation } from '../elasticloadbalancingv2.generated';
import { Attributes, ifUndefined, renderAttributes } from './util';
Expand Down Expand Up @@ -44,7 +45,7 @@ export interface BaseLoadBalancerProps {
/**
* Base class for both Application and Network Load Balancers
*/
export abstract class BaseLoadBalancer extends cdk.Construct {
export abstract class BaseLoadBalancer extends cdk.Construct implements route53.IAliasRecordTarget {
/**
* The canonical hosted zone ID of this load balancer
*
Expand Down Expand Up @@ -135,4 +136,10 @@ export abstract class BaseLoadBalancer extends cdk.Construct {
this.setAttribute(key, undefined);
}

public asAliasRecordTarget(): route53.AliasRecordTargetProps {
return {
hostedZoneId: this.canonicalHostedZoneId,
dnsName: this.dnsName
};
}
}
1 change: 1 addition & 0 deletions packages/@aws-cdk/aws-elasticloadbalancingv2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"@aws-cdk/aws-codedeploy-api": "^0.15.2",
"@aws-cdk/aws-ec2": "^0.15.2",
"@aws-cdk/aws-iam": "^0.15.2",
"@aws-cdk/aws-route53": "^0.15.2",
"@aws-cdk/aws-s3": "^0.15.2",
"@aws-cdk/cdk": "^0.15.2"
},
Expand Down
Loading

0 comments on commit 72f0124

Please sign in to comment.