@composurecdk/cloudfront 0.4.3 → 0.4.5

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.
Files changed (36) hide show
  1. package/README.md +53 -0
  2. package/dist/alarm-config.d.ts +56 -3
  3. package/dist/alarm-config.d.ts.map +1 -1
  4. package/dist/alarm-defaults.d.ts +14 -0
  5. package/dist/alarm-defaults.d.ts.map +1 -1
  6. package/dist/alarm-defaults.js +31 -0
  7. package/dist/alarm-defaults.js.map +1 -1
  8. package/dist/behavior-function-alarms.d.ts +44 -0
  9. package/dist/behavior-function-alarms.d.ts.map +1 -0
  10. package/dist/behavior-function-alarms.js +136 -0
  11. package/dist/behavior-function-alarms.js.map +1 -0
  12. package/dist/cloudfront-alarm-builder.d.ts +133 -0
  13. package/dist/cloudfront-alarm-builder.d.ts.map +1 -0
  14. package/dist/cloudfront-alarm-builder.js +131 -0
  15. package/dist/cloudfront-alarm-builder.js.map +1 -0
  16. package/dist/defaults.d.ts +6 -1
  17. package/dist/defaults.d.ts.map +1 -1
  18. package/dist/defaults.js +13 -1
  19. package/dist/defaults.js.map +1 -1
  20. package/dist/distribution-alarms.d.ts +0 -17
  21. package/dist/distribution-alarms.d.ts.map +1 -1
  22. package/dist/distribution-alarms.js +1 -24
  23. package/dist/distribution-alarms.js.map +1 -1
  24. package/dist/distribution-builder.d.ts +224 -62
  25. package/dist/distribution-builder.d.ts.map +1 -1
  26. package/dist/distribution-builder.js +61 -21
  27. package/dist/distribution-builder.js.map +1 -1
  28. package/dist/index.d.ts +5 -4
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +3 -2
  31. package/dist/index.js.map +1 -1
  32. package/dist/resolve-behaviors.d.ts +56 -0
  33. package/dist/resolve-behaviors.d.ts.map +1 -0
  34. package/dist/resolve-behaviors.js +98 -0
  35. package/dist/resolve-behaviors.js.map +1 -0
  36. package/package.json +1 -1
@@ -0,0 +1,131 @@
1
+ import { Annotations, Stack, Token } from "aws-cdk-lib";
2
+ import { Builder, resolve, } from "@composurecdk/core";
3
+ import { AlarmDefinitionBuilder, createAlarms } from "@composurecdk/cloudwatch";
4
+ import { resolveDistributionAlarmDefinitions } from "./distribution-alarms.js";
5
+ import { resolveBehaviorFunctionAlarmDefinitions } from "./behavior-function-alarms.js";
6
+ /**
7
+ * CloudFront metrics are emitted in `us-east-1` only. CloudWatch alarms are
8
+ * regional, so alarms created in any other region will never receive data.
9
+ * Warn (don't error) when alarms are being created outside `us-east-1`,
10
+ * unless the region is an unresolved token (env-agnostic stack — user knows
11
+ * best).
12
+ */
13
+ function warnIfNotUsEast1(scope) {
14
+ const region = Stack.of(scope).region;
15
+ if (Token.isUnresolved(region))
16
+ return;
17
+ if (region === "us-east-1")
18
+ return;
19
+ Annotations.of(scope).addWarningV2("@composurecdk/cloudfront:alarm-region", `CloudFront metrics are emitted in us-east-1 only, but this stack is deployed ` +
20
+ `in "${region}". CloudWatch alarms created here will not fire. Deploy the ` +
21
+ `stack in us-east-1, or use createCloudFrontAlarmBuilder() routed to a ` +
22
+ `us-east-1 stack via compose().withStacks().`);
23
+ }
24
+ /**
25
+ * Shared alarm-assembly used by both {@link createDistributionBuilder} (in its
26
+ * own stack) and {@link createCloudFrontAlarmBuilder} (typically in a separate
27
+ * `us-east-1` stack). Materializes the recommended distribution-level alarms,
28
+ * the recommended per-function alarms, and any user-supplied custom alarms,
29
+ * emits the region warning if the resulting scope is not in `us-east-1`, and
30
+ * creates the alarm constructs.
31
+ *
32
+ * @internal
33
+ */
34
+ export function buildCloudFrontAlarms(scope, id, target, options = {}) {
35
+ const recommended = options.recommendedAlarms;
36
+ const recommendedDefs = recommended === false || recommended?.enabled === false
37
+ ? []
38
+ : [
39
+ ...resolveDistributionAlarmDefinitions(target.distribution, recommended),
40
+ ...Object.values(target.functions).flatMap((entry) => resolveBehaviorFunctionAlarmDefinitions(entry.pathPattern, entry.eventType, entry.function, entry.recommendedAlarms)),
41
+ ];
42
+ const customAlarmDefs = options.customAlarms?.map((b) => b.resolve(target.distribution)) ?? [];
43
+ const allAlarmDefs = [...recommendedDefs, ...customAlarmDefs];
44
+ if (allAlarmDefs.length > 0) {
45
+ warnIfNotUsEast1(scope);
46
+ }
47
+ return createAlarms(scope, id, allAlarmDefs);
48
+ }
49
+ class CloudFrontAlarmBuilder {
50
+ props = {};
51
+ #distribution;
52
+ #customAlarms = [];
53
+ /**
54
+ * Sets the distribution to alarm on. Pass the result of
55
+ * {@link createDistributionBuilder} (or a {@link Ref} to it). The builder
56
+ * reads the distribution and any inline-function metadata from the result.
57
+ *
58
+ * Pair with `compose().withStacks()` to route this component into a
59
+ * `us-east-1` stack while the distribution itself lives elsewhere — set
60
+ * `crossRegionReferences: true` on both stacks so CDK can wire the
61
+ * `DistributionId` reference automatically.
62
+ */
63
+ distribution(distribution) {
64
+ this.#distribution = distribution;
65
+ return this;
66
+ }
67
+ /**
68
+ * Adds a custom alarm against the distribution. The configure callback
69
+ * receives a fresh {@link AlarmDefinitionBuilder} pre-set with the alarm's
70
+ * key; configure metric, threshold, comparison and any other options.
71
+ *
72
+ * The created alarm is materialized in this builder's stack — useful for
73
+ * cross-region setups where you want all CloudFront alarms to live with the
74
+ * recommended ones.
75
+ */
76
+ addAlarm(key, configure) {
77
+ this.#customAlarms.push(configure(new AlarmDefinitionBuilder(key)));
78
+ return this;
79
+ }
80
+ build(scope, id, context) {
81
+ if (!this.#distribution) {
82
+ throw new Error(`CloudFrontAlarmBuilder "${id}" requires a distribution. ` +
83
+ `Call .distribution() with a DistributionBuilderResult or a Ref to one.`);
84
+ }
85
+ const distribution = resolve(this.#distribution, context ?? {});
86
+ return {
87
+ alarms: buildCloudFrontAlarms(scope, id, distribution, {
88
+ recommendedAlarms: this.props.recommendedAlarms,
89
+ customAlarms: this.#customAlarms,
90
+ }),
91
+ };
92
+ }
93
+ }
94
+ /**
95
+ * Creates a new {@link ICloudFrontAlarmBuilder} for materializing CloudFront
96
+ * alarms in a stack separate from the distribution itself.
97
+ *
98
+ * The recommended use is multi-region deployments: the distribution lives in
99
+ * the site's stack (often outside `us-east-1` for latency or compliance
100
+ * reasons), and CloudFront alarms must live in a `us-east-1` stack so they
101
+ * can read the metrics CloudFront emits there.
102
+ *
103
+ * @example
104
+ * ```ts
105
+ * compose(
106
+ * {
107
+ * cdn: createDistributionBuilder()
108
+ * .origin(...)
109
+ * .defaultBehavior({ functions: [{ eventType, code }] })
110
+ * .recommendedAlarms(false), // suppress alarms in the dist's own stack
111
+ *
112
+ * cdnAlarms: createCloudFrontAlarmBuilder()
113
+ * .distribution(ref<DistributionBuilderResult>("cdn"))
114
+ * .recommendedAlarms({ errorRate: { threshold: 2 } }),
115
+ * },
116
+ * { cdn: [], cdnAlarms: ["cdn"] },
117
+ * )
118
+ * .withStacks({
119
+ * cdn: siteStack, // eu-west-2
120
+ * cdnAlarms: certStack, // us-east-1 (existing ACM stack)
121
+ * })
122
+ * .build(app, "App");
123
+ * ```
124
+ *
125
+ * Set `crossRegionReferences: true` on both stacks so CDK can export the
126
+ * `DistributionId` from the site stack and import it in the alarm stack.
127
+ */
128
+ export function createCloudFrontAlarmBuilder() {
129
+ return Builder(CloudFrontAlarmBuilder);
130
+ }
131
+ //# sourceMappingURL=cloudfront-alarm-builder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cloudfront-alarm-builder.js","sourceRoot":"","sources":["../src/cloudfront-alarm-builder.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAC;AAExD,OAAO,EACL,OAAO,EAGP,OAAO,GAER,MAAM,oBAAoB,CAAC;AAE5B,OAAO,EAAE,sBAAsB,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AAEhF,OAAO,EAAE,mCAAmC,EAAE,MAAM,0BAA0B,CAAC;AAC/E,OAAO,EAAE,uCAAuC,EAAE,MAAM,+BAA+B,CAAC;AAuDxF;;;;;;GAMG;AACH,SAAS,gBAAgB,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istributionBuilderProps, InlineFunctionDefinition } from "./distribution-builder.js";
2
2
  /**
3
3
  * Secure, AWS-recommended defaults applied to every CloudFront distribution
4
4
  * built with {@link createDistributionBuilder}. Each property can be individually
@@ -9,4 +9,9 @@ import type { DistributionBuilderProps } from "./distribution-builder.js";
9
9
  * then the resolved origin is injected.
10
10
  */
11
11
  export declare const DISTRIBUTION_DEFAULTS: Partial<DistributionBuilderProps>;
12
+ /**
13
+ * Defaults applied to every inline CloudFront Function declared on a cache
14
+ * behavior. User-provided properties take precedence.
15
+ */
16
+ export declare const INLINE_FUNCTION_DEFAULTS: Partial<InlineFunctionDefinition>;
12
17
  //# sourceMappingURL=defaults.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAE1E;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,EAAE,OAAO,CAAC,wBAAwB,CAkDnE,CAAC"}
1
+ {"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAQA,OAAO,KAAK,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,2BAA2B,CAAC;AAEpG;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,EAAE,OAAO,CAAC,wBAAwB,CAkDnE,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,wBAAwB,EAAE,OAAO,CAAC,wBAAwB,CAOtE,CAAC"}
package/dist/defaults.js CHANGED
@@ -1,4 +1,4 @@
1
- import { HttpVersion, PriceClass, ResponseHeadersPolicy, SecurityPolicyProtocol, ViewerProtocolPolicy, } from "aws-cdk-lib/aws-cloudfront";
1
+ import { FunctionRuntime, HttpVersion, PriceClass, ResponseHeadersPolicy, SecurityPolicyProtocol, ViewerProtocolPolicy, } from "aws-cdk-lib/aws-cloudfront";
2
2
  /**
3
3
  * Secure, AWS-recommended defaults applied to every CloudFront distribution
4
4
  * built with {@link createDistributionBuilder}. Each property can be individually
@@ -53,4 +53,16 @@ export const DISTRIBUTION_DEFAULTS = {
53
53
  responseHeadersPolicy: ResponseHeadersPolicy.SECURITY_HEADERS,
54
54
  },
55
55
  };
56
+ /**
57
+ * Defaults applied to every inline CloudFront Function declared on a cache
58
+ * behavior. User-provided properties take precedence.
59
+ */
60
+ export const INLINE_FUNCTION_DEFAULTS = {
61
+ /**
62
+ * Use the `cloudfront-js-2.0` runtime by default. It is a superset of
63
+ * `cloudfront-js-1.0` (async/await, `crypto.subtle`, KeyValueStore support).
64
+ * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-20.html
65
+ */
66
+ runtime: FunctionRuntime.JS_2_0,
67
+ };
56
68
  //# sourceMappingURL=defaults.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,WAAW,EACX,UAAU,EACV,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,4BAA4B,CAAC;AAGpC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAsC;IACtE;;;;;OAKG;IACH,aAAa,EAAE,IAAI;IAEnB;;;;OAIG;IACH,UAAU,EAAE,UAAU,CAAC,eAAe;IAEtC;;;;OAIG;IACH,WAAW,EAAE,WAAW,CAAC,WAAW;IAEpC;;;OAGG;IACH,iBAAiB,EAAE,YAAY;IAE/B;;;;OAIG;IACH,sBAAsB,EAAE,sBAAsB,CAAC,aAAa;IAE5D,eAAe,EAAE;QACf;;;WAGG;QACH,oBAAoB,EAAE,oBAAoB,CAAC,iBAAiB;QAE5D;;;;WAIG;QACH,qBAAqB,EAAE,qBAAqB,CAAC,gBAAgB;KAC9D;CACF,CAAC"}
1
+ {"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,WAAW,EACX,UAAU,EACV,qBAAqB,EACrB,sBAAsB,EACtB,oBAAoB,GACrB,MAAM,4BAA4B,CAAC;AAGpC;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAsC;IACtE;;;;;OAKG;IACH,aAAa,EAAE,IAAI;IAEnB;;;;OAIG;IACH,UAAU,EAAE,UAAU,CAAC,eAAe;IAEtC;;;;OAIG;IACH,WAAW,EAAE,WAAW,CAAC,WAAW;IAEpC;;;OAGG;IACH,iBAAiB,EAAE,YAAY;IAE/B;;;;OAIG;IACH,sBAAsB,EAAE,sBAAsB,CAAC,aAAa;IAE5D,eAAe,EAAE;QACf;;;WAGG;QACH,oBAAoB,EAAE,oBAAoB,CAAC,iBAAiB;QAE5D;;;;WAIG;QACH,qBAAqB,EAAE,qBAAqB,CAAC,gBAAgB;KAC9D;CACF,CAAC;AAEF;;;GAGG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAsC;IACzE;;;;OAIG;IACH,OAAO,EAAE,eAAe,CAAC,MAAM;CAChC,CAAC"}
@@ -1,26 +1,9 @@
1
- import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
2
1
  import type { Distribution } from "aws-cdk-lib/aws-cloudfront";
3
- import type { IConstruct } from "constructs";
4
2
  import type { AlarmDefinition } from "@composurecdk/cloudwatch";
5
- import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
6
3
  import type { DistributionAlarmConfig } from "./alarm-config.js";
7
4
  /**
8
5
  * Resolves the recommended alarm configuration into fully-resolved
9
6
  * {@link AlarmDefinition}s for a CloudFront distribution.
10
7
  */
11
8
  export declare function resolveDistributionAlarmDefinitions(distribution: Distribution, config: DistributionAlarmConfig | undefined): AlarmDefinition[];
12
- /**
13
- * Creates AWS-recommended CloudWatch alarms for a CloudFront distribution,
14
- * merging recommended definitions with any custom alarm builders.
15
- *
16
- * @param scope - CDK construct scope for creating alarm constructs.
17
- * @param id - Base identifier for alarm construct ids.
18
- * @param distribution - The CloudFront distribution to create alarms for.
19
- * @param config - User-provided alarm configuration, or `false` to disable all.
20
- * @param customAlarms - Custom alarm builders added via `addAlarm()`.
21
- * @returns A record mapping alarm keys to their created Alarm constructs.
22
- *
23
- * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#CloudFront
24
- */
25
- export declare function createDistributionAlarms(scope: IConstruct, id: string, distribution: Distribution, config: DistributionAlarmConfig | false | undefined, customAlarms?: AlarmDefinitionBuilder<Distribution>[]): Record<string, Alarm>;
26
9
  //# sourceMappingURL=distribution-alarms.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"distribution-alarms.d.ts","sourceRoot":"","sources":["../src/distribution-alarms.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAqC,MAAM,4BAA4B,CAAC;AAC3F,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAChE,OAAO,EAAE,sBAAsB,EAAoC,MAAM,0BAA0B,CAAC;AACpG,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AA2BjE;;;GAGG;AACH,wBAAgB,mCAAmC,CACjD,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,uBAAuB,GAAG,SAAS,GAC1C,eAAe,EAAE,CAqCnB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,wBAAwB,CACtC,KAAK,EAAE,UAAU,EACjB,EAAE,EAAE,MAAM,EACV,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,uBAAuB,GAAG,KAAK,GAAG,SAAS,EACnD,YAAY,GAAE,sBAAsB,CAAC,YAAY,CAAC,EAAO,GACxD,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAUvB"}
1
+ {"version":3,"file":"distribution-alarms.d.ts","sourceRoot":"","sources":["../src/distribution-alarms.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,4BAA4B,CAAC;AAC/D,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAEhE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AA2BjE;;;GAGG;AACH,wBAAgB,mCAAmC,CACjD,YAAY,EAAE,YAAY,EAC1B,MAAM,EAAE,uBAAuB,GAAG,SAAS,GAC1C,eAAe,EAAE,CAqCnB"}
@@ -1,6 +1,6 @@
1
1
  import { Duration } from "aws-cdk-lib";
2
2
  import { ComparisonOperator, Metric, Stats } from "aws-cdk-lib/aws-cloudwatch";
3
- import { createAlarms, resolveAlarmConfig } from "@composurecdk/cloudwatch";
3
+ import { resolveAlarmConfig } from "@composurecdk/cloudwatch";
4
4
  import { DISTRIBUTION_ALARM_DEFAULTS } from "./alarm-defaults.js";
5
5
  const METRIC_PERIOD = Duration.minutes(1);
6
6
  const METRIC_PERIOD_LABEL = `${String(METRIC_PERIOD.toMinutes())} minute`;
@@ -56,27 +56,4 @@ export function resolveDistributionAlarmDefinitions(distribution, config) {
56
56
  }
57
57
  return definitions;
58
58
  }
59
- /**
60
- * Creates AWS-recommended CloudWatch alarms for a CloudFront distribution,
61
- * merging recommended definitions with any custom alarm builders.
62
- *
63
- * @param scope - CDK construct scope for creating alarm constructs.
64
- * @param id - Base identifier for alarm construct ids.
65
- * @param distribution - The CloudFront distribution to create alarms for.
66
- * @param config - User-provided alarm configuration, or `false` to disable all.
67
- * @param customAlarms - Custom alarm builders added via `addAlarm()`.
68
- * @returns A record mapping alarm keys to their created Alarm constructs.
69
- *
70
- * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#CloudFront
71
- */
72
- export function createDistributionAlarms(scope, id, distribution, config, customAlarms = []) {
73
- if (config === false)
74
- return {};
75
- const enabled = config?.enabled ?? DISTRIBUTION_ALARM_DEFAULTS.enabled;
76
- if (!enabled)
77
- return {};
78
- const recommended = resolveDistributionAlarmDefinitions(distribution, config);
79
- const custom = customAlarms.map((b) => b.resolve(distribution));
80
- return createAlarms(scope, id, [...recommended, ...custom]);
81
- }
82
59
  //# sourceMappingURL=distribution-alarms.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"distribution-alarms.js","sourceRoot":"","sources":["../src/distribution-alarms.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAAc,kBAAkB,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AAI3F,OAAO,EAA0B,YAAY,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEpG,OAAO,EAAE,2BAA2B,EAAE,MAAM,qBAAqB,CAAC;AAElE,MAAM,aAAa,GAAG,QAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAC1C,MAAM,mBAAmB,GAAG,GAAG,MAAM,CAAC,aAAa,CAAC,SAAS,EAAE,CAAC,SAAS,CAAC;AAE1E;;;GAGG;AACH,SAAS,kBAAkB,CACzB,YAA0B,EAC1B,UAAkB,EAClB,SAAiB;IAEjB,OAAO,IAAI,MAAM,CAAC;QAChB,SAAS,EAAE,gBAAgB;QAC3B,UAAU;QACV,aAAa,EAAE;YACb,cAAc,EAAE,YAAY,CAAC,cAAc;YAC3C,MAAM,EAAE,QAAQ;SACjB;QACD,SAAS;QACT,MAAM,EAAE,aAAa;KACtB,CAAC,CAAC;AACL,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,mCAAmC,CACjD,YAA0B,EAC1B,MAA2C;IAE3C,IAAI,MAAM,EAAE,OAAO,KAAK,KAAK;QAAE,OAAO,EAAE,CAAC;IAEzC,MAAM,WAAW,GAAsB,EAAE,CAAC;IAE1C,IAAI,MAAM,EA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E,GAAG,CAAC,SAAS;YACxB,kBAAkB,EAAE,kBAAkB,CAAC,sBAAsB;YAC7D,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;YACxC,iBAAiB,EAAE,GAAG,CAAC,iBAAiB;YACxC,gBAAgB,EAAE,GAAG,CAAC,gBAAgB;YACtC,WAAW,EAAE,uDAAuD,MAAM,CAAC,GAAG,CAAC,SAAS,CAAC,aAAa,mBAAmB,GAAG;SAC7H,CAAC,CAAC;IACL,CAAC;IAED,OAAO,WAAW,CAAC;AACrB,CAAC"}
@@ -1,24 +1,133 @@
1
- import { Distribution, type DistributionProps, type IOrigin, type AddBehaviorOptions } from "aws-cdk-lib/aws-cloudfront";
1
+ import { Distribution, type DistributionProps, type IOrigin, type AddBehaviorOptions, type BehaviorOptions, type Function as CfFunction, type FunctionCode, type FunctionEventType, type FunctionRuntime, type IKeyValueStore } from "aws-cdk-lib/aws-cloudfront";
2
2
  import { type ICertificate } from "aws-cdk-lib/aws-certificatemanager";
3
3
  import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
4
4
  import { type Bucket } from "aws-cdk-lib/aws-s3";
5
5
  import { type IConstruct } from "constructs";
6
6
  import { type IBuilder, type Lifecycle, type Resolvable } from "@composurecdk/core";
7
7
  import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
8
- import type { DistributionAlarmConfig } from "./alarm-config.js";
8
+ import type { DistributionAlarmConfig, FunctionAlarmConfig } from "./alarm-config.js";
9
+ /**
10
+ * Per-function metadata exposed on {@link DistributionBuilderResult.functions}.
11
+ * Bundles the CDK construct with the behavior context the builder used to
12
+ * create it. Consumers (notably {@link createCloudFrontAlarmBuilder}) use the
13
+ * `pathPattern`, `eventType`, and `recommendedAlarms` fields to reproduce the
14
+ * same recommended alarms in a different stack.
15
+ */
16
+ export interface FunctionEntry {
17
+ /** The CloudFront Function created by the distribution builder. */
18
+ function: CfFunction;
19
+ /**
20
+ * The behavior the function is attached to. `null` for the default behavior;
21
+ * otherwise the path pattern (e.g. `"/api/*"`).
22
+ */
23
+ pathPattern: string | null;
24
+ /** The viewer event the function handles. */
25
+ eventType: FunctionEventType;
26
+ /**
27
+ * The {@link InlineFunctionDefinition.recommendedAlarms} value the user
28
+ * supplied for this function. Omitted entirely when the user did not
29
+ * provide one — consumers should treat that as "use defaults".
30
+ */
31
+ recommendedAlarms?: FunctionAlarmConfig | false;
32
+ }
33
+ /**
34
+ * A CloudFront Function declared inline on a cache behavior. The distribution
35
+ * builder creates the underlying {@link CfFunction} construct and wires it
36
+ * into the behavior's function associations. Recommended alarms for the
37
+ * function are emitted automatically, scoped to the behavior's path pattern.
38
+ *
39
+ * Only inline declarations are supported — there is no "bring your own
40
+ * function" escape hatch, because the whole point of owning the function is
41
+ * to emit path-scoped `FunctionExecutionErrors` / `FunctionValidationErrors`
42
+ * / `FunctionThrottles` alarms that couldn't be emitted from an external
43
+ * `IFunctionRef` (which exposes only an ARN, not a function name).
44
+ *
45
+ * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/cloudfront-functions.html
46
+ */
47
+ export interface InlineFunctionDefinition {
48
+ /** The viewer event that should invoke the function. */
49
+ eventType: FunctionEventType;
50
+ /** The source for the function — `FunctionCode.fromInline()` or `.fromFile()`. */
51
+ code: FunctionCode;
52
+ /**
53
+ * Explicit physical name for the function. Useful when operators search
54
+ * CloudWatch logs or metrics by function name rather than by ARN. If omitted,
55
+ * CDK generates a name derived from the construct path.
56
+ */
57
+ functionName?: string;
58
+ /**
59
+ * JavaScript runtime.
60
+ * @default FunctionRuntime.JS_2_0
61
+ */
62
+ runtime?: FunctionRuntime;
63
+ /** Optional comment stored on the function resource. */
64
+ comment?: string;
65
+ /**
66
+ * Key-value store to associate with the function. Only supported on the
67
+ * `cloudfront-js-2.0` runtime.
68
+ */
69
+ keyValueStore?: IKeyValueStore;
70
+ /**
71
+ * Per-function alarm configuration. Defaults to the three AWS-recommended
72
+ * alarms (execution errors, validation errors, throttles) each with
73
+ * threshold 0. Set to `false` to disable all alarms for this function, or
74
+ * override individual alarms via their config entry.
75
+ *
76
+ * **Region requirement:** CloudFront metrics are emitted in `us-east-1`
77
+ * only. CloudWatch alarms are regional, so these alarms will only fire if
78
+ * the containing stack is deployed in `us-east-1`. The builder emits a
79
+ * synth-time warning if this isn't the case.
80
+ *
81
+ * @see https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/monitoring-functions.html
82
+ */
83
+ recommendedAlarms?: FunctionAlarmConfig | false;
84
+ }
85
+ /**
86
+ * Configuration for the distribution's default cache behavior.
87
+ *
88
+ * The origin is set separately via {@link IDistributionBuilder.origin}. The
89
+ * `functionAssociations` field from {@link AddBehaviorOptions} is replaced by
90
+ * {@link functions}, which takes {@link InlineFunctionDefinition}s — the
91
+ * builder owns the CloudFront Function constructs so it can emit alarms for
92
+ * them.
93
+ */
94
+ export interface DefaultBehaviorConfig extends Omit<AddBehaviorOptions, "functionAssociations"> {
95
+ /**
96
+ * CloudFront Functions to associate with the default behavior. At most one
97
+ * function per {@link FunctionEventType} is allowed.
98
+ */
99
+ functions?: InlineFunctionDefinition[];
100
+ }
101
+ /**
102
+ * Configuration for a path-pattern cache behavior attached to the distribution
103
+ * via {@link IDistributionBuilder.behavior}.
104
+ *
105
+ * Unlike {@link DefaultBehaviorConfig}, `origin` is required. It may be a
106
+ * concrete {@link IOrigin} or a {@link Resolvable} (typically a {@link ref})
107
+ * for cross-component wiring.
108
+ */
109
+ export interface AdditionalBehaviorConfig extends Omit<BehaviorOptions, "origin" | "functionAssociations"> {
110
+ /** The origin for this behavior, concrete or resolved at build time. */
111
+ origin: Resolvable<IOrigin>;
112
+ /**
113
+ * CloudFront Functions to associate with this behavior. At most one
114
+ * function per {@link FunctionEventType} is allowed.
115
+ */
116
+ functions?: InlineFunctionDefinition[];
117
+ }
9
118
  /**
10
119
  * Configuration properties for the CloudFront distribution builder.
11
120
  *
12
121
  * Extends the CDK {@link DistributionProps} with additional builder-specific
13
- * options. The `defaultBehavior` field is replaced with {@link AddBehaviorOptions}
14
- * (which excludes `origin`) because the origin is set separately via the
15
- * {@link IDistributionBuilder.origin | origin()} method, which supports
16
- * {@link Resolvable} for cross-component wiring.
122
+ * options. `defaultBehavior` accepts a {@link DefaultBehaviorConfig} with
123
+ * inline function definitions; additional behaviors are added via the
124
+ * {@link IDistributionBuilder.behavior} method rather than the raw
125
+ * `additionalBehaviors` record.
17
126
  *
18
127
  * The `enableLogging` CDK prop is replaced by {@link accessLogging}, which
19
128
  * auto-creates a logging bucket with secure defaults when enabled.
20
129
  */
21
- export interface DistributionBuilderProps extends Omit<DistributionProps, "defaultBehavior" | "enableLogging" | "certificate"> {
130
+ export interface DistributionBuilderProps extends Omit<DistributionProps, "defaultBehavior" | "additionalBehaviors" | "enableLogging" | "certificate"> {
22
131
  /**
23
132
  * Whether to automatically create an S3 bucket for CloudFront standard
24
133
  * access logging.
@@ -44,36 +153,63 @@ export interface DistributionBuilderProps extends Omit<DistributionProps, "defau
44
153
  */
45
154
  certificate?: Resolvable<ICertificate>;
46
155
  /**
47
- * Options for the default cache behavior, excluding `origin`.
48
- *
49
- * The origin is set via the {@link IDistributionBuilder.origin | origin()}
50
- * method and injected at build time. All other behavior options (cache
51
- * policy, function associations, viewer protocol policy, etc.) can be
52
- * configured here.
156
+ * Configuration for the default cache behavior. The origin is set via
157
+ * {@link IDistributionBuilder.origin}. Inline CloudFront Functions declared
158
+ * in `functions` are created by the builder and receive path-scoped alarms
159
+ * automatically.
53
160
  */
54
- defaultBehavior?: AddBehaviorOptions;
161
+ defaultBehavior?: DefaultBehaviorConfig;
55
162
  /**
56
- * Configuration for AWS-recommended CloudWatch alarms.
163
+ * Configuration for AWS-recommended CloudWatch alarms at the **distribution**
164
+ * level (5xx error rate, origin latency).
165
+ *
166
+ * Per-function alarm shapes (`FunctionExecutionErrors`,
167
+ * `FunctionValidationErrors`, `FunctionThrottles`) are configured per
168
+ * function via {@link InlineFunctionDefinition.recommendedAlarms}, because
169
+ * their correct disposition depends on the behavior the function is
170
+ * attached to. The kill switch below (`recommendedAlarms: false`) still
171
+ * applies to function alarms — see below.
172
+ *
173
+ * **Region requirement:** CloudFront metrics are emitted in `us-east-1`
174
+ * only. CloudWatch alarms are regional, so every alarm created by this
175
+ * builder will only fire if the containing stack is deployed in
176
+ * `us-east-1`. The builder emits a synth-time warning if this isn't the
177
+ * case. For multi-region deployments, set `recommendedAlarms: false` here
178
+ * and use {@link createCloudFrontAlarmBuilder} routed to a `us-east-1`
179
+ * stack via `compose().withStacks()`.
57
180
  *
58
- * By default, the builder creates recommended alarms for 5xx error rate
59
- * and origin latency. Individual alarms can be customized or disabled.
60
- * Set to `false` to disable all alarms.
181
+ * Set to `false` to disable all recommended alarms (both distribution-level
182
+ * and per-function). Custom alarms added via
183
+ * {@link IDistributionBuilder.addAlarm} are unaffected. To disable a single
184
+ * function's alarms, set `recommendedAlarms: false` on the corresponding
185
+ * {@link InlineFunctionDefinition}.
61
186
  *
62
- * No alarm actions are configured by default since notification
63
- * methods are user-specific. Access alarms from the build result
64
- * or use an `afterBuild` hook to apply actions.
187
+ * No alarm actions are configured by default since notification methods
188
+ * are user-specific. Access alarms from the build result or use an
189
+ * `afterBuild` hook to apply actions.
65
190
  *
66
- * Function-level alarms (FunctionValidationErrors, FunctionExecutionErrors,
67
- * FunctionThrottles) require per-function dimensions. Use
68
- * {@link IDistributionBuilder.addAlarm} to add them.
191
+ * @example Tighter dist-level threshold; function alarms unchanged.
192
+ * ```ts
193
+ * createDistributionBuilder()
194
+ * .origin(siteOrigin)
195
+ * .recommendedAlarms({ errorRate: { threshold: 2 } });
196
+ * ```
197
+ *
198
+ * @example Multi-region setup — suppress all alarms here, recreate them in
199
+ * a `us-east-1` stack via {@link createCloudFrontAlarmBuilder}.
200
+ * ```ts
201
+ * createDistributionBuilder()
202
+ * .origin(siteOrigin)
203
+ * .recommendedAlarms(false);
204
+ * ```
69
205
  *
70
206
  * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#CloudFront
71
207
  */
72
208
  recommendedAlarms?: DistributionAlarmConfig | false;
73
209
  }
74
210
  /**
75
- * The build output of a {@link IDistributionBuilder}. Contains the CDK constructs
76
- * created during {@link Lifecycle.build}, keyed by role.
211
+ * The build output of an {@link IDistributionBuilder}. Contains the CDK
212
+ * constructs created during {@link Lifecycle.build}, keyed by role.
77
213
  */
78
214
  export interface DistributionBuilderResult {
79
215
  /** The CloudFront distribution construct created by the builder. */
@@ -84,42 +220,58 @@ export interface DistributionBuilderResult {
84
220
  */
85
221
  accessLogsBucket?: Bucket;
86
222
  /**
87
- * CloudWatch alarms created for the distribution, keyed by alarm name.
88
- *
89
- * Includes both AWS-recommended alarms and any custom alarms added
90
- * via {@link IDistributionBuilder.addAlarm}. Access individual alarms
91
- * by key (e.g., `result.alarms.errorRate`).
92
- *
93
- * No alarm actions are configured — apply them via the result or an
94
- * `afterBuild` hook.
223
+ * CloudFront Functions created by the builder for inline function
224
+ * definitions, keyed by `<behaviorScope><EventType>` —
225
+ * e.g. `defaultBehaviorViewerRequest`, `behaviorApiStarViewerRequest`.
226
+ * Each entry bundles the {@link CfFunction} with the behavior context
227
+ * (`pathPattern`, `eventType`) and the per-function `recommendedAlarms`
228
+ * config the user supplied. Empty if no inline functions were declared.
229
+ */
230
+ functions: Record<string, FunctionEntry>;
231
+ /**
232
+ * CloudWatch alarms created for the distribution and its inline functions,
233
+ * keyed by alarm name. Distribution-level keys: `errorRate`, `originLatency`.
234
+ * Function-level keys are prefixed by behavior scope and event type —
235
+ * e.g. `defaultBehaviorViewerRequestExecutionErrors`.
95
236
  *
96
- * @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#CloudFront
237
+ * Includes both recommended alarms and custom alarms added via
238
+ * {@link IDistributionBuilder.addAlarm}. Empty when `recommendedAlarms` is
239
+ * `false` and no custom alarms were added. No alarm actions are configured.
97
240
  */
98
241
  alarms: Record<string, Alarm>;
99
242
  }
100
243
  /**
101
244
  * A fluent builder for configuring and creating a CloudFront distribution.
102
245
  *
103
- * Configuration properties from CDK {@link DistributionProps} are exposed as
104
- * overloaded getter/setter methods via the builder proxy. The origin is set
105
- * via the {@link origin} method, which accepts a concrete {@link IOrigin} or
106
- * a {@link Ref} for cross-component wiring.
246
+ * Properties from CDK {@link DistributionProps} are exposed as overloaded
247
+ * getter/setter methods. The origin is set via {@link origin}, which accepts
248
+ * a concrete {@link IOrigin} or a {@link Ref} for cross-component wiring.
249
+ * Additional cache behaviors are added via {@link behavior}, which takes a
250
+ * path pattern and a config including its own origin and inline functions.
107
251
  *
108
252
  * The builder implements {@link Lifecycle}, so it can be used directly as a
109
- * component in a {@link compose | composed system}. When built, it creates
110
- * a CloudFront distribution with the configured properties and returns a
111
- * {@link DistributionBuilderResult}.
253
+ * component in a {@link compose | composed system}. When built, it creates a
254
+ * CloudFront distribution, any inline CloudFront Functions declared on its
255
+ * behaviors, and AWS-recommended alarms scoped per-behavior.
112
256
  *
113
257
  * @example
114
258
  * ```ts
115
259
  * const cdn = createDistributionBuilder()
116
- * .origin(ref("site", (r: BucketBuilderResult) =>
117
- * S3BucketOrigin.withOriginAccessControl(r.bucket)))
118
- * .errorResponses([{
119
- * httpStatus: 404,
120
- * responsePagePath: "/index.html",
121
- * responseHttpStatus: 200,
122
- * }]);
260
+ * .origin(siteOrigin)
261
+ * .defaultBehavior({
262
+ * functions: [{
263
+ * eventType: FunctionEventType.VIEWER_REQUEST,
264
+ * code: FunctionCode.fromFile({ filePath: "src/edge/rewrite.js" }),
265
+ * }],
266
+ * })
267
+ * .behavior("/api/*", {
268
+ * origin: apiOrigin,
269
+ * cachePolicy: CachePolicy.CACHING_DISABLED,
270
+ * functions: [{
271
+ * eventType: FunctionEventType.VIEWER_REQUEST,
272
+ * code: FunctionCode.fromFile({ filePath: "src/edge/api-auth.js" }),
273
+ * }],
274
+ * });
123
275
  * ```
124
276
  */
125
277
  export type IDistributionBuilder = IBuilder<DistributionBuilderProps, DistributionBuilder>;
@@ -137,6 +289,15 @@ declare class DistributionBuilder implements Lifecycle<DistributionBuilderResult
137
289
  * @returns This builder for chaining.
138
290
  */
139
291
  origin(origin: Resolvable<IOrigin>): this;
292
+ /**
293
+ * Adds an additional cache behavior for a path pattern. The behavior's
294
+ * origin is required (concrete or Resolvable). Any inline functions are
295
+ * created by the builder and receive per-behavior alarms scoped to the
296
+ * path pattern.
297
+ *
298
+ * @throws If a behavior for the same path pattern has already been added.
299
+ */
300
+ behavior(pathPattern: string, config: AdditionalBehaviorConfig): this;
140
301
  build(scope: IConstruct, id: string, context?: Record<string, object>): DistributionBuilderResult;
141
302
  }
142
303
  /**
@@ -145,26 +306,27 @@ declare class DistributionBuilder implements Lifecycle<DistributionBuilderResult
145
306
  * This is the entry point for defining a CloudFront distribution component.
146
307
  * The returned builder exposes every {@link DistributionBuilderProps} property
147
308
  * as a fluent setter/getter, plus {@link IDistributionBuilder.origin | origin()}
148
- * for setting the default origin with Ref support. It implements {@link Lifecycle}
149
- * for use with {@link compose}.
309
+ * for setting the default origin with Ref support and
310
+ * {@link IDistributionBuilder.behavior | behavior()} for path-pattern behaviors.
311
+ * It implements {@link Lifecycle} for use with {@link compose}.
150
312
  *
151
313
  * @returns A fluent builder for a CloudFront distribution.
152
314
  *
153
315
  * @example
154
316
  * ```ts
155
317
  * const cdn = createDistributionBuilder()
156
- * .origin(ref("site", (r: BucketBuilderResult) =>
318
+ * .origin(ref<BucketBuilderResult>("site", (r) =>
157
319
  * S3BucketOrigin.withOriginAccessControl(r.bucket)))
158
- * .comment("Community website CDN");
159
- *
160
- * // Use standalone:
161
- * const result = cdn.build(stack, "CDN");
162
- *
163
- * // Or compose into a system:
164
- * const system = compose(
165
- * { site: createBucketBuilder(), cdn },
166
- * { site: [], cdn: ["site"] },
167
- * );
320
+ * .defaultBehavior({
321
+ * functions: [{
322
+ * eventType: FunctionEventType.VIEWER_REQUEST,
323
+ * code: FunctionCode.fromFile({ filePath: "src/edge/rewrite.js" }),
324
+ * }],
325
+ * })
326
+ * .behavior("/api/*", {
327
+ * origin: apiOrigin,
328
+ * cachePolicy: CachePolicy.CACHING_DISABLED,
329
+ * });
168
330
  * ```
169
331
  */
170
332
  export declare function createDistributionBuilder(): IDistributionBuilder;