Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions src/aws-route53/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Split Horizon DNS CDK Construct

## Overview


## Usage


### Basic Example


### Advanced Example
1 change: 1 addition & 0 deletions src/aws-route53/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './split-horizon-dns';
162 changes: 162 additions & 0 deletions src/aws-route53/split-horizon-dns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import * as cdk from 'aws-cdk-lib';
import * as acm from 'aws-cdk-lib/aws-certificatemanager';
import * as ec2 from 'aws-cdk-lib/aws-ec2';
import * as route53 from 'aws-cdk-lib/aws-route53';
import { Construct } from 'constructs';

const isAliasRecordTarget = (value: route53.IAliasRecordTarget | Array<string>): value is route53.IAliasRecordTarget => {
return !('length' in value);
};

type ARecordArray = Array<route53.ARecord>;

export interface AliasTarget {
readonly target: route53.IAliasRecordTarget | Array<string>;
readonly private?: boolean;
readonly public?: boolean;
readonly ttl?: cdk.Duration;
}

/**
* @param zoneName The DNS name of the zone to create
* @param existingPublicZone An existing public zone to use instead of creating a new one
* @param existingPrivateZone An existing private zone to use instead of creating a new one
* @param disallowPrivateZone Override the default behavior of creating a private zone. Will also block adding private records.
* @param includeCertificate Whether to create an ACM certificate for the zone
* @param certAlternateNames Alternate names to include in the certificate
* @param privateZoneVpcs VPCs to associate with the private zone
* @param targets Targets to create A records for
*/
export interface ISplitHorizonDnsProps {
readonly zoneName: string;
readonly recordName?: string;
readonly existingPublicZone?: route53.IHostedZone;
readonly existingPrivateZone?: route53.IHostedZone;
readonly disallowPrivateZone?: boolean;
readonly certAlternateNames?: Array<string>;
readonly privateZoneVpcs?: Array<ec2.Vpc>;
readonly targets: Array<AliasTarget>;
readonly includeCertificate?: boolean;
}

export interface ISplitHorizonDns {
publicZone: route53.IHostedZone;
privateZone?: route53.IHostedZone;
records: Array<ARecordArray>;
}

interface createHostedZoneProps {
zoneName: string;
vpcs?: Array<ec2.IVpc>;
}

interface createCertificateProps {

domainName: string;
subjectAlternativeNames?: Array<string>;
}
/**
* Creates a public and private zone for a given domain name, and creates A records for the given targets.
* @property publicZone The public zone created
* @property privateZone The private zone created
* @property records The A records created
*/
export class SplitHorizonDns extends Construct implements ISplitHorizonDns {
public publicZone: route53.IHostedZone;

public privateZone?: route53.IHostedZone;

public records: Array<ARecordArray>;

public certificate?: acm.ICertificate;

constructor(scope: Construct, id: string, private props: ISplitHorizonDnsProps) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please make this protected so sub-classes can access this info

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I follow why a protected constructor is needed here?

super(scope, id);

const {
zoneName,
existingPublicZone,
existingPrivateZone,
disallowPrivateZone,
includeCertificate,
certAlternateNames,
privateZoneVpcs,
targets,
} = this.props;

if (existingPublicZone) {
this.publicZone = existingPublicZone;
} else {
this.publicZone = this.createHostedZone('PublicZone', {
zoneName: zoneName,
});
}

if (includeCertificate) {
this.certificate = this.createCertificate('Certificate', {
domainName: zoneName,
subjectAlternativeNames: certAlternateNames,
});
}

if (disallowPrivateZone) {
console.log('Private zone creation is disallowed. Skipping...');
} else if (existingPrivateZone) {
this.privateZone = existingPrivateZone;
} else {
this.privateZone = this.createHostedZone('PrivateZone', {
zoneName: zoneName,
vpcs: privateZoneVpcs,
});
}

this.records = targets.reduce((accu: Array<ARecordArray>, curr: AliasTarget) => {
let target;

if (isAliasRecordTarget(curr.target)) {
target = route53.RecordTarget.fromAlias(curr.target);
} else {
target = route53.RecordTarget.fromValues(...curr.target);
}

if (!curr.private && !curr.public) {
console.error(`Neither public nor private was specified for ${JSON.stringify(curr)}. Omitting...`);
return accu;
}

const records = [] as ARecordArray;
if (curr.public) {
const publicARecord = new route53.ARecord(this, `${curr.target.toString()}PublicARecord`, {
zone: this.publicZone,
target: target,
ttl: curr.ttl,
recordName: props.recordName,
});
records.push(publicARecord);
}

if (disallowPrivateZone) {
console.log('Private zone creation is disallowed. Skipping...');
} else if (curr.private && this.privateZone) {
const privateARecord = new route53.ARecord(this, `${curr.target.toString()}PrivateARecord`, {
zone: this.privateZone,
target: target,
recordName: props.recordName,
});
records.push(privateARecord);
} else if (curr.private && !this.privateZone) {
console.error(`Private zone was specified for ${curr}, but private zone was not created. Omitting...`);
}
accu.push(records);
return accu;
}, [] as Array<ARecordArray>);
}

protected createHostedZone(zoneId: string, props: createHostedZoneProps): route53.IHostedZone {
return new route53.HostedZone(this, zoneId, props);
}

protected createCertificate(certId: string, props: createCertificateProps): acm.ICertificate {
return new acm.Certificate(this, certId, props);
}
}
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Export constructs here
export * as aws_cur from './aws-cur';
export * as aws_ec2 from './aws-ec2';
export * as aws_ec2 from './aws-ec2';
export * as aws_route53 from './aws-route53';
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "36.0.0",
"files": {
"21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22": {
"source": {
"path": "SplitHorizonDnsDefaultTestDeployAssertB4E57D9C.template.json",
"packaging": "file"
},
"destinations": {
"current_account-current_region": {
"bucketName": "cdk-hnb659fds-assets-${AWS::AccountId}-${AWS::Region}",
"objectKey": "21fbb51d7b23f6a6c262b46a9caee79d744a3ac019fd45422d988b96d44b2a22.json",
"assumeRoleArn": "arn:${AWS::Partition}:iam::${AWS::AccountId}:role/cdk-hnb659fds-file-publishing-role-${AWS::AccountId}-${AWS::Region}"
}
}
}
},
"dockerImages": {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"Parameters": {
"BootstrapVersion": {
"Type": "AWS::SSM::Parameter::Value<String>",
"Default": "/cdk-bootstrap/hnb659fds/version",
"Description": "Version of the CDK Bootstrap resources in this environment, automatically retrieved from SSM Parameter Store. [cdk:skip]"
}
},
"Rules": {
"CheckBootstrapVersion": {
"Assertions": [
{
"Assert": {
"Fn::Not": [
{
"Fn::Contains": [
[
"1",
"2",
"3",
"4",
"5"
],
{
"Ref": "BootstrapVersion"
}
]
}
]
},
"AssertDescription": "CDK bootstrap stack version 6 required. Please run 'cdk bootstrap' with a recent version of the CDK CLI."
}
]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"version": "36.0.0",
"files": {
"dd5711540f04e06aa955d7f4862fc04e8cdea464cb590dae91ed2976bb78098e": {
"source": {
"path": "asset.dd5711540f04e06aa955d7f4862fc04e8cdea464cb590dae91ed2976bb78098e",
"packaging": "zip"
},
"destinations": {
"697863601562-us-east-1": {
"bucketName": "cdk-hnb659fds-assets-697863601562-us-east-1",
"objectKey": "dd5711540f04e06aa955d7f4862fc04e8cdea464cb590dae91ed2976bb78098e.zip",
"region": "us-east-1",
"assumeRoleArn": "arn:${AWS::Partition}:iam::697863601562:role/cdk-hnb659fds-file-publishing-role-697863601562-us-east-1"
}
}
},
"251d0dc1993c060b2439b54cb243f4ead92e308a2b7c7a5f0c5e0e3a565d6d51": {
"source": {
"path": "SplitHorizonDnsStack.template.json",
"packaging": "file"
},
"destinations": {
"697863601562-us-east-1": {
"bucketName": "cdk-hnb659fds-assets-697863601562-us-east-1",
"objectKey": "251d0dc1993c060b2439b54cb243f4ead92e308a2b7c7a5f0c5e0e3a565d6d51.json",
"region": "us-east-1",
"assumeRoleArn": "arn:${AWS::Partition}:iam::697863601562:role/cdk-hnb659fds-file-publishing-role-697863601562-us-east-1"
}
}
}
},
"dockerImages": {}
}
Loading
Loading