-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(domains): rework how domains are passed, allow for many, auto-…
…guess zone (#73, #79) Allow for passing multiple domains. Get domains from CLI, automatically select the most fitting one. BREAKING CHANGE: Changed CLI interface, different way to pass domains #73, #79
- Loading branch information
Showing
11 changed files
with
182 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,74 @@ | ||
import { CfnOutput, Stack } from 'aws-cdk-lib' | ||
import { IDistribution } from 'aws-cdk-lib/aws-cloudfront' | ||
import { AaaaRecord, ARecord, IHostedZone, RecordTarget } from 'aws-cdk-lib/aws-route53' | ||
import { AaaaRecord, ARecord, HostedZone, RecordTarget } from 'aws-cdk-lib/aws-route53' | ||
import { CloudFrontTarget } from 'aws-cdk-lib/aws-route53-targets' | ||
import { execSync } from 'child_process' | ||
import { MappedDomain } from '../types' | ||
import { readFileSync } from 'fs' | ||
import { tmpdir } from 'os' | ||
import path from 'path' | ||
|
||
export interface PrepareDomainProps { | ||
domains: string[] | ||
profile?: string | ||
} | ||
|
||
export interface SetupDnsRecordsProps { | ||
dnsPrefix?: string | ||
hostedZone: IHostedZone | ||
domains: MappedDomain[] | ||
cfnDistro: IDistribution | ||
} | ||
|
||
export const setupDnsRecords = (scope: Stack, { dnsPrefix: recordName, hostedZone: zone, cfnDistro }: SetupDnsRecordsProps) => { | ||
// AWS-CDK does not have a way to retrieve the hosted zones in given account, so we need to go around. | ||
const getAvailableHostedZones = (profile?: string): string[] => { | ||
const tmpDir = path.join(tmpdir(), 'hosted-zones.json') | ||
const profileFlag = profile ? `--profile ${profile}` : '' | ||
execSync(`aws route53 list-hosted-zones --output json ${profileFlag} > ${tmpDir}`) | ||
const output = JSON.parse(readFileSync(tmpDir, 'utf8')) | ||
return output.HostedZones.map((zone: any) => zone.Name) | ||
} | ||
|
||
const matchDomainToHostedZone = (domainToMatch: string, zones: string[]) => { | ||
const matchedZone = zones.reduce((acc, curr) => { | ||
const matchRegex = new RegExp(`(.*)${curr}$`) | ||
|
||
const isMatching = !!`${domainToMatch}.`.match(matchRegex) | ||
const isMoreSpecific = curr.split('.').length > (acc?.split('.').length ?? 0) | ||
|
||
if (isMatching && isMoreSpecific) { | ||
return curr | ||
} else { | ||
return acc | ||
} | ||
}, null as string | null) | ||
|
||
if (!matchedZone) { | ||
throw new Error(`No hosted zone found for domain: ${domainToMatch}`) | ||
} | ||
|
||
return matchedZone.replace('/.$/', '') | ||
} | ||
|
||
export const prepareDomains = (scope: Stack, { domains, profile }: PrepareDomainProps): MappedDomain[] => { | ||
const zones = getAvailableHostedZones(profile) | ||
|
||
return domains.map((domain, index) => { | ||
const hostedZone = matchDomainToHostedZone(domain, zones) | ||
const recordName = domain.replace(hostedZone, '') | ||
|
||
const zone = HostedZone.fromLookup(scope, `Zone_${index}`, { domainName: hostedZone }) | ||
|
||
return { zone, recordName, domain } | ||
}) | ||
} | ||
|
||
export const setupDnsRecords = (scope: Stack, { domains, cfnDistro }: SetupDnsRecordsProps) => { | ||
const target = RecordTarget.fromAlias(new CloudFrontTarget(cfnDistro)) | ||
|
||
const dnsARecord = new ARecord(scope, 'AAliasRecord', { recordName, target, zone }) | ||
const dnsAaaaRecord = new AaaaRecord(scope, 'AaaaAliasRecord', { recordName, target, zone }) | ||
domains.forEach(({ recordName, zone }, index) => { | ||
const dnsARecord = new ARecord(scope, `AAliasRecord_${index}`, { recordName, target, zone }) | ||
const dnsAaaaRecord = new AaaaRecord(scope, `AaaaAliasRecord_${index}`, { recordName, target, zone }) | ||
|
||
new CfnOutput(scope, 'dns_A_Record', { value: dnsARecord.domainName }) | ||
new CfnOutput(scope, 'dns_AAAA_Record', { value: dnsAaaaRecord.domainName }) | ||
new CfnOutput(scope, `dns_A_Record_${index}`, { value: dnsARecord.domainName }) | ||
new CfnOutput(scope, `dns_AAAA_Record_${index}`, { value: dnsAaaaRecord.domainName }) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,17 @@ | ||
import { CfnOutput, Stack } from 'aws-cdk-lib' | ||
import { IHostedZone } from 'aws-cdk-lib/aws-route53' | ||
import { HttpsRedirect } from 'aws-cdk-lib/aws-route53-patterns' | ||
import { MappedDomain } from '../types' | ||
|
||
export interface SetupApexRedirectProps { | ||
sourceHostedZone: IHostedZone | ||
targetDomain: string | ||
domain: MappedDomain | ||
} | ||
|
||
export const setupApexRedirect = (scope: Stack, { sourceHostedZone, targetDomain }: SetupApexRedirectProps) => { | ||
export const setupApexRedirect = (scope: Stack, { domain }: SetupApexRedirectProps) => { | ||
new HttpsRedirect(scope, `ApexRedirect`, { | ||
// Currently supports only apex (root) domain. | ||
zone: sourceHostedZone, | ||
targetDomain, | ||
zone: domain.zone, | ||
targetDomain: domain.recordName, | ||
}) | ||
|
||
new CfnOutput(scope, 'RedirectFrom', { value: sourceHostedZone.zoneName }) | ||
new CfnOutput(scope, 'RedirectFrom', { value: domain.zone.zoneName }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.