-
Notifications
You must be signed in to change notification settings - Fork 2
feature: Add package for creating/managing Route53 hosted zone and associated optional resources #1575
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
Merged
TheOrangePuff
merged 4 commits into
aligent:main
from
finnholland:feature/domain-hosting-stack
Jan 20, 2026
Merged
feature: Add package for creating/managing Route53 hosted zone and associated optional resources #1575
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@aligent/cdk-domain-hosting": major | ||
| --- | ||
|
|
||
| Initial release of new domain-hosting package |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # TypeScript source files | ||
| *.ts | ||
| !*.d.ts | ||
|
|
||
| # But keep Lambda handler TypeScript files | ||
| !lib/handlers/**/*.ts | ||
|
|
||
| # TypeScript configuration | ||
| tsconfig*.json | ||
|
|
||
| # Jest configuration | ||
| jest.config.ts | ||
|
|
||
| # Nx project configuration | ||
| project.json | ||
|
|
||
| # Test files | ||
| test/ | ||
| **/*.test.ts | ||
| **/*.test.js | ||
| **/*.spec.ts | ||
| **/*.spec.js | ||
|
|
||
| # Development dependencies | ||
| node_modules/ | ||
|
|
||
| # Keep these files in published package: | ||
| # *.js (compiled JavaScript) | ||
| # *.d.ts (TypeScript declarations) | ||
| # README.md | ||
| # CHANGELOG.md | ||
| # package.json | ||
| # lib/handlers/ (Lambda handler files) | ||
| # docs/ |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| # Aligent AWS Domain Hosting | ||
|
|
||
| ## Overview | ||
|
|
||
|    | ||
|
|
||
| A simple package used for creating and/or managing a hosted zone in AWS. Handy for setting up a cloudfront site from scratch when there's no existing domain management in place or integrating into an already existing setup. | ||
| You are able to use a domain that was requested through AWS Route53 or an external provider. | ||
|
|
||
| It can be imported and used within CDK applications. By default this construct will create a hosted zone and a certificate to validate a domain against. Records are not created unless explicitly stated. | ||
|
|
||
| It has the following features that can optionally be enabled: | ||
|
|
||
| - Create custom records of `CNAME`, `A`, `AAAA`, `MX`, and `SRV` types | ||
| - Create a certificate for the specified domain (and any subdomains) | ||
|
|
||
| ## If using an external provider | ||
| You will need to set the NS record in your provider's management page to the values in the hosted zone after you run `cdk deploy` as the certificate will not validate otherwise | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| npm install @aligent/cdk-domain-hosting aws-cdk-lib constructs | ||
| ``` | ||
|
|
||
| Or with yarn: | ||
|
|
||
| ```bash | ||
| yarn add @aligent/cdk-static-hosting aws-cdk-lib constructs | ||
| ``` | ||
|
|
||
| ### Peer Dependencies | ||
|
|
||
| This package has peer dependencies on: | ||
| - `aws-cdk-lib` (^2.120.0) | ||
| - `constructs` (^10.0.0) | ||
|
|
||
| Make sure to install compatible versions of these packages in your CDK application. | ||
|
|
||
| ## Usage | ||
| ### `domainName`(string) | ||
| - Domain name for the hosted zone. This is also the base for the certificate that is created. Combined with the subDomainName it is used as the name for the S3 origin and an alternative domain name for the CloudFront distribution | ||
|
|
||
| ### `hostedZoneId?`(string) | ||
| - If you are using a zone that already exists just put its id instead. This will make the CDK update the existing zone in place (it will not remove records that aren't in the code yet) | ||
|
|
||
| Default: **undefined** | ||
|
|
||
| ### `createCertificate?` (boolean) | ||
|
|
||
| - Explicitly state if you want a certificate created for the default domain `domainName` value. Hosted Zones do not require a cert on creation. This is set to true if you pass in `subDomains` | ||
| - The Cert is created in `us-east-1` | ||
|
|
||
| Default: **false** | ||
|
|
||
| ### `certificateArn?` (string) | ||
|
|
||
| - The arn of the certificate to validate against if one already exists. | ||
|
|
||
| Default: **undefined** | ||
|
|
||
| ### `subDomains?` (string) | ||
|
|
||
| - Extra subdomains to add to the certificate for validation. | ||
|
|
||
| Default: **[]** | ||
|
|
||
| ### `records?` (string) | ||
|
|
||
| - Any records to create in the zone, can be of types `CNAME`, `A`, `AAAA`, `MX`, and `SRV` | ||
| - See usage block below for an example | ||
|
|
||
|
|
||
| ## Example | ||
|
|
||
| The following CDK snippet can be used to provision a zone with records and a certificate using this construct. | ||
|
|
||
| ``` | ||
| import { Construct } from "constructs"; | ||
| import { Stack } from "aws-cdk-lib"; | ||
| import { DomainHosting } from "@aligent/cdk-domain-hosting"; | ||
| import { DomainHostingProps } from "../types"; | ||
| import { RecordTarget } from "aws-cdk-lib/aws-route53"; | ||
| import { CloudFrontTarget } from "aws-cdk-lib/aws-route53-targets"; | ||
|
|
||
| export class DomainHostingStack extends Stack { | ||
| constructor(scope: Construct, id: string, props: DomainHostingProps) { | ||
| super(scope, id, props); | ||
|
|
||
| new DomainHosting(this, "DomainHostingStack", { | ||
| domainName: props.domainName, | ||
| subDomains: ['www'], | ||
| records: [ | ||
| { | ||
| type: 'CNAME', | ||
| name: 'www.example.com', | ||
| value: props.distribution.distributionDomainName | ||
| }, | ||
| { | ||
| type: 'A', | ||
| name: 'www.example.com', | ||
| value: RecordTarget.fromAlias(new CloudFrontTarget(props.distribution)) | ||
| }, | ||
| { | ||
| type: 'SRV', | ||
| name: 'xmpp.example.com', | ||
| value: [{ port: 443, priority: 1, hostName: 'xmpp-srv', weight: 10 }] | ||
| }, | ||
| { | ||
| type: 'MX', | ||
| name: 'mail.example.com', | ||
| value: [{ priority: 1, hostName: 'mymail' }] | ||
| }, | ||
| { | ||
| type: 'AAAA', | ||
| name: 'www.example.com', | ||
| value: RecordTarget.fromAlias(new CloudFrontTarget(props.distribution)) | ||
| }, | ||
| ] | ||
| }); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| The below one would be used if you already had a certificate or hosted zone and just wanted to control it with CDK | ||
|
|
||
| ``` | ||
| new DomainHosting(this, "DomainHostingStack", { | ||
| domainName: props.domainName, | ||
| hostedZoneId: 'Z0123ABC', | ||
| certificateArn: 'arn:aws:acm:us-east-1:xyz', | ||
| }) | ||
| ``` | ||
| ### Little note | ||
| If you are using the StaticHostingStack you can get the CloudFront distribution value to pass as an Alias like so: | ||
|
|
||
| #### static-hosting-stack.ts | ||
| ``` | ||
| export class StaticHostingStack extends Stack { | ||
| public readonly distribution: IDistribution; // create a class variable | ||
| ... | ||
| const hosting = new StaticHosting(this, "StaticHostingStack", {...}) // save it to a variable | ||
| this.distribution = hosting.distribution // get the variable here | ||
| } | ||
| ``` | ||
|
|
||
| #### application.ts | ||
| ``` | ||
| const staticHosting = new StaticHostingStack(this, "StaticHostingStack", { | ||
| ... | ||
| }); // from static-hosting-stack.ts class | ||
|
|
||
| const domainHosting = new DomainHostingStack(this, "DomainHostingStack", { | ||
| ...props, | ||
| distribution: staticHosting.distribution // pass in here | ||
| }); | ||
| ``` | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| import { DomainHosting, DomainHostingProps } from "./lib/domain-hosting"; | ||
|
|
||
| export { DomainHosting, DomainHostingProps }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| /* eslint-disable */ | ||
| export default { | ||
| displayName: "domain-hosting", | ||
| preset: "../../jest.preset.js", | ||
| testEnvironment: "node", | ||
| transform: { | ||
| "^.+\\.[tj]s$": ["ts-jest", { tsconfig: "<rootDir>/tsconfig.spec.json" }], | ||
| }, | ||
| moduleFileExtensions: ["ts", "js", "html"], | ||
| coverageDirectory: "../../coverage/packages/domain-hosting", | ||
| }; |
This file contains hidden or 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 |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| import { Construct } from "constructs"; | ||
| import { | ||
| Certificate, | ||
| DnsValidatedCertificate, | ||
| } from "aws-cdk-lib/aws-certificatemanager"; | ||
| import { | ||
| AaaaRecord, | ||
| ARecord, | ||
| CnameRecord, | ||
| HostedZone, | ||
| MxRecord, | ||
| MxRecordValue, | ||
| PublicHostedZone, | ||
| RecordTarget, | ||
| SrvRecord, | ||
| SrvRecordValue, | ||
| TxtRecord, | ||
| } from "aws-cdk-lib/aws-route53"; | ||
|
|
||
| type DNSRecord = | ||
| | { type: "A"; name: string; value: RecordTarget } | ||
| | { type: "AAAA"; name: string; value: RecordTarget } | ||
| | { type: "CNAME"; name: string; value: string } | ||
| | { type: "TXT"; name: string; value: string[] } | ||
| | { type: "MX"; name: string; value: MxRecordValue[] } | ||
| | { type: "SRV"; name: string; value: SrvRecordValue[] }; | ||
|
|
||
| export interface DomainHostingProps { | ||
| domainName: string; | ||
finnholland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| hostedZoneId?: string; | ||
| createCertificate?: boolean; | ||
| certificateArn?: string; | ||
| subDomains?: string[]; | ||
| records?: DNSRecord[]; | ||
| } | ||
|
|
||
| export class DomainHosting extends Construct { | ||
| constructor(scope: Construct, id: string, props: DomainHostingProps) { | ||
| super(scope, id); | ||
|
|
||
| const { | ||
| domainName, | ||
| hostedZoneId, | ||
| createCertificate = false, | ||
| certificateArn, | ||
| subDomains = [], | ||
| records = [], | ||
| } = props; | ||
|
|
||
| let hostedZone; | ||
| const createCert = createCertificate || subDomains.length > 0; | ||
TheOrangePuff marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!hostedZoneId) { | ||
| hostedZone = new PublicHostedZone(this, "HostedZone", { | ||
| zoneName: domainName, | ||
| }); | ||
| } else { | ||
| hostedZone = HostedZone.fromHostedZoneAttributes(this, "HostedZone", { | ||
| hostedZoneId, | ||
| zoneName: domainName, | ||
| }); | ||
| } | ||
| if (certificateArn) { | ||
| Certificate.fromCertificateArn(this, "DomainCertificate", certificateArn); | ||
| } else if (createCert) { | ||
| // If you are creating a certificate and your domain is hosted outside AWS don't forget to update the NS record in your provider | ||
| // @deprecated but no replacement for generating cert and creating CNAMEs in zone | ||
| new DnsValidatedCertificate(this, "Certificate", { | ||
| domainName: domainName, | ||
| subjectAlternativeNames: [...subDomains.map(s => `${s}.${domainName}`)], | ||
| hostedZone: hostedZone, | ||
| region: "us-east-1", | ||
| }); | ||
| } | ||
| if (records) { | ||
| records.map(r => { | ||
| const recordId = `${r.name}-${r.type}`; | ||
| switch (r.type) { | ||
| case "A": | ||
| new ARecord(this, recordId, { | ||
| zone: hostedZone, | ||
| target: r.value, | ||
| }); | ||
| break; | ||
| case "AAAA": | ||
| new AaaaRecord(this, recordId, { | ||
| zone: hostedZone, | ||
| target: r.value, | ||
| }); | ||
| break; | ||
| case "CNAME": | ||
| new CnameRecord(this, recordId, { | ||
| zone: hostedZone, | ||
| domainName: r.value, | ||
| recordName: r.name, | ||
| }); | ||
| break; | ||
| case "TXT": | ||
| new TxtRecord(this, recordId, { | ||
| zone: hostedZone, | ||
| values: r.value, | ||
| recordName: r.name, | ||
| }); | ||
| break; | ||
| case "MX": | ||
| new MxRecord(this, recordId, { | ||
| zone: hostedZone, | ||
| values: r.value, | ||
| recordName: r.name, | ||
| }); | ||
| break; | ||
| case "SRV": | ||
| new SrvRecord(this, recordId, { | ||
| zone: hostedZone, | ||
| values: r.value, | ||
| recordName: r.name, | ||
| }); | ||
| break; | ||
| default: | ||
| break; | ||
finnholland marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.