-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdns.ts
More file actions
63 lines (54 loc) · 1.73 KB
/
Copy pathdns.ts
File metadata and controls
63 lines (54 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { Duration } from "aws-cdk-lib";
import { Certificate, ICertificate } from "aws-cdk-lib/aws-certificatemanager";
import { Distribution, IDistribution } from "aws-cdk-lib/aws-cloudfront";
import { CnameRecord, PublicHostedZone } from "aws-cdk-lib/aws-route53";
import { Construct } from "constructs";
export interface DnsProps {
env: string;
account: string;
}
export class Dns extends Construct {
certificate?: ICertificate;
hostedZone?: PublicHostedZone;
constructor(scope: Construct, id: string, props: DnsProps) {
super(scope, id);
const hostedZoneId = process.env.HOSTED_ZONE_ID!;
const certificateArn = process.env.CERTIFICATE_ARN!;
if (hostedZoneId && certificateArn) {
this.hostedZone = PublicHostedZone.fromHostedZoneAttributes(
this,
"ImportedHostedZone",
{
hostedZoneId: hostedZoneId,
zoneName: "stickerlandia.dev",
},
) as PublicHostedZone;
this.certificate = Certificate.fromCertificateArn(
this,
"ImportedCertificate",
certificateArn,
);
} else {
this.certificate = undefined;
this.hostedZone = undefined;
}
}
addCnameFor(distribution: Distribution, env: string) {
// Add a CName if the hosted zone exists.
if (this.hostedZone) {
const cNameRecord = new CnameRecord(this, "CnameRecord", {
zone: this.hostedZone!,
domainName: distribution.domainName,
recordName: this.getPrimaryDomainName(env)!,
ttl: Duration.minutes(5),
});
}
}
getPrimaryDomainName(env: string): string | undefined {
return this.certificate
? env === "prod"
? "app.stickerlandia.dev"
: `${env}.stickerlandia.dev`
: undefined;
}
}