@fy-stack/cdn-construct 0.0.127 → 0.0.128

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -8,5 +8,6 @@ import { CDNConstructProps } from './types';
8
8
  export declare class CDNConstruct extends Construct {
9
9
  distribution: cloudfront.Distribution;
10
10
  constructor(scope: Construct, id: string, props: CDNConstructProps);
11
+ private parseDomain;
11
12
  }
12
13
  //# sourceMappingURL=cdn-construct.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"cdn-construct.d.ts","sourceRoot":"","sources":["../../src/lib/cdn-construct.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,4BAA4B,CAAC;AACzD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAE5C;;;GAGG;AACH,qBAAa,YAAa,SAAQ,SAAS;IAClC,YAAY,EAAE,UAAU,CAAC,YAAY,CAAC;gBAEjC,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB;CAkCnE"}
1
+ {"version":3,"file":"cdn-construct.d.ts","sourceRoot":"","sources":["../../src/lib/cdn-construct.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,4BAA4B,CAAC;AAEzD,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAC;AAI5C;;;GAGG;AACH,qBAAa,YAAa,SAAQ,SAAS;IAClC,YAAY,EAAE,UAAU,CAAC,YAAY,CAAC;gBAEjC,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,iBAAiB;IAwHlE,OAAO,CAAC,WAAW;CAIpB"}
@@ -3,7 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.CDNConstruct = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const cloudfront = tslib_1.__importStar(require("aws-cdk-lib/aws-cloudfront"));
6
+ const acm = tslib_1.__importStar(require("aws-cdk-lib/aws-certificatemanager"));
6
7
  const constructs_1 = require("constructs");
8
+ const route53 = tslib_1.__importStar(require("aws-cdk-lib/aws-route53"));
9
+ const route53Targets = tslib_1.__importStar(require("aws-cdk-lib/aws-route53-targets"));
7
10
  /**
8
11
  * CDNConstruct is a custom construct that sets up a CloudFront distribution
9
12
  * based on provided routing and resource configurations.
@@ -28,11 +31,66 @@ class CDNConstruct extends constructs_1.Construct {
28
31
  for (const i in otherRoutes) {
29
32
  Object.assign(additionalBehaviors, otherRoutes[i]?.cloudfront(i));
30
33
  }
34
+ let certificate;
35
+ let defaultDomainName;
36
+ const subjectAlternativeNames = [];
37
+ const zones = {};
38
+ if (props.domains) {
39
+ const [defaultDomain, ...otherRecords] = props.domains;
40
+ if (defaultDomain) {
41
+ const { records: [defaultDomainRecord, ...otherDefaultDomainRecords], } = defaultDomain;
42
+ defaultDomainName = this.parseDomain(defaultDomainRecord, defaultDomain.domain);
43
+ zones[defaultDomain.domain] = route53.HostedZone.fromLookup(this, 'Zone0', { domainName: defaultDomain.domain });
44
+ zones[defaultDomainName] = zones[defaultDomain.domain];
45
+ for (const i in otherDefaultDomainRecords) {
46
+ const recordDomain = this.parseDomain(otherDefaultDomainRecords[i], defaultDomain.domain);
47
+ zones[recordDomain] = zones[defaultDomain.domain];
48
+ subjectAlternativeNames.push(recordDomain);
49
+ }
50
+ for (const i in otherRecords) {
51
+ zones[otherRecords[i].domain] = route53.HostedZone.fromLookup(this, `Zone${i + 1}`, { domainName: otherRecords[i].domain });
52
+ for (const j in otherRecords[i].records) {
53
+ const recordDomain = this.parseDomain(otherRecords[i].records[j], otherRecords[i].domain);
54
+ zones[recordDomain] = zones[otherRecords[i].domain];
55
+ subjectAlternativeNames.push(recordDomain);
56
+ }
57
+ }
58
+ certificate = new acm.Certificate(this, 'DomainCertificate', {
59
+ domainName: defaultDomainName,
60
+ subjectAlternativeNames,
61
+ validation: acm.CertificateValidation.fromDnsMultiZone(zones),
62
+ });
63
+ }
64
+ }
31
65
  this.distribution = new cloudfront.Distribution(this, 'CDN', {
32
66
  defaultBehavior,
33
67
  additionalBehaviors,
68
+ certificate,
69
+ domainNames: defaultDomainName
70
+ ? [defaultDomainName, ...subjectAlternativeNames]
71
+ : undefined,
34
72
  priceClass: cloudfront.PriceClass.PRICE_CLASS_100,
35
73
  });
74
+ if (props.domains) {
75
+ const distributionTarget = new route53Targets.CloudFrontTarget(this.distribution);
76
+ for (const i in props.domains) {
77
+ for (const j in props.domains[i].records) {
78
+ new route53.ARecord(this, `Record${i}${j}`, {
79
+ target: { aliasTarget: distributionTarget },
80
+ zone: zones[props.domains[i].domain],
81
+ recordName: props.domains[i].records[j] === '*'
82
+ ? undefined
83
+ : props.domains[i].records[j],
84
+ });
85
+ }
86
+ }
87
+ }
88
+ }
89
+ parseDomain(record, domain) {
90
+ if (record === '*')
91
+ return domain;
92
+ else
93
+ return `${record}.${domain}`;
36
94
  }
37
95
  }
38
96
  exports.CDNConstruct = CDNConstruct;
@@ -8,5 +8,19 @@ export interface CDNConstructProps {
8
8
  * An optional mapping of resource names to CDN resources.
9
9
  * */
10
10
  resources?: Record<string, CDNResource | undefined>;
11
+ /**
12
+ * A list of domain name records
13
+ * */
14
+ domains?: {
15
+ /**
16
+ * Domain name
17
+ * */
18
+ domain: string;
19
+ /**
20
+ * Domain name records to map to distribution,
21
+ * add "*" for default
22
+ * */
23
+ records: string[];
24
+ }[];
11
25
  }
12
26
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEhE,MAAM,WAAW,iBAAiB;IAChC;;SAEK;IACL,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACpC;;SAEK;IACL,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC,CAAC;CACrD"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/lib/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEhE,MAAM,WAAW,iBAAiB;IAChC;;SAEK;IACL,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IACpC;;SAEK;IACL,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,GAAG,SAAS,CAAC,CAAC;IACpD;;SAEK;IACL,OAAO,CAAC,EAAE;QACR;;aAEK;QACL,MAAM,EAAE,MAAM,CAAC;QACf;;;aAGK;QACL,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB,EAAE,CAAC;CACL"}
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@fy-stack/cdn-construct",
3
- "version": "0.0.127",
3
+ "version": "0.0.128",
4
4
  "dependencies": {
5
5
  "tslib": "^2.3.0",
6
- "@fy-stack/types": "0.0.127"
6
+ "@fy-stack/types": "0.0.128"
7
7
  },
8
8
  "peerDependencies": {
9
9
  "aws-cdk-lib": "2.166.0",