Skip to content

Commit

Permalink
Merge pull request #2 from coltenkrauter/development
Browse files Browse the repository at this point in the history
Add DNS stack
  • Loading branch information
Colten Krauter authored Sep 6, 2022
2 parents c5162c2 + c5c9909 commit 161688c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 45 deletions.
7 changes: 6 additions & 1 deletion cdk.context.json
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
{}
{
"hosted-zone:account=775126750502:domainName=rememberval.com:region=us-east-1": {
"Id": "/hostedzone/Z10280523GOIQ59UKGL5W",
"Name": "rememberval.com."
}
}
15 changes: 14 additions & 1 deletion cdk/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { writeJSON } from '@bevry/jsonfile';

import { getConfig, STAGE } from './config';
import { Next } from './stacks/next';
import { DNS } from './stacks/dns';

const builder = new Builder('.', './build', { args: ['build'] });
const config = getConfig();
Expand All @@ -19,6 +20,17 @@ const main = async () => {
// Build the NextJS app
await builder.build();

const dNSStack = new DNS(app, `${config.codenameCapitalized}DNS`, {
terminationProtection: config.isProd,
env: {
account: process.env.AWS_DEFAULT_ACCOUNT_ID,
region: process.env.AWS_DEFAULT_REGION || 'us-east-1',
},
analyticsReporting: true,
description: 'The DNS stack',
config,
});

// Deploy the NextJS app
new Next(app, `${config.prefixCamelCase}Next`, {
terminationProtection: config.isProd,
Expand All @@ -29,7 +41,8 @@ const main = async () => {
analyticsReporting: true,
description: 'The Next stack',
config,
});
zoneId: dNSStack.zoneId,
}).addDependency(dNSStack);
};

main();
4 changes: 3 additions & 1 deletion cdk/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ const prefixKebabCase = `${stage}-${codename}-`;
export interface Config {
bucketConfigName: string;
codename: string;
codenameCapitalized: string;
dbMailingList: string;
developerEmail: string;
domainBase: string;
Expand All @@ -84,7 +85,7 @@ export interface Config {
githubRepository: string;
githubServerUrl: string;
githubSha: string;
hCaptchaSecret: string;
hCaptchaSecret: string;
isBranch: boolean;
isMerge: boolean;
isProd: boolean;
Expand All @@ -102,6 +103,7 @@ export const getConfig = () => {
return {
bucketConfigName: `${prefixKebabCase}config-bucket`,
codename,
codenameCapitalized,
dbMailingList,
developerEmail,
domainBase,
Expand Down
73 changes: 73 additions & 0 deletions cdk/stacks/dns.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { Certificate, CertificateValidation } from 'aws-cdk-lib/aws-certificatemanager';
import { Construct } from 'constructs';
import { HostedZone, ARecord, RecordTarget, CnameRecord, NsRecord } from 'aws-cdk-lib/aws-route53';
import { Metric } from 'aws-cdk-lib/aws-cloudwatch';
import { Route53RecordTarget } from 'aws-cdk-lib/aws-route53-targets';
import { Stack, StackProps, Duration, CfnOutput, RemovalPolicy } from 'aws-cdk-lib';

import { Config } from '../config';

interface DNSProps extends StackProps {
config: Config;
}

export class DNS extends Stack {
readonly zoneId;
constructor(scope: Construct, id: string, props: DNSProps) {
super(scope, id, props);

// DNS & certs
const zone = new HostedZone(this, `${props.config.codenameCapitalized}HostedZone`, {
zoneName: props.config.domainBase,
});
zone.applyRemovalPolicy(RemovalPolicy.DESTROY);

this.zoneId = zone.hostedZoneId;
const metric = new Metric({
namespace: 'AWS/Route53',
metricName: 'DNSQueries',
dimensionsMap: {
HostedZoneId: zone.hostedZoneId,
}
});

// const record =vnew ARecord(this, 'AliasRecord', {
// zone,
// target: RecordTarget.fromAlias(new Route53RecordTarget(props.config.domainBase),
// deleteExisting: true,
// ttl: Duration.minutes(5),
// });
// record.applyRemovalPolicy(RemovalPolicy.DESTROY);

// const cname = new CnameRecord(this, `${props.config.stage}CnameRecord`, {
// recordName: props.config.stage,
// zone,
// domainName: props.config.domainBase,
// deleteExisting: true,
// ttl: Duration.minutes(5),
// });
// cname.applyRemovalPolicy(RemovalPolicy.DESTROY);

// const ns = new NsRecord(this, 'NSRecord', {
// zone,
// recordName: props.config.domainBase,
// values: [
// // Get these from the AWS > Route53 > Registered domains > <domain_name> > Name servers
// 'ns-1214.awsdns-23.org.',
// 'ns-191.awsdns-23.com.',
// 'ns-1640.awsdns-13.co.uk.',
// 'ns-790.awsdns-34.net.',
// ],
// deleteExisting: true,
// ttl: Duration.minutes(5),
// });
// ns.applyRemovalPolicy(RemovalPolicy.DESTROY);

// const certificate = new Certificate(this, `${id}Certificate`, {
// domainName: props.config.domainBase,
// subjectAlternativeNames: [`www.${props.config.domainBase}`],
// validation: CertificateValidation.fromDns(zone),
// });
// certificate.applyRemovalPolicy(RemovalPolicy.DESTROY);
}
}
45 changes: 3 additions & 42 deletions cdk/stacks/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,18 @@ import { Config } from '../config';

interface NextProps extends StackProps {
config: Config;
zoneId: string;
}

export class Next extends Stack {
constructor(scope: Construct, id: string, props: NextProps) {
super(scope, id, props);

// DNS & certs
const zone = new HostedZone(this, `${id}HostedZone`, {
const zone = HostedZone.fromHostedZoneAttributes(this, props.zoneId, {
zoneName: props.config.domainBase,
hostedZoneId: props.zoneId,
});
zone.applyRemovalPolicy(RemovalPolicy.DESTROY);

const metric = new Metric({
namespace: 'AWS/Route53',
metricName: 'DNSQueries',
dimensionsMap: {
HostedZoneId: zone.hostedZoneId
}
});

// const record =vnew ARecord(this, 'AliasRecord', {
// zone,
// target: RecordTarget.fromAlias(new Route53RecordTarget(props.config.domainBase),
// deleteExisting: true,
// ttl: Duration.minutes(5),
// });
// record.applyRemovalPolicy(RemovalPolicy.DESTROY);

// const cname = new CnameRecord(this, `${props.config.stage}CnameRecord`, {
// recordName: props.config.stage,
// zone,
// domainName: props.config.domainBase,
// deleteExisting: true,
// ttl: Duration.minutes(5),
// });
// cname.applyRemovalPolicy(RemovalPolicy.DESTROY);

// const ns = new NsRecord(this, 'NSRecord', {
// zone,
// recordName: props.config.domainBase,
// values: [
// // Get these from the AWS > Route53 > Registered domains > <domain_name> > Name servers
// 'ns-1214.awsdns-23.org.',
// 'ns-191.awsdns-23.com.',
// 'ns-1640.awsdns-13.co.uk.',
// 'ns-790.awsdns-34.net.',
// ],
// deleteExisting: true,
// ttl: Duration.minutes(5),
// });
// ns.applyRemovalPolicy(RemovalPolicy.DESTROY);

const certificate = new Certificate(this, `${id}Certificate`, {
domainName: props.config.domainStage,
Expand Down

0 comments on commit 161688c

Please sign in to comment.