@composurecdk/apigateway 0.1.3 → 0.3.2
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 +93 -0
- package/dist/alarm-config.d.ts +47 -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 +15 -0
- package/dist/alarm-defaults.d.ts.map +1 -0
- package/dist/alarm-defaults.js +43 -0
- package/dist/alarm-defaults.js.map +1 -0
- package/dist/builder-common.d.ts +68 -0
- package/dist/builder-common.d.ts.map +1 -0
- package/dist/builder-common.js +2 -0
- package/dist/builder-common.js.map +1 -0
- package/dist/defaults.d.ts +14 -0
- package/dist/defaults.d.ts.map +1 -1
- package/dist/defaults.js +37 -18
- package/dist/defaults.js.map +1 -1
- package/dist/deploy-options.d.ts +17 -0
- package/dist/deploy-options.d.ts.map +1 -0
- package/dist/deploy-options.js +30 -0
- package/dist/deploy-options.js.map +1 -0
- package/dist/index.d.ts +4 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/rest-api-alarms.d.ts +26 -0
- package/dist/rest-api-alarms.d.ts.map +1 -0
- package/dist/rest-api-alarms.js +95 -0
- package/dist/rest-api-alarms.js.map +1 -0
- package/dist/rest-api-builder.d.ts +7 -27
- package/dist/rest-api-builder.d.ts.map +1 -1
- package/dist/rest-api-builder.js +17 -24
- package/dist/rest-api-builder.js.map +1 -1
- package/dist/spec-rest-api-builder.d.ts +87 -0
- package/dist/spec-rest-api-builder.d.ts.map +1 -0
- package/dist/spec-rest-api-builder.js +66 -0
- package/dist/spec-rest-api-builder.js.map +1 -0
- package/package.json +8 -7
package/README.md
CHANGED
|
@@ -69,6 +69,99 @@ result.accessLogGroup; // LogGroup | undefined
|
|
|
69
69
|
|
|
70
70
|
To provide your own destination instead, set `deployOptions.accessLogDestination` — the auto-created log group is skipped. To disable access logging entirely, set `.accessLogging(false)`.
|
|
71
71
|
|
|
72
|
+
## Recommended Alarms
|
|
73
|
+
|
|
74
|
+
Both builders create [AWS-recommended CloudWatch alarms](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway) by default. No alarm actions are configured — access alarms from the build result to add SNS topics or other actions.
|
|
75
|
+
|
|
76
|
+
| Alarm | Metric | Default threshold | Created when |
|
|
77
|
+
| ------------- | ------------------------- | ----------------- | ------------ |
|
|
78
|
+
| `clientError` | 4XXError (Average, 1 min) | > 0.05 (5%) | Always |
|
|
79
|
+
| `serverError` | 5XXError (Average, 1 min) | > 0.05 (5%) | Always |
|
|
80
|
+
| `latency` | Latency (p90, 1 min) | >= 2500ms | Always |
|
|
81
|
+
|
|
82
|
+
Alarm metrics include both `ApiName` and `Stage` dimensions, targeting the deployment stage created by the builder.
|
|
83
|
+
|
|
84
|
+
The defaults are exported as `REST_API_ALARM_DEFAULTS` for visibility and testing:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
import { REST_API_ALARM_DEFAULTS } from "@composurecdk/apigateway";
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Customizing thresholds
|
|
91
|
+
|
|
92
|
+
Override individual alarm properties via `recommendedAlarms`. Unspecified fields keep their defaults.
|
|
93
|
+
|
|
94
|
+
```ts
|
|
95
|
+
const api = createRestApiBuilder()
|
|
96
|
+
.restApiName("My Service")
|
|
97
|
+
.addMethod("GET", integration, methodResponse)
|
|
98
|
+
.recommendedAlarms({
|
|
99
|
+
serverError: { threshold: 0.1 }, // 10% error rate
|
|
100
|
+
latency: { threshold: 1000 }, // 1 second p90
|
|
101
|
+
clientError: { evaluationPeriods: 3 }, // fewer evaluation periods
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
### Disabling alarms
|
|
106
|
+
|
|
107
|
+
Disable all recommended alarms:
|
|
108
|
+
|
|
109
|
+
```ts
|
|
110
|
+
builder.recommendedAlarms(false);
|
|
111
|
+
// or
|
|
112
|
+
builder.recommendedAlarms({ enabled: false });
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
Disable individual alarms:
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
builder.recommendedAlarms({ clientError: false, latency: false });
|
|
119
|
+
```
|
|
120
|
+
|
|
121
|
+
### Custom alarms
|
|
122
|
+
|
|
123
|
+
Add custom alarms alongside the recommended ones via `addAlarm`. The callback receives an `AlarmDefinitionBuilder` typed to `RestApiBase`, so the metric factory has access to the API's properties.
|
|
124
|
+
|
|
125
|
+
```ts
|
|
126
|
+
import { Metric } from "aws-cdk-lib/aws-cloudwatch";
|
|
127
|
+
|
|
128
|
+
const api = createRestApiBuilder()
|
|
129
|
+
.restApiName("My Service")
|
|
130
|
+
.addMethod("GET", integration, methodResponse)
|
|
131
|
+
.addAlarm("integrationLatency", (alarm) =>
|
|
132
|
+
alarm
|
|
133
|
+
.metric(
|
|
134
|
+
(api) =>
|
|
135
|
+
new Metric({
|
|
136
|
+
namespace: "AWS/ApiGateway",
|
|
137
|
+
metricName: "IntegrationLatency",
|
|
138
|
+
dimensionsMap: {
|
|
139
|
+
ApiName: api.restApiName,
|
|
140
|
+
Stage: api.deploymentStage.stageName,
|
|
141
|
+
},
|
|
142
|
+
statistic: "p90",
|
|
143
|
+
period: Duration.minutes(1),
|
|
144
|
+
}),
|
|
145
|
+
)
|
|
146
|
+
.threshold(2000)
|
|
147
|
+
.greaterThanOrEqual()
|
|
148
|
+
.description("Integration latency is elevated"),
|
|
149
|
+
);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Applying alarm actions
|
|
153
|
+
|
|
154
|
+
Alarms are returned in the build result as `Record<string, Alarm>`:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const result = api.build(stack, "MyApi");
|
|
158
|
+
|
|
159
|
+
const alertTopic = new Topic(stack, "AlertTopic");
|
|
160
|
+
for (const alarm of Object.values(result.alarms)) {
|
|
161
|
+
alarm.addAlarmAction(new SnsAction(alertTopic));
|
|
162
|
+
}
|
|
163
|
+
```
|
|
164
|
+
|
|
72
165
|
## Examples
|
|
73
166
|
|
|
74
167
|
- [LambdaApiStack](../examples/src/lambda-api-app.ts) — REST API backed by a Lambda function, wired with `ref`
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import type { AlarmConfig } from "@composurecdk/cloudwatch";
|
|
2
|
+
/**
|
|
3
|
+
* Controls which recommended alarms are created for an API Gateway REST API.
|
|
4
|
+
* All applicable alarms are enabled by default with AWS-recommended thresholds.
|
|
5
|
+
* Set individual alarms to `false` to disable them, or provide an
|
|
6
|
+
* {@link AlarmConfig} to tune thresholds.
|
|
7
|
+
*
|
|
8
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
9
|
+
*/
|
|
10
|
+
export interface RestApiAlarmConfig {
|
|
11
|
+
/**
|
|
12
|
+
* Master switch: set to `false` to disable all recommended alarms.
|
|
13
|
+
* Individual alarms can also be disabled via their own entry.
|
|
14
|
+
* @default true
|
|
15
|
+
*/
|
|
16
|
+
enabled?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* Alarm when the API returns an elevated rate of client-side errors
|
|
19
|
+
* (4XX HTTP status codes).
|
|
20
|
+
*
|
|
21
|
+
* Metric: `AWS/ApiGateway 4XXError`, statistic Average, period 1 minute.
|
|
22
|
+
* Default threshold: > 0.05 (5% of requests).
|
|
23
|
+
*
|
|
24
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
25
|
+
*/
|
|
26
|
+
clientError?: AlarmConfig | false;
|
|
27
|
+
/**
|
|
28
|
+
* Alarm when the API returns an elevated rate of server-side errors
|
|
29
|
+
* (5XX HTTP status codes).
|
|
30
|
+
*
|
|
31
|
+
* Metric: `AWS/ApiGateway 5XXError`, statistic Average, period 1 minute.
|
|
32
|
+
* Default threshold: > 0.05 (5% of requests).
|
|
33
|
+
*
|
|
34
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
35
|
+
*/
|
|
36
|
+
serverError?: AlarmConfig | false;
|
|
37
|
+
/**
|
|
38
|
+
* Alarm when API latency is elevated.
|
|
39
|
+
*
|
|
40
|
+
* Metric: `AWS/ApiGateway Latency`, statistic p90, period 1 minute.
|
|
41
|
+
* Default threshold: >= 2500ms.
|
|
42
|
+
*
|
|
43
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
44
|
+
*/
|
|
45
|
+
latency?: AlarmConfig | false;
|
|
46
|
+
}
|
|
47
|
+
//# 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;;;;;;;GAOG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;OAIG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;;;;;OAQG;IACH,WAAW,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAElC;;;;;;;;OAQG;IACH,WAAW,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAElC;;;;;;;OAOG;IACH,OAAO,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;CAC/B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"alarm-config.js","sourceRoot":"","sources":["../src/alarm-config.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { AlarmConfig } from "@composurecdk/cloudwatch";
|
|
2
|
+
interface RestApiAlarmDefaults {
|
|
3
|
+
enabled: true;
|
|
4
|
+
clientError: Required<AlarmConfig>;
|
|
5
|
+
serverError: Required<AlarmConfig>;
|
|
6
|
+
latency: Required<AlarmConfig>;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* AWS-recommended default alarm configuration for API Gateway REST APIs.
|
|
10
|
+
*
|
|
11
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
12
|
+
*/
|
|
13
|
+
export declare const REST_API_ALARM_DEFAULTS: RestApiAlarmDefaults;
|
|
14
|
+
export {};
|
|
15
|
+
//# 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;AAE5D,UAAU,oBAAoB;IAC5B,OAAO,EAAE,IAAI,CAAC;IACd,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IACnC,WAAW,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;IACnC,OAAO,EAAE,QAAQ,CAAC,WAAW,CAAC,CAAC;CAChC;AAED;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,oBAsCrC,CAAC"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { TreatMissingData } from "aws-cdk-lib/aws-cloudwatch";
|
|
2
|
+
/**
|
|
3
|
+
* AWS-recommended default alarm configuration for API Gateway REST APIs.
|
|
4
|
+
*
|
|
5
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
6
|
+
*/
|
|
7
|
+
export const REST_API_ALARM_DEFAULTS = {
|
|
8
|
+
enabled: true,
|
|
9
|
+
/**
|
|
10
|
+
* Elevated 4XX error rate indicates client-side issues such as
|
|
11
|
+
* authorization failures, invalid parameters, or throttling.
|
|
12
|
+
* Threshold 0.05 = 5% of requests.
|
|
13
|
+
*/
|
|
14
|
+
clientError: {
|
|
15
|
+
threshold: 0.05,
|
|
16
|
+
evaluationPeriods: 5,
|
|
17
|
+
datapointsToAlarm: 5,
|
|
18
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
19
|
+
},
|
|
20
|
+
/**
|
|
21
|
+
* Elevated 5XX error rate indicates server-side issues such as
|
|
22
|
+
* backend failures or integration errors.
|
|
23
|
+
* Threshold 0.05 = 5% of requests.
|
|
24
|
+
*/
|
|
25
|
+
serverError: {
|
|
26
|
+
threshold: 0.05,
|
|
27
|
+
evaluationPeriods: 3,
|
|
28
|
+
datapointsToAlarm: 3,
|
|
29
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
30
|
+
},
|
|
31
|
+
/**
|
|
32
|
+
* Elevated p90 latency indicates slow API responses that may
|
|
33
|
+
* impact user experience or trigger downstream timeouts.
|
|
34
|
+
* Default 2500ms threshold per AWS recommendation.
|
|
35
|
+
*/
|
|
36
|
+
latency: {
|
|
37
|
+
threshold: 2500,
|
|
38
|
+
evaluationPeriods: 5,
|
|
39
|
+
datapointsToAlarm: 5,
|
|
40
|
+
treatMissingData: TreatMissingData.NOT_BREACHING,
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
//# 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;AAU9D;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAyB;IAC3D,OAAO,EAAE,IAAI;IAEb;;;;OAIG;IACH,WAAW,EAAE;QACX,SAAS,EAAE,IAAI;QACf,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;IAED;;;;OAIG;IACH,WAAW,EAAE;QACX,SAAS,EAAE,IAAI;QACf,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;IAED;;;;OAIG;IACH,OAAO,EAAE;QACP,SAAS,EAAE,IAAI;QACf,iBAAiB,EAAE,CAAC;QACpB,iBAAiB,EAAE,CAAC;QACpB,gBAAgB,EAAE,gBAAgB,CAAC,aAAa;KACjD;CACF,CAAC"}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { Alarm } from "aws-cdk-lib/aws-cloudwatch";
|
|
2
|
+
import type { RestApiBase } from "aws-cdk-lib/aws-apigateway";
|
|
3
|
+
import type { LogGroup } from "aws-cdk-lib/aws-logs";
|
|
4
|
+
import type { RestApiAlarmConfig } from "./alarm-config.js";
|
|
5
|
+
/**
|
|
6
|
+
* Builder-specific properties shared by both {@link RestApiBuilderProps}
|
|
7
|
+
* and {@link SpecRestApiBuilderProps}.
|
|
8
|
+
*/
|
|
9
|
+
export interface RestApiBuilderPropsBase {
|
|
10
|
+
/**
|
|
11
|
+
* Whether to automatically create a CloudWatch log group for access logging.
|
|
12
|
+
*
|
|
13
|
+
* When `true`, the builder creates a log group using
|
|
14
|
+
* {@link createLogGroupBuilder} (with its secure defaults) and configures it
|
|
15
|
+
* as the stage's access log destination with JSON-formatted output. The
|
|
16
|
+
* created log group is returned in the build result as `accessLogGroup`.
|
|
17
|
+
*
|
|
18
|
+
* When `false`, no access log group is created. You can still provide your
|
|
19
|
+
* own destination via `deployOptions.accessLogDestination`.
|
|
20
|
+
*
|
|
21
|
+
* This setting is ignored when `deployOptions.accessLogDestination` is
|
|
22
|
+
* provided — the user-supplied destination takes precedence.
|
|
23
|
+
*/
|
|
24
|
+
accessLogging?: boolean;
|
|
25
|
+
/**
|
|
26
|
+
* Configuration for AWS-recommended CloudWatch alarms.
|
|
27
|
+
*
|
|
28
|
+
* By default, the builder creates recommended alarms for client error rate,
|
|
29
|
+
* server error rate, and latency. Individual alarms can be customized or
|
|
30
|
+
* disabled. Set to `false` to disable all alarms.
|
|
31
|
+
*
|
|
32
|
+
* No alarm actions are configured by default since notification
|
|
33
|
+
* methods are user-specific. Access alarms from the build result
|
|
34
|
+
* or use an `afterBuild` hook to apply actions.
|
|
35
|
+
*
|
|
36
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
37
|
+
*/
|
|
38
|
+
recommendedAlarms?: RestApiAlarmConfig | false;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Build result properties shared by both {@link RestApiBuilderResult}
|
|
42
|
+
* and {@link SpecRestApiBuilderResult}.
|
|
43
|
+
*
|
|
44
|
+
* @typeParam T - The concrete REST API type (`RestApi` or `SpecRestApi`).
|
|
45
|
+
*/
|
|
46
|
+
export interface RestApiBuilderResultBase<T extends RestApiBase> {
|
|
47
|
+
/** The REST API construct created by the builder. */
|
|
48
|
+
api: T;
|
|
49
|
+
/**
|
|
50
|
+
* The CloudWatch log group created for access logging, or `undefined` if
|
|
51
|
+
* access logging was disabled or the user provided their own destination.
|
|
52
|
+
*/
|
|
53
|
+
accessLogGroup?: LogGroup;
|
|
54
|
+
/**
|
|
55
|
+
* CloudWatch alarms created for the API, keyed by alarm name.
|
|
56
|
+
*
|
|
57
|
+
* Includes both AWS-recommended alarms and any custom alarms added
|
|
58
|
+
* via `addAlarm()`. Access individual alarms by key
|
|
59
|
+
* (e.g., `result.alarms.serverError`).
|
|
60
|
+
*
|
|
61
|
+
* No alarm actions are configured — apply them via the result or an
|
|
62
|
+
* `afterBuild` hook.
|
|
63
|
+
*
|
|
64
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
65
|
+
*/
|
|
66
|
+
alarms: Record<string, Alarm>;
|
|
67
|
+
}
|
|
68
|
+
//# sourceMappingURL=builder-common.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder-common.d.ts","sourceRoot":"","sources":["../src/builder-common.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC9D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAE5D;;;GAGG;AACH,MAAM,WAAW,uBAAuB;IACtC;;;;;;;;;;;;;OAaG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IAExB;;;;;;;;;;;;OAYG;IACH,iBAAiB,CAAC,EAAE,kBAAkB,GAAG,KAAK,CAAC;CAChD;AAED;;;;;GAKG;AACH,MAAM,WAAW,wBAAwB,CAAC,CAAC,SAAS,WAAW;IAC7D,qDAAqD;IACrD,GAAG,EAAE,CAAC,CAAC;IAEP;;;OAGG;IACH,cAAc,CAAC,EAAE,QAAQ,CAAC;IAE1B;;;;;;;;;;;OAWG;IACH,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;CAC/B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"builder-common.js","sourceRoot":"","sources":["../src/builder-common.ts"],"names":[],"mappings":""}
|
package/dist/defaults.d.ts
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
1
|
+
import { type StageOptions } from "aws-cdk-lib/aws-apigateway";
|
|
1
2
|
import type { RestApiBuilderProps } from "./rest-api-builder.js";
|
|
3
|
+
import type { SpecRestApiBuilderProps } from "./spec-rest-api-builder.js";
|
|
4
|
+
/**
|
|
5
|
+
* Secure, AWS-recommended deploy-stage defaults shared by all API Gateway
|
|
6
|
+
* builders. Each property can be individually overridden via the builder's
|
|
7
|
+
* fluent API.
|
|
8
|
+
*/
|
|
9
|
+
export declare const DEPLOY_OPTIONS_DEFAULTS: StageOptions;
|
|
2
10
|
/**
|
|
3
11
|
* Secure, AWS-recommended defaults applied to every REST API built with
|
|
4
12
|
* {@link createRestApiBuilder}. Each property can be individually overridden
|
|
5
13
|
* via the builder's fluent API.
|
|
6
14
|
*/
|
|
7
15
|
export declare const REST_API_DEFAULTS: Partial<RestApiBuilderProps>;
|
|
16
|
+
/**
|
|
17
|
+
* Secure, AWS-recommended defaults applied to every spec-driven REST API
|
|
18
|
+
* built with {@link createSpecRestApiBuilder}. Each property can be
|
|
19
|
+
* individually overridden via the builder's fluent API.
|
|
20
|
+
*/
|
|
21
|
+
export declare const SPEC_REST_API_DEFAULTS: Partial<SpecRestApiBuilderProps>;
|
|
8
22
|
//# sourceMappingURL=defaults.d.ts.map
|
package/dist/defaults.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"defaults.d.ts","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,KAAK,YAAY,EAAE,MAAM,4BAA4B,CAAC;AACnF,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,KAAK,EAAE,uBAAuB,EAAE,MAAM,4BAA4B,CAAC;AAE1E;;;;GAIG;AACH,eAAO,MAAM,uBAAuB,EAAE,YAmBrC,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,iBAAiB,EAAE,OAAO,CAAC,mBAAmB,CAU1D,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,sBAAsB,EAAE,OAAO,CAAC,uBAAuB,CAQnE,CAAC"}
|
package/dist/defaults.js
CHANGED
|
@@ -1,4 +1,27 @@
|
|
|
1
1
|
import { MethodLoggingLevel } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
/**
|
|
3
|
+
* Secure, AWS-recommended deploy-stage defaults shared by all API Gateway
|
|
4
|
+
* builders. Each property can be individually overridden via the builder's
|
|
5
|
+
* fluent API.
|
|
6
|
+
*/
|
|
7
|
+
export const DEPLOY_OPTIONS_DEFAULTS = {
|
|
8
|
+
/**
|
|
9
|
+
* Enable AWS X-Ray tracing on the API Gateway stage.
|
|
10
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-distributed-tracing.html
|
|
11
|
+
*/
|
|
12
|
+
tracingEnabled: true,
|
|
13
|
+
/**
|
|
14
|
+
* Enable CloudWatch execution logging for API Gateway methods.
|
|
15
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-logging.html
|
|
16
|
+
*/
|
|
17
|
+
loggingLevel: MethodLoggingLevel.INFO,
|
|
18
|
+
/**
|
|
19
|
+
* Disable full request/response body logging to prevent sensitive data
|
|
20
|
+
* from appearing in CloudWatch logs.
|
|
21
|
+
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html
|
|
22
|
+
*/
|
|
23
|
+
dataTraceEnabled: false,
|
|
24
|
+
};
|
|
2
25
|
/**
|
|
3
26
|
* Secure, AWS-recommended defaults applied to every REST API built with
|
|
4
27
|
* {@link createRestApiBuilder}. Each property can be individually overridden
|
|
@@ -12,23 +35,19 @@ export const REST_API_DEFAULTS = {
|
|
|
12
35
|
* @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-logging.html
|
|
13
36
|
*/
|
|
14
37
|
accessLogging: true,
|
|
15
|
-
deployOptions:
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
* @see https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html
|
|
30
|
-
*/
|
|
31
|
-
dataTraceEnabled: false,
|
|
32
|
-
},
|
|
38
|
+
deployOptions: DEPLOY_OPTIONS_DEFAULTS,
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Secure, AWS-recommended defaults applied to every spec-driven REST API
|
|
42
|
+
* built with {@link createSpecRestApiBuilder}. Each property can be
|
|
43
|
+
* individually overridden via the builder's fluent API.
|
|
44
|
+
*/
|
|
45
|
+
export const SPEC_REST_API_DEFAULTS = {
|
|
46
|
+
/**
|
|
47
|
+
* Automatically create an access log group with structured JSON output.
|
|
48
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/serverless-applications-lens/opex-logging.html
|
|
49
|
+
*/
|
|
50
|
+
accessLogging: true,
|
|
51
|
+
deployOptions: DEPLOY_OPTIONS_DEFAULTS,
|
|
33
52
|
};
|
|
34
53
|
//# sourceMappingURL=defaults.js.map
|
package/dist/defaults.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"defaults.js","sourceRoot":"","sources":["../src/defaults.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAqB,MAAM,4BAA4B,CAAC;AAInF;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAiB;IACnD;;;OAGG;IACH,cAAc,EAAE,IAAI;IAEpB;;;OAGG;IACH,YAAY,EAAE,kBAAkB,CAAC,IAAI;IAErC;;;;OAIG;IACH,gBAAgB,EAAE,KAAK;CACxB,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAiC;IAC7D;;;;;OAKG;IACH,aAAa,EAAE,IAAI;IAEnB,aAAa,EAAE,uBAAuB;CACvC,CAAC;AAEF;;;;GAIG;AACH,MAAM,CAAC,MAAM,sBAAsB,GAAqC;IACtE;;;OAGG;IACH,aAAa,EAAE,IAAI;IAEnB,aAAa,EAAE,uBAAuB;CACvC,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type StageOptions } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
import { type LogGroup } from "aws-cdk-lib/aws-logs";
|
|
3
|
+
import { type IConstruct } from "constructs";
|
|
4
|
+
interface AccessLoggingResult {
|
|
5
|
+
accessLogGroup?: LogGroup;
|
|
6
|
+
deployOptions: StageOptions;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Resolves access logging configuration and merges deploy options with
|
|
10
|
+
* the provided defaults. Shared by {@link RestApiBuilder} and
|
|
11
|
+
* {@link SpecRestApiBuilder}.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export declare function resolveDeployOptions(scope: IConstruct, id: string, accessLogging: boolean | undefined, defaults: StageOptions, userDeployOptions: StageOptions): AccessLoggingResult;
|
|
16
|
+
export {};
|
|
17
|
+
//# sourceMappingURL=deploy-options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deploy-options.d.ts","sourceRoot":"","sources":["../src/deploy-options.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,YAAY,EAClB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,KAAK,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACrD,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAG7C,UAAU,mBAAmB;IAC3B,cAAc,CAAC,EAAE,QAAQ,CAAC;IAC1B,aAAa,EAAE,YAAY,CAAC;CAC7B;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,KAAK,EAAE,UAAU,EACjB,EAAE,EAAE,MAAM,EACV,aAAa,EAAE,OAAO,GAAG,SAAS,EAClC,QAAQ,EAAE,YAAY,EACtB,iBAAiB,EAAE,YAAY,GAC9B,mBAAmB,CAsBrB"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { AccessLogFormat, LogGroupLogDestination, } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
import { createLogGroupBuilder } from "@composurecdk/logs";
|
|
3
|
+
/**
|
|
4
|
+
* Resolves access logging configuration and merges deploy options with
|
|
5
|
+
* the provided defaults. Shared by {@link RestApiBuilder} and
|
|
6
|
+
* {@link SpecRestApiBuilder}.
|
|
7
|
+
*
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export function resolveDeployOptions(scope, id, accessLogging, defaults, userDeployOptions) {
|
|
11
|
+
const autoAccessLog = (accessLogging ?? true) && !userDeployOptions.accessLogDestination;
|
|
12
|
+
let accessLogGroup;
|
|
13
|
+
let accessLogProps = {};
|
|
14
|
+
if (autoAccessLog) {
|
|
15
|
+
accessLogGroup = createLogGroupBuilder().build(scope, `${id}AccessLogs`).logGroup;
|
|
16
|
+
accessLogProps = {
|
|
17
|
+
accessLogDestination: new LogGroupLogDestination(accessLogGroup),
|
|
18
|
+
accessLogFormat: AccessLogFormat.jsonWithStandardFields(),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
return {
|
|
22
|
+
accessLogGroup,
|
|
23
|
+
deployOptions: {
|
|
24
|
+
...defaults,
|
|
25
|
+
...accessLogProps,
|
|
26
|
+
...userDeployOptions,
|
|
27
|
+
},
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=deploy-options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deploy-options.js","sourceRoot":"","sources":["../src/deploy-options.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,eAAe,EACf,sBAAsB,GAEvB,MAAM,4BAA4B,CAAC;AAGpC,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAO3D;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAClC,KAAiB,EACjB,EAAU,EACV,aAAkC,EAClC,QAAsB,EACtB,iBAA+B;IAE/B,MAAM,aAAa,GAAG,CAAC,aAAa,IAAI,IAAI,CAAC,IAAI,CAAC,iBAAiB,CAAC,oBAAoB,CAAC;IAEzF,IAAI,cAAoC,CAAC;IACzC,IAAI,cAAc,GAAG,EAAE,CAAC;IAExB,IAAI,aAAa,EAAE,CAAC;QAClB,cAAc,GAAG,qBAAqB,EAAE,CAAC,KAAK,CAAC,KAAK,EAAE,GAAG,EAAE,YAAY,CAAC,CAAC,QAAQ,CAAC;QAClF,cAAc,GAAG;YACf,oBAAoB,EAAE,IAAI,sBAAsB,CAAC,cAAc,CAAC;YAChE,eAAe,EAAE,eAAe,CAAC,sBAAsB,EAAE;SAC1D,CAAC;IACJ,CAAC;IAED,OAAO;QACL,cAAc;QACd,aAAa,EAAE;YACb,GAAG,QAAQ;YACX,GAAG,cAAc;YACjB,GAAG,iBAAiB;SACrB;KACF,CAAC;AACJ,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
1
|
export { createRestApiBuilder, type RestApiBuilderResult, type IRestApiBuilder, } from "./rest-api-builder.js";
|
|
2
|
-
export {
|
|
2
|
+
export { createSpecRestApiBuilder, type SpecRestApiBuilderResult, type ISpecRestApiBuilder, } from "./spec-rest-api-builder.js";
|
|
3
|
+
export { REST_API_DEFAULTS, SPEC_REST_API_DEFAULTS } from "./defaults.js";
|
|
4
|
+
export { type RestApiAlarmConfig } from "./alarm-config.js";
|
|
5
|
+
export { REST_API_ALARM_DEFAULTS } from "./alarm-defaults.js";
|
|
3
6
|
//# 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,oBAAoB,EACpB,KAAK,oBAAoB,EACzB,KAAK,eAAe,GACrB,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,oBAAoB,EACpB,KAAK,oBAAoB,EACzB,KAAK,eAAe,GACrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,wBAAwB,EACxB,KAAK,wBAAwB,EAC7B,KAAK,mBAAmB,GACzB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAC1E,OAAO,EAAE,KAAK,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export { createRestApiBuilder, } from "./rest-api-builder.js";
|
|
2
|
-
export {
|
|
2
|
+
export { createSpecRestApiBuilder, } from "./spec-rest-api-builder.js";
|
|
3
|
+
export { REST_API_DEFAULTS, SPEC_REST_API_DEFAULTS } from "./defaults.js";
|
|
4
|
+
export { REST_API_ALARM_DEFAULTS } from "./alarm-defaults.js";
|
|
3
5
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,oBAAoB,GAGrB,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,oBAAoB,GAGrB,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EACL,wBAAwB,GAGzB,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AAE1E,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Alarm } from "aws-cdk-lib/aws-cloudwatch";
|
|
2
|
+
import type { RestApiBase } from "aws-cdk-lib/aws-apigateway";
|
|
3
|
+
import type { IConstruct } from "constructs";
|
|
4
|
+
import type { AlarmDefinition } from "@composurecdk/cloudwatch";
|
|
5
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
6
|
+
import type { RestApiAlarmConfig } from "./alarm-config.js";
|
|
7
|
+
/**
|
|
8
|
+
* Resolves the recommended alarm configuration into fully-resolved
|
|
9
|
+
* {@link AlarmDefinition}s for an API Gateway REST API.
|
|
10
|
+
*/
|
|
11
|
+
export declare function resolveRestApiAlarmDefinitions(api: RestApiBase, config: RestApiAlarmConfig | undefined): AlarmDefinition[];
|
|
12
|
+
/**
|
|
13
|
+
* Creates AWS-recommended CloudWatch alarms for an API Gateway REST API,
|
|
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 api - The REST API 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#ApiGateway
|
|
24
|
+
*/
|
|
25
|
+
export declare function createRestApiAlarms(scope: IConstruct, id: string, api: RestApiBase, config: RestApiAlarmConfig | false | undefined, customAlarms?: AlarmDefinitionBuilder<RestApiBase>[]): Record<string, Alarm>;
|
|
26
|
+
//# sourceMappingURL=rest-api-alarms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest-api-alarms.d.ts","sourceRoot":"","sources":["../src/rest-api-alarms.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,KAAK,EAAqC,MAAM,4BAA4B,CAAC;AAC3F,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAC;AAC9D,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,kBAAkB,EAAE,MAAM,mBAAmB,CAAC;AAuB5D;;;GAGG;AACH,wBAAgB,8BAA8B,CAC5C,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,kBAAkB,GAAG,SAAS,GACrC,eAAe,EAAE,CAgDnB;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,mBAAmB,CACjC,KAAK,EAAE,UAAU,EACjB,EAAE,EAAE,MAAM,EACV,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,kBAAkB,GAAG,KAAK,GAAG,SAAS,EAC9C,YAAY,GAAE,sBAAsB,CAAC,WAAW,CAAC,EAAO,GACvD,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAUvB"}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { Duration } from "aws-cdk-lib";
|
|
2
|
+
import { ComparisonOperator, Metric, Stats } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import { createAlarms, resolveAlarmConfig } from "@composurecdk/cloudwatch";
|
|
4
|
+
import { REST_API_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
|
+
* Creates an API Gateway metric with the correct namespace and
|
|
9
|
+
* dimensions (ApiName + Stage).
|
|
10
|
+
*/
|
|
11
|
+
function apiMetric(api, metricName, statistic) {
|
|
12
|
+
return new Metric({
|
|
13
|
+
namespace: "AWS/ApiGateway",
|
|
14
|
+
metricName,
|
|
15
|
+
dimensionsMap: {
|
|
16
|
+
ApiName: api.restApiName,
|
|
17
|
+
Stage: api.deploymentStage.stageName,
|
|
18
|
+
},
|
|
19
|
+
statistic,
|
|
20
|
+
period: METRIC_PERIOD,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Resolves the recommended alarm configuration into fully-resolved
|
|
25
|
+
* {@link AlarmDefinition}s for an API Gateway REST API.
|
|
26
|
+
*/
|
|
27
|
+
export function resolveRestApiAlarmDefinitions(api, config) {
|
|
28
|
+
if (config?.enabled === false)
|
|
29
|
+
return [];
|
|
30
|
+
const definitions = [];
|
|
31
|
+
if (config?.clientError !== false) {
|
|
32
|
+
const cfg = resolveAlarmConfig(config?.clientError, REST_API_ALARM_DEFAULTS.clientError);
|
|
33
|
+
definitions.push({
|
|
34
|
+
key: "clientError",
|
|
35
|
+
metric: apiMetric(api, "4XXError", Stats.AVERAGE),
|
|
36
|
+
threshold: cfg.threshold,
|
|
37
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
|
|
38
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
39
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
40
|
+
treatMissingData: cfg.treatMissingData,
|
|
41
|
+
description: `REST API client error rate is elevated. Threshold: > ${String(cfg.threshold * 100)}% of requests in ${METRIC_PERIOD_LABEL}.`,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (config?.serverError !== false) {
|
|
45
|
+
const cfg = resolveAlarmConfig(config?.serverError, REST_API_ALARM_DEFAULTS.serverError);
|
|
46
|
+
definitions.push({
|
|
47
|
+
key: "serverError",
|
|
48
|
+
metric: apiMetric(api, "5XXError", Stats.AVERAGE),
|
|
49
|
+
threshold: cfg.threshold,
|
|
50
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_THRESHOLD,
|
|
51
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
52
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
53
|
+
treatMissingData: cfg.treatMissingData,
|
|
54
|
+
description: `REST API server error rate is elevated. Threshold: > ${String(cfg.threshold * 100)}% of requests in ${METRIC_PERIOD_LABEL}.`,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (config?.latency !== false) {
|
|
58
|
+
const cfg = resolveAlarmConfig(config?.latency, REST_API_ALARM_DEFAULTS.latency);
|
|
59
|
+
definitions.push({
|
|
60
|
+
key: "latency",
|
|
61
|
+
metric: apiMetric(api, "Latency", Stats.percentile(90)),
|
|
62
|
+
threshold: cfg.threshold,
|
|
63
|
+
comparisonOperator: ComparisonOperator.GREATER_THAN_OR_EQUAL_TO_THRESHOLD,
|
|
64
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
65
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
66
|
+
treatMissingData: cfg.treatMissingData,
|
|
67
|
+
description: `REST API p90 latency is elevated. Threshold: >= ${String(cfg.threshold)}ms in ${METRIC_PERIOD_LABEL}.`,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
return definitions;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Creates AWS-recommended CloudWatch alarms for an API Gateway REST API,
|
|
74
|
+
* merging recommended definitions with any custom alarm builders.
|
|
75
|
+
*
|
|
76
|
+
* @param scope - CDK construct scope for creating alarm constructs.
|
|
77
|
+
* @param id - Base identifier for alarm construct ids.
|
|
78
|
+
* @param api - The REST API to create alarms for.
|
|
79
|
+
* @param config - User-provided alarm configuration, or `false` to disable all.
|
|
80
|
+
* @param customAlarms - Custom alarm builders added via `addAlarm()`.
|
|
81
|
+
* @returns A record mapping alarm keys to their created Alarm constructs.
|
|
82
|
+
*
|
|
83
|
+
* @see https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/Best_Practice_Recommended_Alarms_AWS_Services.html#ApiGateway
|
|
84
|
+
*/
|
|
85
|
+
export function createRestApiAlarms(scope, id, api, config, customAlarms = []) {
|
|
86
|
+
if (config === false)
|
|
87
|
+
return {};
|
|
88
|
+
const enabled = config?.enabled ?? REST_API_ALARM_DEFAULTS.enabled;
|
|
89
|
+
if (!enabled)
|
|
90
|
+
return {};
|
|
91
|
+
const recommended = resolveRestApiAlarmDefinitions(api, config);
|
|
92
|
+
const custom = customAlarms.map((b) => b.resolve(api));
|
|
93
|
+
return createAlarms(scope, id, [...recommended, ...custom]);
|
|
94
|
+
}
|
|
95
|
+
//# sourceMappingURL=rest-api-alarms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"rest-api-alarms.js","sourceRoot":"","sources":["../src/rest-api-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,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;;;GAGG;AACH,SAAS,SAAS,CAAC,GAAgB,EAAE,UAAkB,EAAE,SAAiB;IACxE,OAAO,IAAI,MAAM,CAAC;QAChB,SAAS,EAAE,gBAAgB;QAC3B,UAAU;QACV,aAAa,EAAE;YACb,OAAO,EAAE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AAE,CAAC;IAExB,MAAM,WAAW,GAAG,8BAA8B,CAAC,GAAG,EAAE,MAAM,CAAC,CAAC;IAChE,MAAM,MAAM,GAAG,YAAY,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;IAEvD,OAAO,YAAY,CAAC,KAAK,EAAE,EAAE,EAAE,CAAC,GAAG,WAAW,EAAE,GAAG,MAAM,CAAC,CAAC,CAAC;AAC9D,CAAC"}
|
|
@@ -1,43 +1,21 @@
|
|
|
1
|
-
import { type Integration, type MethodOptions, RestApi, type RestApiProps } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
-
import { type LogGroup } from "aws-cdk-lib/aws-logs";
|
|
1
|
+
import { type Integration, type MethodOptions, RestApi, type RestApiBase, type RestApiProps } from "aws-cdk-lib/aws-apigateway";
|
|
3
2
|
import { type IConstruct } from "constructs";
|
|
4
3
|
import { type IBuilder, type Lifecycle, type Resolvable } from "@composurecdk/core";
|
|
4
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
5
|
+
import type { RestApiBuilderPropsBase, RestApiBuilderResultBase } from "./builder-common.js";
|
|
5
6
|
import { ResourceBuilder } from "./resource-builder.js";
|
|
6
7
|
/**
|
|
7
8
|
* Configuration properties for the REST API builder.
|
|
8
9
|
*
|
|
9
10
|
* Extends the CDK {@link RestApiProps} with additional builder-specific options.
|
|
10
11
|
*/
|
|
11
|
-
export interface RestApiBuilderProps extends RestApiProps {
|
|
12
|
-
/**
|
|
13
|
-
* Whether to automatically create a CloudWatch log group for access logging.
|
|
14
|
-
*
|
|
15
|
-
* When `true`, the builder creates a log group using
|
|
16
|
-
* {@link createLogGroupBuilder} (with its secure defaults) and configures it
|
|
17
|
-
* as the stage's access log destination with JSON-formatted output. The
|
|
18
|
-
* created log group is returned in the build result as `accessLogGroup`.
|
|
19
|
-
*
|
|
20
|
-
* When `false`, no access log group is created. You can still provide your
|
|
21
|
-
* own destination via `deployOptions.accessLogDestination`.
|
|
22
|
-
*
|
|
23
|
-
* This setting is ignored when `deployOptions.accessLogDestination` is
|
|
24
|
-
* provided — the user-supplied destination takes precedence.
|
|
25
|
-
*/
|
|
26
|
-
accessLogging?: boolean;
|
|
12
|
+
export interface RestApiBuilderProps extends RestApiProps, RestApiBuilderPropsBase {
|
|
27
13
|
}
|
|
28
14
|
/**
|
|
29
15
|
* The build output of a {@link IRestApiBuilder}. Contains the CDK constructs
|
|
30
16
|
* created during {@link Lifecycle.build}, keyed by role.
|
|
31
17
|
*/
|
|
32
|
-
export
|
|
33
|
-
/** The REST API construct created by the builder. */
|
|
34
|
-
api: RestApi;
|
|
35
|
-
/**
|
|
36
|
-
* The CloudWatch log group created for access logging, or `undefined` if
|
|
37
|
-
* access logging was disabled or the user provided their own destination.
|
|
38
|
-
*/
|
|
39
|
-
accessLogGroup?: LogGroup;
|
|
40
|
-
}
|
|
18
|
+
export type RestApiBuilderResult = RestApiBuilderResultBase<RestApi>;
|
|
41
19
|
/**
|
|
42
20
|
* A fluent builder for configuring and creating an API Gateway REST API.
|
|
43
21
|
*
|
|
@@ -68,6 +46,8 @@ export type IRestApiBuilder = IBuilder<RestApiBuilderProps, RestApiBuilder>;
|
|
|
68
46
|
declare class RestApiBuilder implements Lifecycle<RestApiBuilderResult> {
|
|
69
47
|
props: Partial<RestApiBuilderProps>;
|
|
70
48
|
private readonly root;
|
|
49
|
+
private readonly customAlarms;
|
|
50
|
+
addAlarm(key: string, configure: (alarm: AlarmDefinitionBuilder<RestApiBase>) => AlarmDefinitionBuilder<RestApiBase>): this;
|
|
71
51
|
/**
|
|
72
52
|
* Adds an HTTP method to the API root resource (`/`).
|
|
73
53
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest-api-builder.d.ts","sourceRoot":"","sources":["../src/rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"rest-api-builder.d.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,aAAa,GACtB,IAAI;IAKP;;;;;;OAMG;IACH,WAAW,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,CAAC,QAAQ,EAAE,eAAe,KAAK,IAAI,GAAG,IAAI;IAKpF,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,oBAAoB;CAoB7F;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,wBAAgB,oBAAoB,IAAI,eAAe,CAEtD"}
|
package/dist/rest-api-builder.js
CHANGED
|
@@ -1,11 +1,18 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { RestApi, } from "aws-cdk-lib/aws-apigateway";
|
|
2
2
|
import { Builder } from "@composurecdk/core";
|
|
3
|
-
import {
|
|
4
|
-
import { ResourceBuilder } from "./resource-builder.js";
|
|
3
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
5
4
|
import { REST_API_DEFAULTS } from "./defaults.js";
|
|
5
|
+
import { resolveDeployOptions } from "./deploy-options.js";
|
|
6
|
+
import { ResourceBuilder } from "./resource-builder.js";
|
|
7
|
+
import { createRestApiAlarms } from "./rest-api-alarms.js";
|
|
6
8
|
class RestApiBuilder {
|
|
7
9
|
props = {};
|
|
8
10
|
root = new ResourceBuilder();
|
|
11
|
+
customAlarms = [];
|
|
12
|
+
addAlarm(key, configure) {
|
|
13
|
+
this.customAlarms.push(configure(new AlarmDefinitionBuilder(key)));
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
9
16
|
/**
|
|
10
17
|
* Adds an HTTP method to the API root resource (`/`).
|
|
11
18
|
*
|
|
@@ -31,29 +38,15 @@ class RestApiBuilder {
|
|
|
31
38
|
return this;
|
|
32
39
|
}
|
|
33
40
|
build(scope, id, context) {
|
|
34
|
-
const { accessLogging, ...restApiProps } = this.props;
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
let accessLogGroup;
|
|
38
|
-
let accessLogProps = {};
|
|
39
|
-
if (autoAccessLog) {
|
|
40
|
-
accessLogGroup = createLogGroupBuilder().build(scope, `${id}AccessLogs`).logGroup;
|
|
41
|
-
accessLogProps = {
|
|
42
|
-
accessLogDestination: new LogGroupLogDestination(accessLogGroup),
|
|
43
|
-
accessLogFormat: AccessLogFormat.jsonWithStandardFields(),
|
|
44
|
-
};
|
|
45
|
-
}
|
|
46
|
-
const mergedProps = {
|
|
41
|
+
const { accessLogging, recommendedAlarms: alarmConfig, ...restApiProps } = this.props;
|
|
42
|
+
const { accessLogGroup, deployOptions } = resolveDeployOptions(scope, id, accessLogging, REST_API_DEFAULTS.deployOptions ?? {}, restApiProps.deployOptions ?? {});
|
|
43
|
+
const api = new RestApi(scope, id, {
|
|
47
44
|
...restApiProps,
|
|
48
|
-
deployOptions
|
|
49
|
-
|
|
50
|
-
...accessLogProps,
|
|
51
|
-
...userDeployOptions,
|
|
52
|
-
},
|
|
53
|
-
};
|
|
54
|
-
const api = new RestApi(scope, id, mergedProps);
|
|
45
|
+
deployOptions,
|
|
46
|
+
});
|
|
55
47
|
this.root.applyTo(api.root, context ?? {});
|
|
56
|
-
|
|
48
|
+
const alarms = createRestApiAlarms(scope, id, api, alarmConfig, this.customAlarms);
|
|
49
|
+
return { api, accessLogGroup, alarms };
|
|
57
50
|
}
|
|
58
51
|
}
|
|
59
52
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rest-api-builder.js","sourceRoot":"","sources":["../src/rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"rest-api-builder.js","sourceRoot":"","sources":["../src/rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,OAAO,GAGR,MAAM,4BAA4B,CAAC;AAEpC,OAAO,EAAE,OAAO,EAAkD,MAAM,oBAAoB,CAAC;AAC7F,OAAO,EAAE,sBAAsB,EA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,GAAG,GAAG,IAAI,OAAO,CAAC,KAAK,EAAE,EAAE,EAAE;YACjC,GAAG,YAAY;YACf,aAAa;SACE,CAAC,CAAC;QACnB,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,IAAI,EAAE,CAAC,CAAC;QAE3C,MAAM,MAAM,GAAG,mBAAmB,CAAC,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAEnF,OAAO,EAAE,GAAG,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC;IACzC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,UAAU,oBAAoB;IAClC,OAAO,OAAO,CAAsC,cAAc,CAAC,CAAC;AACtE,CAAC"}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type RestApiBase, SpecRestApi, type SpecRestApiProps } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
import { type IConstruct } from "constructs";
|
|
3
|
+
import { type IBuilder, type Lifecycle } from "@composurecdk/core";
|
|
4
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
5
|
+
import type { RestApiBuilderPropsBase, RestApiBuilderResultBase } from "./builder-common.js";
|
|
6
|
+
/**
|
|
7
|
+
* Configuration properties for the spec-driven REST API builder.
|
|
8
|
+
*
|
|
9
|
+
* Extends the CDK {@link SpecRestApiProps} with additional builder-specific
|
|
10
|
+
* options.
|
|
11
|
+
*/
|
|
12
|
+
export interface SpecRestApiBuilderProps extends SpecRestApiProps, RestApiBuilderPropsBase {
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The build output of a {@link ISpecRestApiBuilder}. Contains the CDK
|
|
16
|
+
* constructs created during {@link Lifecycle.build}, keyed by role.
|
|
17
|
+
*/
|
|
18
|
+
export type SpecRestApiBuilderResult = RestApiBuilderResultBase<SpecRestApi>;
|
|
19
|
+
/**
|
|
20
|
+
* A fluent builder for configuring and creating an API Gateway REST API from
|
|
21
|
+
* an OpenAPI specification.
|
|
22
|
+
*
|
|
23
|
+
* Configuration properties from CDK {@link SpecRestApiProps} are exposed as
|
|
24
|
+
* overloaded getter/setter methods via the builder proxy. The API structure
|
|
25
|
+
* is defined entirely by the OpenAPI specification provided via
|
|
26
|
+
* {@link apiDefinition}.
|
|
27
|
+
*
|
|
28
|
+
* The builder implements {@link Lifecycle}, so it can be used directly as a
|
|
29
|
+
* component in a {@link compose | composed system}. When built, it creates
|
|
30
|
+
* a {@link SpecRestApi} with the configured properties and returns a
|
|
31
|
+
* {@link SpecRestApiBuilderResult}.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const api = createSpecRestApiBuilder()
|
|
36
|
+
* .restApiName("PetStore")
|
|
37
|
+
* .apiDefinition(ApiDefinition.fromAsset("openapi/petstore.yaml"));
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export type ISpecRestApiBuilder = IBuilder<SpecRestApiBuilderProps, SpecRestApiBuilder>;
|
|
41
|
+
declare class SpecRestApiBuilder implements Lifecycle<SpecRestApiBuilderResult> {
|
|
42
|
+
props: Partial<SpecRestApiBuilderProps>;
|
|
43
|
+
private readonly customAlarms;
|
|
44
|
+
addAlarm(key: string, configure: (alarm: AlarmDefinitionBuilder<RestApiBase>) => AlarmDefinitionBuilder<RestApiBase>): this;
|
|
45
|
+
build(scope: IConstruct, id: string): SpecRestApiBuilderResult;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Creates a new {@link ISpecRestApiBuilder} for configuring an API Gateway
|
|
49
|
+
* REST API from an OpenAPI specification.
|
|
50
|
+
*
|
|
51
|
+
* This is the entry point for defining a spec-driven REST API component. The
|
|
52
|
+
* returned builder exposes every {@link SpecRestApiProps} property as a fluent
|
|
53
|
+
* setter/getter. It implements {@link Lifecycle} for use with {@link compose}.
|
|
54
|
+
*
|
|
55
|
+
* The API structure — resources, methods, and integrations — is defined
|
|
56
|
+
* entirely by the OpenAPI specification passed to {@link apiDefinition}.
|
|
57
|
+
* Use CDK's {@link ApiDefinition} static methods to load the spec from an
|
|
58
|
+
* inline object, a local file, or an S3 bucket.
|
|
59
|
+
*
|
|
60
|
+
* @returns A fluent builder for a spec-driven API Gateway REST API.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* // From a local OpenAPI file
|
|
65
|
+
* const api = createSpecRestApiBuilder()
|
|
66
|
+
* .restApiName("PetStore")
|
|
67
|
+
* .apiDefinition(ApiDefinition.fromAsset("openapi/petstore.yaml"));
|
|
68
|
+
*
|
|
69
|
+
* // From an inline definition
|
|
70
|
+
* const api = createSpecRestApiBuilder()
|
|
71
|
+
* .restApiName("PetStore")
|
|
72
|
+
* .apiDefinition(ApiDefinition.fromInline({
|
|
73
|
+
* openapi: "3.0.2",
|
|
74
|
+
* info: { title: "PetStore", version: "1.0" },
|
|
75
|
+
* paths: { "/pets": { get: { ... } } },
|
|
76
|
+
* }));
|
|
77
|
+
*
|
|
78
|
+
* // Compose into a system
|
|
79
|
+
* const system = compose(
|
|
80
|
+
* { api },
|
|
81
|
+
* { api: [] },
|
|
82
|
+
* );
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function createSpecRestApiBuilder(): ISpecRestApiBuilder;
|
|
86
|
+
export {};
|
|
87
|
+
//# sourceMappingURL=spec-rest-api-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spec-rest-api-builder.d.ts","sourceRoot":"","sources":["../src/spec-rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,WAAW,EAAE,WAAW,EAAE,KAAK,gBAAgB,EAAE,MAAM,4BAA4B,CAAC;AAClG,OAAO,EAAE,KAAK,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAW,KAAK,QAAQ,EAAE,KAAK,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAClE,OAAO,KAAK,EAAE,uBAAuB,EAAE,wBAAwB,EAAE,MAAM,qBAAqB,CAAC;AAK7F;;;;;GAKG;AACH,MAAM,WAAW,uBAAwB,SAAQ,gBAAgB,EAAE,uBAAuB;CAAG;AAE7F;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG,wBAAwB,CAAC,WAAW,CAAC,CAAC;AAE7E;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,MAAM,mBAAmB,GAAG,QAAQ,CAAC,uBAAuB,EAAE,kBAAkB,CAAC,CAAC;AAExF,cAAM,kBAAmB,YAAW,SAAS,CAAC,wBAAwB,CAAC;IACrE,KAAK,EAAE,OAAO,CAAC,uBAAuB,CAAC,CAAM;IAC7C,OAAO,CAAC,QAAQ,CAAC,YAAY,CAA6C;IAE1E,QAAQ,CACN,GAAG,EAAE,MAAM,EACX,SAAS,EAAE,CAAC,KAAK,EAAE,sBAAsB,CAAC,WAAW,CAAC,KAAK,sBAAsB,CAAC,WAAW,CAAC,GAC7F,IAAI;IAKP,KAAK,CAAC,KAAK,EAAE,UAAU,EAAE,EAAE,EAAE,MAAM,GAAG,wBAAwB;CAmB/D;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,wBAAgB,wBAAwB,IAAI,mBAAmB,CAE9D"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { SpecRestApi } from "aws-cdk-lib/aws-apigateway";
|
|
2
|
+
import { Builder } from "@composurecdk/core";
|
|
3
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
4
|
+
import { SPEC_REST_API_DEFAULTS } from "./defaults.js";
|
|
5
|
+
import { resolveDeployOptions } from "./deploy-options.js";
|
|
6
|
+
import { createRestApiAlarms } from "./rest-api-alarms.js";
|
|
7
|
+
class SpecRestApiBuilder {
|
|
8
|
+
props = {};
|
|
9
|
+
customAlarms = [];
|
|
10
|
+
addAlarm(key, configure) {
|
|
11
|
+
this.customAlarms.push(configure(new AlarmDefinitionBuilder(key)));
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
build(scope, id) {
|
|
15
|
+
const { accessLogging, recommendedAlarms: alarmConfig, ...specRestApiProps } = this.props;
|
|
16
|
+
const { accessLogGroup, deployOptions } = resolveDeployOptions(scope, id, accessLogging, SPEC_REST_API_DEFAULTS.deployOptions ?? {}, specRestApiProps.deployOptions ?? {});
|
|
17
|
+
const api = new SpecRestApi(scope, id, {
|
|
18
|
+
...specRestApiProps,
|
|
19
|
+
deployOptions,
|
|
20
|
+
});
|
|
21
|
+
const alarms = createRestApiAlarms(scope, id, api, alarmConfig, this.customAlarms);
|
|
22
|
+
return { api, accessLogGroup, alarms };
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Creates a new {@link ISpecRestApiBuilder} for configuring an API Gateway
|
|
27
|
+
* REST API from an OpenAPI specification.
|
|
28
|
+
*
|
|
29
|
+
* This is the entry point for defining a spec-driven REST API component. The
|
|
30
|
+
* returned builder exposes every {@link SpecRestApiProps} property as a fluent
|
|
31
|
+
* setter/getter. It implements {@link Lifecycle} for use with {@link compose}.
|
|
32
|
+
*
|
|
33
|
+
* The API structure — resources, methods, and integrations — is defined
|
|
34
|
+
* entirely by the OpenAPI specification passed to {@link apiDefinition}.
|
|
35
|
+
* Use CDK's {@link ApiDefinition} static methods to load the spec from an
|
|
36
|
+
* inline object, a local file, or an S3 bucket.
|
|
37
|
+
*
|
|
38
|
+
* @returns A fluent builder for a spec-driven API Gateway REST API.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* // From a local OpenAPI file
|
|
43
|
+
* const api = createSpecRestApiBuilder()
|
|
44
|
+
* .restApiName("PetStore")
|
|
45
|
+
* .apiDefinition(ApiDefinition.fromAsset("openapi/petstore.yaml"));
|
|
46
|
+
*
|
|
47
|
+
* // From an inline definition
|
|
48
|
+
* const api = createSpecRestApiBuilder()
|
|
49
|
+
* .restApiName("PetStore")
|
|
50
|
+
* .apiDefinition(ApiDefinition.fromInline({
|
|
51
|
+
* openapi: "3.0.2",
|
|
52
|
+
* info: { title: "PetStore", version: "1.0" },
|
|
53
|
+
* paths: { "/pets": { get: { ... } } },
|
|
54
|
+
* }));
|
|
55
|
+
*
|
|
56
|
+
* // Compose into a system
|
|
57
|
+
* const system = compose(
|
|
58
|
+
* { api },
|
|
59
|
+
* { api: [] },
|
|
60
|
+
* );
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
63
|
+
export function createSpecRestApiBuilder() {
|
|
64
|
+
return Builder(SpecRestApiBuilder);
|
|
65
|
+
}
|
|
66
|
+
//# sourceMappingURL=spec-rest-api-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"spec-rest-api-builder.js","sourceRoot":"","sources":["../src/spec-rest-api-builder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoB,WAAW,EAAyB,MAAM,4BAA4B,CAAC;AAElG,OAAO,EAAE,OAAO,EAAiC,MAAM,oBAAoB,CAAC;AAC5E,OAAO,EAAE,sBAAsB,EAAE,MAAM,0BAA0B,CAAC;AAElE,OAAO,EAAE,sBAAsB,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAuC3D,MAAM,kBAAkB;IACtB,KAAK,GAAqC,EAAE,CAAC;IAC5B,YAAY,GAA0C,EAAE,CAAC;IAE1E,QAAQ,CACN,GAAW,EACX,SAA8F;QAE9F,IAAI,CAAC,YAAY,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,sBAAsB,CAAc,GAAG,CAAC,CAAC,CAAC,CAAC;QAChF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,KAAK,CAAC,KAAiB,EAAE,EAAU;QACjC,MAAM,EAAE,aAAa,EAAE,iBAAiB,EAAE,WAAW,EAAE,GAAG,gBAAgB,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QAC1F,MAAM,EAAE,cAAc,EAAE,aAAa,EAAE,GAAG,oBAAoB,CAC5D,KAAK,EACL,EAAE,EACF,aAAa,EACb,sBAAsB,CAAC,aAAa,IAAI,EAAE,EAC1C,gBAAgB,CAAC,aAAa,IAAI,EAAE,CACrC,CAAC;QAEF,MAAM,GAAG,GAAG,IAAI,WAAW,CAAC,KAAK,EAAE,EAAE,EAAE;YACrC,GAAG,gBAAgB;YACnB,aAAa;SACM,CAAC,CAAC;QAEvB,MAAM,MAAM,GAAG,mBAAmB,CAAC,KAAK,EAAE,EAAE,EAAE,GAAG,EAAE,WAAW,EAAE,IAAI,CAAC,YAAY,CAAC,CAAC;QAEnF,OAAO,EAAE,GAAG,EAAE,cAAc,EAAE,MAAM,EAAE,CAAC;IACzC,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,MAAM,UAAU,wBAAwB;IACtC,OAAO,OAAO,CAA8C,kBAAkB,CAAC,CAAC;AAClF,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@composurecdk/apigateway",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.2",
|
|
4
4
|
"description": "Composable API Gateway REST API builder with well-architected defaults",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -35,16 +35,17 @@
|
|
|
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
|
},
|
|
43
44
|
"devDependencies": {
|
|
44
|
-
"@types/node": "^25.
|
|
45
|
-
"aws-cdk-lib": "^2.
|
|
45
|
+
"@types/node": "^25.6.0",
|
|
46
|
+
"aws-cdk-lib": "^2.250.0",
|
|
46
47
|
"constructs": "^10.6.0",
|
|
47
|
-
"typescript": "^6.0.
|
|
48
|
-
"vitest": "^4.1.
|
|
48
|
+
"typescript": "^6.0.3",
|
|
49
|
+
"vitest": "^4.1.4"
|
|
49
50
|
}
|
|
50
51
|
}
|