@composurecdk/lambda 0.1.3 → 0.3.0
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.
- package/README.md +96 -1
- package/dist/alarm-config.d.ts +79 -0
- package/dist/alarm-config.d.ts.map +1 -0
- package/dist/alarm-config.js +2 -0
- package/dist/alarm-config.js.map +1 -0
- package/dist/alarm-defaults.d.ts +17 -0
- package/dist/alarm-defaults.d.ts.map +1 -0
- package/dist/alarm-defaults.js +44 -0
- package/dist/alarm-defaults.js.map +1 -0
- package/dist/function-alarms.d.ts +28 -0
- package/dist/function-alarms.d.ts.map +1 -0
- package/dist/function-alarms.js +117 -0
- package/dist/function-alarms.js.map +1 -0
- package/dist/function-builder.d.ts +47 -2
- package/dist/function-builder.d.ts.map +1 -1
- package/dist/function-builder.js +13 -6
- package/dist/function-builder.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
package/README.md
CHANGED
|
@@ -49,9 +49,104 @@ const handler = createFunctionBuilder()
|
|
|
49
49
|
.build(stack, "MyFunction");
|
|
50
50
|
```
|
|
51
51
|
|
|
52
|
+
## Recommended Alarms
|
|
53
|
+
|
|
54
|
+
The builder creates [AWS-recommended CloudWatch alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda) by default. No alarm actions are configured — access alarms from the build result to add SNS topics or other actions.
|
|
55
|
+
|
|
56
|
+
| Alarm | Metric | Default threshold | Created when |
|
|
57
|
+
| ---------------------- | --------------------------------- | --------------------------- | ------------------------------------- |
|
|
58
|
+
| `errors` | Errors (Sum, 1 min) | > 0 | Always |
|
|
59
|
+
| `throttles` | Throttles (Sum, 1 min) | > 0 | Always |
|
|
60
|
+
| `duration` | Duration (p99, 1 min) | > 90% of configured timeout | `timeout` is set |
|
|
61
|
+
| `concurrentExecutions` | ConcurrentExecutions (Max, 1 min) | >= 80% of reserved limit | `reservedConcurrentExecutions` is set |
|
|
62
|
+
|
|
63
|
+
The defaults are exported as `FUNCTION_ALARM_DEFAULTS` for visibility and testing:
|
|
64
|
+
|
|
65
|
+
```ts
|
|
66
|
+
import { FUNCTION_ALARM_DEFAULTS } from "@composurecdk/lambda";
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
The `duration` and `concurrentExecutions` alarms use percentage-based thresholds that automatically adjust when the base value changes. For example, if you change the function timeout from 30s to 60s, the duration alarm threshold adjusts from 27s to 54s without any configuration change.
|
|
70
|
+
|
|
71
|
+
### Customizing thresholds
|
|
72
|
+
|
|
73
|
+
Override individual alarm properties via `recommendedAlarms`. Unspecified fields keep their defaults.
|
|
74
|
+
|
|
75
|
+
Absolute-threshold alarms (`errors`, `throttles`) accept a `threshold` value:
|
|
76
|
+
|
|
77
|
+
```ts
|
|
78
|
+
const handler = createFunctionBuilder()
|
|
79
|
+
.runtime(Runtime.NODEJS_22_X)
|
|
80
|
+
.handler("index.handler")
|
|
81
|
+
.code(Code.fromAsset("lambda"))
|
|
82
|
+
.recommendedAlarms({
|
|
83
|
+
errors: { threshold: 5, evaluationPeriods: 3, datapointsToAlarm: 2 },
|
|
84
|
+
});
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
Percentage-based alarms (`duration`, `concurrentExecutions`) accept a `thresholdPercent` between 0 and 1:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
builder.timeout(Duration.seconds(30)).recommendedAlarms({
|
|
91
|
+
duration: { thresholdPercent: 0.75 }, // 75% of timeout = 22.5s
|
|
92
|
+
});
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
For a fixed absolute threshold, disable the recommended alarm and add a custom one via `addAlarm`.
|
|
96
|
+
|
|
97
|
+
### Disabling alarms
|
|
98
|
+
|
|
99
|
+
Disable all recommended alarms:
|
|
100
|
+
|
|
101
|
+
```ts
|
|
102
|
+
builder.recommendedAlarms(false);
|
|
103
|
+
// or
|
|
104
|
+
builder.recommendedAlarms({ enabled: false });
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
Disable individual alarms:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
builder.recommendedAlarms({ errors: false, throttles: false });
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Custom alarms
|
|
114
|
+
|
|
115
|
+
Add custom alarms alongside the recommended ones via `addAlarm`. The callback receives an `AlarmDefinitionBuilder` typed to the Lambda function, so the metric factory has access to the function's built-in metric helpers.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
const handler = createFunctionBuilder()
|
|
119
|
+
.runtime(Runtime.NODEJS_22_X)
|
|
120
|
+
.handler("index.handler")
|
|
121
|
+
.code(Code.fromAsset("lambda"))
|
|
122
|
+
.timeout(Duration.seconds(30))
|
|
123
|
+
.addAlarm("highInvocations", (alarm) =>
|
|
124
|
+
alarm
|
|
125
|
+
.metric((fn) => fn.metricInvocations({ period: Duration.minutes(1) }))
|
|
126
|
+
.threshold(1000)
|
|
127
|
+
.greaterThanOrEqual()
|
|
128
|
+
.description("Invocation count is unusually high"),
|
|
129
|
+
);
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Custom alarm keys must not conflict with recommended alarm keys. To replace a recommended alarm, disable it first and add a custom one with the same key.
|
|
133
|
+
|
|
134
|
+
### Applying alarm actions
|
|
135
|
+
|
|
136
|
+
Alarms are returned in the build result as `Record<string, Alarm>`:
|
|
137
|
+
|
|
138
|
+
```ts
|
|
139
|
+
const result = handler.build(stack, "MyFunction");
|
|
140
|
+
|
|
141
|
+
const alertTopic = new Topic(stack, "AlertTopic");
|
|
142
|
+
for (const alarm of Object.values(result.alarms)) {
|
|
143
|
+
alarm.addAlarmAction(new SnsAction(alertTopic));
|
|
144
|
+
}
|
|
145
|
+
```
|
|
146
|
+
|
|
52
147
|
## Examples
|
|
53
148
|
|
|
54
149
|
- [LambdaApiStack](../examples/src/lambda-api-app.ts) — REST API backed by a Lambda function, wired with `ref`
|
|
55
|
-
- [DualFunctionStack](../examples/src/dual-function-app.ts) — Two Lambda functions with
|
|
150
|
+
- [DualFunctionStack](../examples/src/dual-function-app.ts) — Two Lambda functions with recommended alarms, custom alarms, and SNS alarm actions
|
|
56
151
|
- [MultiStackApp](../examples/src/multi-stack-app.ts) — Lambda split across stacks via `.withStacks()`
|
|
57
152
|
- [StrategyStackApp](../examples/src/strategy-stack-app.ts) — Lambda split across stacks via `.withStackStrategy()`
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import type { AlarmConfig } from "@composurecdk/cloudwatch";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for a percentage-based contextual alarm.
|
|
4
|
+
*
|
|
5
|
+
* Derived from {@link AlarmConfig} with `threshold` replaced by
|
|
6
|
+
* `thresholdPercent`. These alarms derive their threshold as a
|
|
7
|
+
* percentage of a function property (e.g., timeout or reserved
|
|
8
|
+
* concurrency). The threshold automatically adjusts when the base
|
|
9
|
+
* value changes, keeping the alarm true to its definition.
|
|
10
|
+
*
|
|
11
|
+
* For a fixed absolute threshold, disable the recommended alarm and
|
|
12
|
+
* add a custom one via {@link IFunctionBuilder.addAlarm}.
|
|
13
|
+
*/
|
|
14
|
+
export type PercentageAlarmConfig = Omit<AlarmConfig, "threshold"> & {
|
|
15
|
+
/**
|
|
16
|
+
* Threshold as a fraction of the base value (e.g., `0.9` = 90%).
|
|
17
|
+
* Must be between 0 and 1 (exclusive of 0).
|
|
18
|
+
*/
|
|
19
|
+
thresholdPercent?: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Controls which recommended alarms are created for a Lambda function.
|
|
23
|
+
* All alarms are enabled by default with AWS-recommended thresholds.
|
|
24
|
+
* Set individual alarms to `false` to disable them, or provide an
|
|
25
|
+
* {@link AlarmConfig} or {@link PercentageAlarmConfig} to tune thresholds.
|
|
26
|
+
*
|
|
27
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
28
|
+
*/
|
|
29
|
+
export interface FunctionAlarmConfig {
|
|
30
|
+
/**
|
|
31
|
+
* Master switch: set to `false` to disable all recommended alarms.
|
|
32
|
+
* Individual alarms can also be disabled via their own entry.
|
|
33
|
+
* @default true
|
|
34
|
+
*/
|
|
35
|
+
enabled?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* Alarm when the function produces invocation errors.
|
|
38
|
+
*
|
|
39
|
+
* Metric: `AWS/Lambda Errors`, statistic Sum, period 1 minute.
|
|
40
|
+
* Default threshold: > 0 errors.
|
|
41
|
+
*
|
|
42
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
43
|
+
*/
|
|
44
|
+
errors?: AlarmConfig | false;
|
|
45
|
+
/**
|
|
46
|
+
* Alarm when invocations are throttled.
|
|
47
|
+
*
|
|
48
|
+
* Metric: `AWS/Lambda Throttles`, statistic Sum, period 1 minute.
|
|
49
|
+
* Default threshold: > 0 throttles.
|
|
50
|
+
*
|
|
51
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
52
|
+
*/
|
|
53
|
+
throttles?: AlarmConfig | false;
|
|
54
|
+
/**
|
|
55
|
+
* Alarm when p99 duration approaches the configured timeout.
|
|
56
|
+
*
|
|
57
|
+
* Only created when the function has a `timeout` configured, since
|
|
58
|
+
* the threshold is derived as a percentage of the timeout value.
|
|
59
|
+
*
|
|
60
|
+
* Metric: `AWS/Lambda Duration`, statistic p99, period 1 minute.
|
|
61
|
+
* Default: 90% of the function timeout (`thresholdPercent: 0.9`).
|
|
62
|
+
*
|
|
63
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
64
|
+
*/
|
|
65
|
+
duration?: PercentageAlarmConfig | false;
|
|
66
|
+
/**
|
|
67
|
+
* Alarm when concurrent executions approach the reserved limit.
|
|
68
|
+
*
|
|
69
|
+
* Only created when `reservedConcurrentExecutions` is configured,
|
|
70
|
+
* since the threshold is derived as a percentage of the reserved limit.
|
|
71
|
+
*
|
|
72
|
+
* Metric: `AWS/Lambda ConcurrentExecutions`, statistic Maximum, period 1 minute.
|
|
73
|
+
* Default: 80% of reservedConcurrentExecutions (`thresholdPercent: 0.8`).
|
|
74
|
+
*
|
|
75
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
76
|
+
*/
|
|
77
|
+
concurrentExecutions?: PercentageAlarmConfig | false;
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=alarm-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alarm-config.d.ts","sourceRoot":"","sources":["../src/alarm-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;;;;;;;;;;GAWG;AACH,MAAM,MAAM,qBAAqB,GAAG,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,GAAG;IACnE;;;OAGG;IACH,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,WAAW,mBAAmB;IAClC;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAE7B;;;;;;;OAOG;IACH,SAAS,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAEhC;;;;;;;;;;OAUG;IACH,QAAQ,CAAC,EAAE,qBAAqB,GAAG,KAAK,CAAC;IAEzC;;;;;;;;;;OAUG;IACH,oBAAoB,CAAC,EAAE,qBAAqB,GAAG,KAAK,CAAC;CACtD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alarm-config.js","sourceRoot":"","sources":["../src/alarm-config.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { AlarmConfig } from "@composurecdk/cloudwatch";
|
|
2
|
+
import type { PercentageAlarmConfig } from "./alarm-config.js";
|
|
3
|
+
interface FunctionAlarmDefaults {
|
|
4
|
+
enabled: true;
|
|
5
|
+
errors: Required<AlarmConfig>;
|
|
6
|
+
throttles: Required<AlarmConfig>;
|
|
7
|
+
duration: Required<PercentageAlarmConfig>;
|
|
8
|
+
concurrentExecutions: Required<PercentageAlarmConfig>;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* AWS-recommended default alarm configuration for Lambda functions.
|
|
12
|
+
*
|
|
13
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
14
|
+
*/
|
|
15
|
+
export declare const FUNCTION_ALARM_DEFAULTS: FunctionAlarmDefaults;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=alarm-defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alarm-defaults.d.ts","sourceRoot":"","sources":["../src/alarm-defaults.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AAE/D,UAAU,qBAAqB;IAC7B,OAAO,EAAE,IAAI,CAAC;IACd,MAAM,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IAC9B,SAAS,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IACjC,QAAQ,EAAE,QAAQ,CAAC,qBAAqB,CAAC,CAAC;IAC1C,oBAAoB,EAAE,QAAQ,CAAC,qBAAqB,CAAC,CAAC;CACvD;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,qBAwCrC,CAAC"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
|
|
2
|
+
/**
|
|
3
|
+
* AWS-recommended default alarm configuration for Lambda functions.
|
|
4
|
+
*
|
|
5
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
6
|
+
*/
|
|
7
|
+
export const FUNCTION_ALARM_DEFAULTS = {
|
|
8
|
+
enabled: true,
|
|
9
|
+
/** Any error is worth investigating; threshold 0. */
|
|
10
|
+
errors: {
|
|
11
|
+
threshold: 0,
|
|
12
|
+
evaluationPeriods: 1,
|
|
13
|
+
datapointsToAlarm: 1,
|
|
14
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
15
|
+
},
|
|
16
|
+
/** Any throttle indicates capacity pressure; threshold 0. */
|
|
17
|
+
throttles: {
|
|
18
|
+
threshold: 0,
|
|
19
|
+
evaluationPeriods: 1,
|
|
20
|
+
datapointsToAlarm: 1,
|
|
21
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
22
|
+
},
|
|
23
|
+
/**
|
|
24
|
+
* p99 duration approaching timeout indicates risk of timeouts.
|
|
25
|
+
* 90% of the configured timeout — early warning before hard failures.
|
|
26
|
+
*/
|
|
27
|
+
duration: {
|
|
28
|
+
thresholdPercent: 0.9,
|
|
29
|
+
evaluationPeriods: 3,
|
|
30
|
+
datapointsToAlarm: 3,
|
|
31
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
32
|
+
},
|
|
33
|
+
/**
|
|
34
|
+
* Concurrent executions approaching reserved concurrency indicates
|
|
35
|
+
* risk of throttling. 80% of the reserved limit — early warning.
|
|
36
|
+
*/
|
|
37
|
+
concurrentExecutions: {
|
|
38
|
+
thresholdPercent: 0.8,
|
|
39
|
+
evaluationPeriods: 3,
|
|
40
|
+
datapointsToAlarm: 3,
|
|
41
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
42
|
+
},
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=alarm-defaults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alarm-defaults.js","sourceRoot":"","sources":["../src/alarm-defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAY9D;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAA0B;IAC5D,OAAO,EAAE,IAAI;IAEb,qDAAqD;IACrD,MAAM,EAAE;QACN,SAAS,EAAE,CAAC;QACZ,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;IAED,6DAA6D;IAC7D,SAAS,EAAE;QACT,SAAS,EAAE,CAAC;QACZ,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;IAED;;;OAGG;IACH,QAAQ,EAAE;QACR,gBAAgB,EAAE,GAAG;QACrB,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;IAED;;;OAGG;IACH,oBAAoB,EAAE;QACpB,gBAAgB,EAAE,GAAG;QACrB,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;CACF,CAAC"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
|
|
2
|
+
import type { Function as LambdaFunction, FunctionProps } from "aws-cdk-lib/aws-lambda";
|
|
3
|
+
import type { IConstruct } from "constructs";
|
|
4
|
+
import type { AlarmDefinition } from "@composurecdk/cloudwatch";
|
|
5
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
6
|
+
import type { FunctionAlarmConfig } from "./alarm-config.js";
|
|
7
|
+
/**
|
|
8
|
+
* Resolves the recommended alarm configuration into fully-resolved
|
|
9
|
+
* {@link AlarmDefinition}s, applying contextual logic for timeout-based
|
|
10
|
+
* duration and reserved-concurrency-based concurrent execution alarms.
|
|
11
|
+
*/
|
|
12
|
+
export declare function resolveFunctionAlarmDefinitions(fn: LambdaFunction, config: FunctionAlarmConfig | undefined, props: Pick<FunctionProps, "timeout" | "reservedConcurrentExecutions">): AlarmDefinition[];
|
|
13
|
+
/**
|
|
14
|
+
* Creates AWS-recommended CloudWatch alarms for a Lambda function,
|
|
15
|
+
* merging recommended definitions with any custom alarm builders.
|
|
16
|
+
*
|
|
17
|
+
* @param scope - CDK construct scope for creating alarm constructs.
|
|
18
|
+
* @param id - Base identifier for alarm construct ids.
|
|
19
|
+
* @param fn - The Lambda function to create alarms for.
|
|
20
|
+
* @param config - User-provided alarm configuration, or `false` to disable all.
|
|
21
|
+
* @param props - The merged function props, used for contextual alarm thresholds.
|
|
22
|
+
* @param customAlarms - Custom alarm builders added via `addAlarm()`.
|
|
23
|
+
* @returns A record mapping alarm keys to their created Alarm constructs.
|
|
24
|
+
*
|
|
25
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
26
|
+
*/
|
|
27
|
+
export declare function createFunctionAlarms(scope: IConstruct, id: string, fn: LambdaFunction, config: FunctionAlarmConfig | false | undefined, props: Pick<FunctionProps, "timeout" | "reservedConcurrentExecutions">, customAlarms?: AlarmDefinitionBuilder<LambdaFunction>[]): Record<string, Alarm>;
|
|
28
|
+
//# sourceMappingURL=function-alarms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-alarms.d.ts","sourceRoot":"","sources":["../src/function-alarms.ts"],"names":[],"mappings":"AACA,OAAO,EACL,KAAK,KAAK,EAIX,MAAM,4BAA4B,CAAC;AACpC,OAAO,KAAK,EAAE,QAAQ,IAAI,cAAc,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACxF,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,mBAAmB,EAAyB,MAAM,mBAAmB,CAAC;AAMpF;;;;GAIG;AACH,wBAAgB,+BAA+B,CAC7C,EAAE,EAAE,cAAc,EAClB,MAAM,EAAE,mBAAmB,GAAG,SAAS,EACvC,KAAK,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,GAAG,8BAA8B,CAAC,GACrE,eAAe,EAAE,CAwEnB;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,UAAU,EACjB,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,cAAc,EAClB,MAAM,EAAE,mBAAmB,GAAG,KAAK,GAAG,SAAS,EAC/C,KAAK,EAAE,IAAI,CAAC,aAAa,EAAE,SAAS,GAAG,8BAA8B,CAAC,EACtE,YAAY,GAAE,sBAAsB,CAAC,cAAc,CAAC,EAAO,GAC1D,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAUvB"}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
import { Duration } from "aws-cdk-lib";
|
|
2
|
+
import { ComparisonOperator, Stats, } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import { createAlarms, resolveAlarmConfig } from "@composurecdk/cloudwatch";
|
|
4
|
+
import { FUNCTION_ALARM_DEFAULTS } from "./alarm-defaults.js";
|
|
5
|
+
const METRIC_PERIOD = Duration.minutes(1);
|
|
6
|
+
const METRIC_PERIOD_LABEL = `${String(METRIC_PERIOD.toMinutes())} minute`;
|
|
7
|
+
/**
|
|
8
|
+
* Resolves the recommended alarm configuration into fully-resolved
|
|
9
|
+
* {@link AlarmDefinition}s, applying contextual logic for timeout-based
|
|
10
|
+
* duration and reserved-concurrency-based concurrent execution alarms.
|
|
11
|
+
*/
|
|
12
|
+
export function resolveFunctionAlarmDefinitions(fn, config, props) {
|
|
13
|
+
if (config?.enabled === false)
|
|
14
|
+
return [];
|
|
15
|
+
const definitions = [];
|
|
16
|
+
if (config?.errors !== false) {
|
|
17
|
+
const cfg = resolveAlarmConfig(config?.errors, FUNCTION_ALARM_DEFAULTS.errors);
|
|
18
|
+
definitions.push({
|
|
19
|
+
key: "errors",
|
|
20
|
+
metric: fn.metricErrors({ period: METRIC_PERIOD }),
|
|
21
|
+
threshold: cfg.threshold,
|
|
22
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
|
|
23
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
24
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
25
|
+
treatMissingData: cfg.treatMissingData,
|
|
26
|
+
description: `Lambda function is producing invocation errors. Threshold: > ${String(cfg.threshold)} errors in ${METRIC_PERIOD_LABEL}.`,
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
if (config?.throttles !== false) {
|
|
30
|
+
const cfg = resolveAlarmConfig(config?.throttles, FUNCTION_ALARM_DEFAULTS.throttles);
|
|
31
|
+
definitions.push({
|
|
32
|
+
key: "throttles",
|
|
33
|
+
metric: fn.metricThrottles({ period: METRIC_PERIOD }),
|
|
34
|
+
threshold: cfg.threshold,
|
|
35
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
|
|
36
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
37
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
38
|
+
treatMissingData: cfg.treatMissingData,
|
|
39
|
+
description: `Lambda function invocations are being throttled. Threshold: > ${String(cfg.threshold)} throttles in ${METRIC_PERIOD_LABEL}.`,
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const timeoutMs = props.timeout?.toMilliseconds();
|
|
43
|
+
if (config?.duration !== false && timeoutMs !== undefined) {
|
|
44
|
+
const cfg = resolvePercentageAlarmConfig(config?.duration, FUNCTION_ALARM_DEFAULTS.duration);
|
|
45
|
+
const threshold = Math.round(timeoutMs * cfg.thresholdPercent);
|
|
46
|
+
definitions.push({
|
|
47
|
+
key: "duration",
|
|
48
|
+
metric: fn.metricDuration({ period: METRIC_PERIOD, statistic: Stats.percentile(99) }),
|
|
49
|
+
threshold,
|
|
50
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
|
|
51
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
52
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
53
|
+
treatMissingData: cfg.treatMissingData,
|
|
54
|
+
description: `Lambda function p99 duration is approaching the configured timeout. Threshold: > ${String(threshold)}ms (${String(cfg.thresholdPercent * 100)}% of ${String(timeoutMs)}ms timeout).`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
const reservedConcurrency = props.reservedConcurrentExecutions;
|
|
58
|
+
if (config?.concurrentExecutions !== false && reservedConcurrency !== undefined) {
|
|
59
|
+
const cfg = resolvePercentageAlarmConfig(config?.concurrentExecutions, FUNCTION_ALARM_DEFAULTS.concurrentExecutions);
|
|
60
|
+
const threshold = Math.round(reservedConcurrency * cfg.thresholdPercent);
|
|
61
|
+
definitions.push({
|
|
62
|
+
key: "concurrentExecutions",
|
|
63
|
+
metric: fn.metric("ConcurrentExecutions", {
|
|
64
|
+
period: METRIC_PERIOD,
|
|
65
|
+
statistic: Stats.MAXIMUM,
|
|
66
|
+
}),
|
|
67
|
+
threshold,
|
|
68
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
69
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
70
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
71
|
+
treatMissingData: cfg.treatMissingData,
|
|
72
|
+
description: `Lambda function concurrent executions approaching reserved concurrency limit. Threshold: >= ${String(threshold)} (${String(cfg.thresholdPercent * 100)}% of ${String(reservedConcurrency)} reserved).`,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
return definitions;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Creates AWS-recommended CloudWatch alarms for a Lambda function,
|
|
79
|
+
* merging recommended definitions with any custom alarm builders.
|
|
80
|
+
*
|
|
81
|
+
* @param scope - CDK construct scope for creating alarm constructs.
|
|
82
|
+
* @param id - Base identifier for alarm construct ids.
|
|
83
|
+
* @param fn - The Lambda function to create alarms for.
|
|
84
|
+
* @param config - User-provided alarm configuration, or `false` to disable all.
|
|
85
|
+
* @param props - The merged function props, used for contextual alarm thresholds.
|
|
86
|
+
* @param customAlarms - Custom alarm builders added via `addAlarm()`.
|
|
87
|
+
* @returns A record mapping alarm keys to their created Alarm constructs.
|
|
88
|
+
*
|
|
89
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
90
|
+
*/
|
|
91
|
+
export function createFunctionAlarms(scope, id, fn, config, props, customAlarms = []) {
|
|
92
|
+
if (config === false)
|
|
93
|
+
return {};
|
|
94
|
+
const enabled = config?.enabled ?? FUNCTION_ALARM_DEFAULTS.enabled;
|
|
95
|
+
if (!enabled)
|
|
96
|
+
return {};
|
|
97
|
+
const recommended = resolveFunctionAlarmDefinitions(fn, config, props);
|
|
98
|
+
const custom = customAlarms.map((b) => b.resolve(fn));
|
|
99
|
+
return createAlarms(scope, id, [...recommended, ...custom]);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Resolves a percentage-based alarm config by layering user overrides
|
|
103
|
+
* onto the defaults.
|
|
104
|
+
*/
|
|
105
|
+
function resolvePercentageAlarmConfig(userConfig, defaults) {
|
|
106
|
+
const thresholdPercent = userConfig?.thresholdPercent ?? defaults.thresholdPercent;
|
|
107
|
+
if (thresholdPercent <= 0 || thresholdPercent > 1) {
|
|
108
|
+
throw new Error(`thresholdPercent must be between 0 (exclusive) and 1 (inclusive), got ${String(thresholdPercent)}.`);
|
|
109
|
+
}
|
|
110
|
+
return {
|
|
111
|
+
thresholdPercent,
|
|
112
|
+
evaluationPeriods: userConfig?.evaluationPeriods ?? defaults.evaluationPeriods,
|
|
113
|
+
datapointsToAlarm: userConfig?.datapointsToAlarm ?? defaults.datapointsToAlarm,
|
|
114
|
+
treatMissingData: userConfig?.treatMissingData ?? defaults.treatMissingData,
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=function-alarms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"function-alarms.js","sourceRoot":"","sources":["../src/function-alarms.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAC;AACvC,OAAO,EAEL,kBAAkB,EAClB,KAAK,GAEN,MAAM,4BAA4B,CAAC;AAIpC,OAAO,EAA0B,YAAY,EAAE,kBAAkB,EAAE,MAAM,0BAA0B,CAAC;AAEpG,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAE9D,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;;;;GAIG;AACH,MAAM,UAAU,+BAA+B,CAC7C,EAAkB,EAClB,MAAuC,EACvC,KAAsE;IAEtE,IAAI,MAAM,EAAE,OAAO,KAAK,KAAK;QAAE,OAAO,EAAE,CAAC;IAEzC,MAAM,WAAW,GAAsB,EAAE,CAAC;IAE1C,IAAI,MAAM,EAAE,MAAM,KAAK,KAAK,EAAE,CAAC;QAC7B,MAAM,GAAG,GAAG,kBAAk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,CAAC,IAAI,gBAAgB,GAAG,CAAC,EAAE,CAAC;QAClD,MAAM,IAAI,KAAK,CACb,yEAAyE,MAAM,CAAC,gBAAgB,CAAC,GAAG,CACrG,CAAC;IACJ,CAAC;IAED,OAAO;QACL,gBAAgB;QAChB,iBAAiB,EAAE,UAAU,EAAE,iBAAiB,IAAI,QAAQ,CAAC,iBAAiB;QAC9E,iBAAiB,EAAE,UAAU,EAAE,iBAAiB,IAAI,QAAQ,CAAC,iBAAiB;QAC9E,gBAAgB,EAAE,UAAU,EAAE,gBAAgB,IAAI,QAAQ,CAAC,gBAAgB;KAC5E,CAAC;AACJ,CAAC"}
|
|
@@ -1,8 +1,34 @@
|
|
|
1
|
+
import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
|
|
1
2
|
import { Function as LambdaFunction, type FunctionProps } from "aws-cdk-lib/aws-lambda";
|
|
2
3
|
import type { LogGroup } from "aws-cdk-lib/aws-logs";
|
|
3
4
|
import { type IConstruct } from "constructs";
|
|
4
5
|
import { type IBuilder, type Lifecycle } from "@composurecdk/core";
|
|
5
|
-
|
|
6
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
7
|
+
import type { FunctionAlarmConfig } from "./alarm-config.js";
|
|
8
|
+
/**
|
|
9
|
+
* Configuration properties for the Lambda function builder.
|
|
10
|
+
*
|
|
11
|
+
* Extends the CDK {@link FunctionProps} with additional builder-specific options.
|
|
12
|
+
*/
|
|
13
|
+
export interface FunctionBuilderProps extends FunctionProps {
|
|
14
|
+
/**
|
|
15
|
+
* Configuration for AWS-recommended CloudWatch alarms.
|
|
16
|
+
*
|
|
17
|
+
* By default, the builder creates recommended alarms with sensible
|
|
18
|
+
* thresholds for every applicable metric. Individual alarms can be
|
|
19
|
+
* customized or disabled. Set to `false` to disable all alarms.
|
|
20
|
+
*
|
|
21
|
+
* No alarm actions are configured by default since notification
|
|
22
|
+
* methods are user-specific. Access alarms from the build result
|
|
23
|
+
* or use an `afterBuild` hook to apply actions.
|
|
24
|
+
*
|
|
25
|
+
* Contextual alarms (duration, concurrentExecutions) are only created
|
|
26
|
+
* when the corresponding function configuration is present.
|
|
27
|
+
*
|
|
28
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
29
|
+
*/
|
|
30
|
+
recommendedAlarms?: FunctionAlarmConfig | false;
|
|
31
|
+
}
|
|
6
32
|
/**
|
|
7
33
|
* The build output of a {@link IFunctionBuilder}. Contains the CDK constructs
|
|
8
34
|
* created during {@link Lifecycle.build}, keyed by role.
|
|
@@ -24,6 +50,19 @@ export interface FunctionBuilderResult {
|
|
|
24
50
|
* @see https://docs.aws.amazon.com/lambda/latest/dg/monitoring-cloudwatchlogs-loggroups.html
|
|
25
51
|
*/
|
|
26
52
|
logGroup?: LogGroup;
|
|
53
|
+
/**
|
|
54
|
+
* CloudWatch alarms created for the function, keyed by alarm name.
|
|
55
|
+
*
|
|
56
|
+
* Includes both AWS-recommended alarms and any custom alarms added
|
|
57
|
+
* via {@link IFunctionBuilder.addAlarm}. Access individual alarms
|
|
58
|
+
* by key (e.g., `result.alarms.errors`).
|
|
59
|
+
*
|
|
60
|
+
* No alarm actions are configured — apply them via the result or an
|
|
61
|
+
* `afterBuild` hook.
|
|
62
|
+
*
|
|
63
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#Lambda
|
|
64
|
+
*/
|
|
65
|
+
alarms: Record<string, Alarm>;
|
|
27
66
|
}
|
|
28
67
|
/**
|
|
29
68
|
* A fluent builder for configuring and creating an AWS Lambda function.
|
|
@@ -43,6 +82,10 @@ export interface FunctionBuilderResult {
|
|
|
43
82
|
* to the function. This ensures full control over log lifecycle and follows
|
|
44
83
|
* AWS CDK guidance to create a LogGroup explicitly.
|
|
45
84
|
*
|
|
85
|
+
* The builder also creates AWS-recommended CloudWatch alarms by default.
|
|
86
|
+
* Alarms can be customized or disabled via the `recommendedAlarms` property.
|
|
87
|
+
* Custom alarms can be added via the {@link addAlarm} method.
|
|
88
|
+
*
|
|
46
89
|
* @see https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_lambda-readme.html
|
|
47
90
|
*
|
|
48
91
|
* @example
|
|
@@ -58,13 +101,15 @@ export interface FunctionBuilderResult {
|
|
|
58
101
|
export type IFunctionBuilder = IBuilder<FunctionBuilderProps, FunctionBuilder>;
|
|
59
102
|
declare class FunctionBuilder implements Lifecycle<FunctionBuilderResult> {
|
|
60
103
|
props: Partial<FunctionBuilderProps>;
|
|
104
|
+
private readonly customAlarms;
|
|
105
|
+
addAlarm(key: string, configure: (alarm: AlarmDefinitionBuilder<LambdaFunction>) => AlarmDefinitionBuilder<LambdaFunction>): this;
|
|
61
106
|
build(scope: IConstruct, id: string): FunctionBuilderResult;
|
|
62
107
|
}
|
|
63
108
|
/**
|
|
64
109
|
* Creates a new {@link IFunctionBuilder} for configuring an AWS Lambda function.
|
|
65
110
|
*
|
|
66
111
|
* This is the entry point for defining a Lambda function component. The returned
|
|
67
|
-
* builder exposes every {@link
|
|
112
|
+
* builder exposes every {@link FunctionBuilderProps} property as a fluent setter/getter
|
|
68
113
|
* and implements {@link Lifecycle} for use with {@link compose}.
|
|
69
114
|
*
|
|
70
115
|
* @returns A fluent builder for an AWS Lambda function.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"function-builder.d.ts","sourceRoot":"","sources":["../src/function-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAE,KAAK,aAAa,EAAE,MAAM,wBAAwB,CAAC;AACxF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAW,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;
|
|
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T,KAAK,EAAE,sBAAsB,CAAC,cAAc,CAAC,KAC1C,sBAAsB,CAAC,cAAc,CAAC,GAC1C,IAAI;IAKP,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,qBAAqB;CAuB5D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,qBAAqB,IAAI,gBAAgB,CAExD"}
|
package/dist/function-builder.js
CHANGED
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { Function as LambdaFunction } from "aws-cdk-lib/aws-lambda";
|
|
2
2
|
import { Builder } from "@composurecdk/core";
|
|
3
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
3
4
|
import { createLogGroupBuilder } from "@composurecdk/logs";
|
|
5
|
+
import { createFunctionAlarms } from "./function-alarms.js";
|
|
4
6
|
import { FUNCTION_DEFAULTS } from "./defaults.js";
|
|
5
7
|
class FunctionBuilder {
|
|
6
8
|
props = {};
|
|
9
|
+
customAlarms = [];
|
|
10
|
+
addAlarm(key, configure) {
|
|
11
|
+
this.customAlarms.push(configure(new AlarmDefinitionBuilder(key)));
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
7
14
|
build(scope, id) {
|
|
8
15
|
let logGroup;
|
|
9
16
|
let logGroupProps = {};
|
|
@@ -11,22 +18,22 @@ class FunctionBuilder {
|
|
|
11
18
|
logGroup = createLogGroupBuilder().build(scope, `${id}LogGroup`).logGroup;
|
|
12
19
|
logGroupProps = { logGroup };
|
|
13
20
|
}
|
|
21
|
+
const { recommendedAlarms: alarmConfig, ...functionProps } = this.props;
|
|
14
22
|
const mergedProps = {
|
|
15
23
|
...FUNCTION_DEFAULTS,
|
|
16
24
|
...logGroupProps,
|
|
17
|
-
...
|
|
18
|
-
};
|
|
19
|
-
return {
|
|
20
|
-
function: new LambdaFunction(scope, id, mergedProps),
|
|
21
|
-
logGroup,
|
|
25
|
+
...functionProps,
|
|
22
26
|
};
|
|
27
|
+
const fn = new LambdaFunction(scope, id, mergedProps);
|
|
28
|
+
const alarms = createFunctionAlarms(scope, id, fn, alarmConfig, mergedProps, this.customAlarms);
|
|
29
|
+
return { function: fn, logGroup, alarms };
|
|
23
30
|
}
|
|
24
31
|
}
|
|
25
32
|
/**
|
|
26
33
|
* Creates a new {@link IFunctionBuilder} for configuring an AWS Lambda function.
|
|
27
34
|
*
|
|
28
35
|
* This is the entry point for defining a Lambda function component. The returned
|
|
29
|
-
* builder exposes every {@link
|
|
36
|
+
* builder exposes every {@link FunctionBuilderProps} property as a fluent setter/getter
|
|
30
37
|
* and implements {@link Lifecycle} for use with {@link compose}.
|
|
31
38
|
*
|
|
32
39
|
* @returns A fluent builder for an AWS Lambda function.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"function-builder.js","sourceRoot":"","sources":["../src/function-builder.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"function-builder.js","sourceRoot":"","sources":["../src/function-builder.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,IAAI,cAAc,EAAsB,MAAM,wBAAwB,CAAC;AAGxF,OAAO,EAAE,OAAO,EAAiC,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAE3D,OAAO,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAC5D,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAqGlD,MAAM,eAAe;IACnB,KAAK,GAAkC,EAAE,CAAC;IACzB,YAAY,GAA6C,EAAE,CAAC;IAE7E,QAAQ,CACN,GAAW,EACX,SAE2C;QAE3C,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,sBAAsB,CAAiB,GAAG,CAAC,CAAC,CAAC,CAAC;QACnF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,KAAiB,EAAE,EAAU;QACjC,IAAI,QAA8B,CAAC;QACnC,IAAI,aAAa,GAAG,EAAE,CAAC;QAEvB,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;YACzB,QAAQ,GAAG,qBAAqB,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,UAAU,CAAC,CAAC,QAAQ,CAAC;YAC1E,aAAa,GAAG,EAAE,QAAQ,EAAE,CAAC;QAC/B,CAAC;QAED,MAAM,EAAE,iBAAiB,EAAE,WAAW,EAAE,GAAG,aAAa,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAExE,MAAM,WAAW,GAAG;YAClB,GAAG,iBAAiB;YACpB,GAAG,aAAa;YAChB,GAAG,aAAa;SACO,CAAC;QAE1B,MAAM,EAAE,GAAG,IAAI,cAAc,CAAC,KAAK,EAAE,EAAE,EAAE,WAAW,CAAC,CAAC;QAEtD,MAAM,MAAM,GAAG,oBAAoB,CAAC,KAAK,EAAE,EAAE,EAAE,EAAE,EAAE,WAAW,EAAE,WAAW,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAEhG,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;IAC5C,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,MAAM,UAAU,qBAAqB;IACnC,OAAO,OAAO,CAAwC,eAAe,CAAC,CAAC;AACzE,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { createFunctionBuilder, type FunctionBuilderResult, type IFunctionBuilder, } from "./function-builder.js";
|
|
2
2
|
export { FUNCTION_DEFAULTS } from "./defaults.js";
|
|
3
|
+
export { type FunctionAlarmConfig, type PercentageAlarmConfig } from "./alarm-config.js";
|
|
4
|
+
export { FUNCTION_ALARM_DEFAULTS } from "./alarm-defaults.js";
|
|
3
5
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,EACrB,KAAK,qBAAqB,EAC1B,KAAK,gBAAgB,GACtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAClD,OAAO,EAAE,KAAK,mBAAmB,EAAE,KAAK,qBAAqB,EAAE,MAAM,mBAAmB,CAAC;AACzF,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAGtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,qBAAqB,GAGtB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAElD,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composurecdk/lambda",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "Composable Lambda function builder with well-architected defaults",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -35,8 +35,9 @@
|
|
|
35
35
|
},
|
|
36
36
|
"type": "module",
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@composurecdk/
|
|
39
|
-
"@composurecdk/
|
|
38
|
+
"@composurecdk/cloudwatch": "^0.3.0",
|
|
39
|
+
"@composurecdk/core": "^0.3.0",
|
|
40
|
+
"@composurecdk/logs": "^0.3.0",
|
|
40
41
|
"aws-cdk-lib": "^2.0.0",
|
|
41
42
|
"constructs": "^10.0.0"
|
|
42
43
|
},
|