@ashishpandeyone/cdk-lambda-web-function 0.1.2 → 0.1.3
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/.jsii +649 -79
- package/.jsii.tabl.json +1 -1
- package/API.md +506 -0
- package/README.md +47 -5
- package/assets/web-function-provider/index.js +26 -26
- package/lib/nodejs-web-function.js +1 -1
- package/lib/private/metrics.d.ts +23 -0
- package/lib/private/metrics.js +49 -0
- package/lib/private/provider-protocol.d.ts +17 -0
- package/lib/private/provider-protocol.js +6 -2
- package/lib/private/provider.d.ts +3 -3
- package/lib/private/provider.js +15 -5
- package/lib/private/regional-alarm.d.ts +23 -0
- package/lib/private/regional-alarm.js +40 -0
- package/lib/private/rollback.d.ts +9 -0
- package/lib/private/rollback.js +3 -0
- package/lib/private/traffic-shift.d.ts +2 -0
- package/lib/private/traffic-shift.js +3 -2
- package/lib/web-function-deployment.d.ts +64 -0
- package/lib/web-function-deployment.js +41 -6
- package/lib/web-function-endpoint.d.ts +46 -0
- package/lib/web-function-endpoint.js +155 -2
- package/lib/web-function-grants.js +1 -1
- package/lib/web-function-revision.js +1 -1
- package/lib/web-function.d.ts +30 -0
- package/lib/web-function.js +52 -2
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -250,18 +250,18 @@ fn.addEndpoint('Stable', { authType: WebFunctionAuthType.AWS_IAM, revision: prev
|
|
|
250
250
|
By default an endpoint moves all its traffic to a new revision at once. With `deployment`, it moves in steps instead, watching CloudWatch alarms: if one goes into ALARM, the endpoint goes back to the revision it served before and the deployment fails, so CloudFormation rolls the stack back.
|
|
251
251
|
|
|
252
252
|
```ts
|
|
253
|
-
// errorRate: a cloudwatch.IAlarm, for example on the share of 5xx responses
|
|
254
253
|
const api = new WebFunction(this, 'Api', {
|
|
255
254
|
runtime: lambda.Runtime.NODEJS_24_X,
|
|
256
255
|
code: lambda.Code.fromAsset('dist'),
|
|
257
256
|
authType: WebFunctionAuthType.AWS_IAM,
|
|
258
257
|
endpoint: {
|
|
259
|
-
// 10% of the traffic for 10 minutes, then all of it.
|
|
260
|
-
deployment: WebFunctionDeployment.canary({ percent: 10, bake: Duration.minutes(10),
|
|
258
|
+
// 10% of the traffic for 10 minutes, then all of it; back if more than 2% of requests get a 5XX.
|
|
259
|
+
deployment: WebFunctionDeployment.canary({ percent: 10, bake: Duration.minutes(10), rollbackOn: { serverErrorRate: 2 } }),
|
|
261
260
|
},
|
|
262
261
|
});
|
|
263
262
|
|
|
264
|
-
// 25% more every 5 minutes. Each endpoint has its own deployment.
|
|
263
|
+
// 25% more every 5 minutes, watching an alarm of your own. Each endpoint has its own deployment.
|
|
264
|
+
// errorRate: any cloudwatch.IAlarm, e.g. on a metric your application publishes.
|
|
265
265
|
api.addEndpoint('Public', {
|
|
266
266
|
authType: WebFunctionAuthType.NONE,
|
|
267
267
|
deployment: WebFunctionDeployment.linear({ percent: 25, interval: Duration.minutes(5), alarms: [errorRate] }),
|
|
@@ -284,10 +284,52 @@ How it works:
|
|
|
284
284
|
- A deployment can take up to an hour: CloudFormation waits for it before it updates the endpoint. Deployments of different endpoints run in parallel.
|
|
285
285
|
- A canary or linear endpoint always moves to the current revision, so it can't also use `revision`, `additionalRevisions` or `routeTraffic()`. Use `allAtOnce()` to deploy a fix immediately, for example while an alarm is in ALARM.
|
|
286
286
|
|
|
287
|
-
|
|
287
|
+
#### Rollback alarms
|
|
288
|
+
|
|
289
|
+
`rollbackOn` makes the endpoint create alarms on its own error rates, and the deployment watches them alongside any `alarms` you pass:
|
|
290
|
+
|
|
291
|
+
| `rollbackOn` | Default | |
|
|
292
|
+
|---|---|---|
|
|
293
|
+
| `serverErrorRate` | none | roll back when more than this % of requests get a 5XX |
|
|
294
|
+
| `clientErrorRate` | none | the same for 4XX (including 429s from `rateLimit` and `maxEnvironments`) |
|
|
295
|
+
| `minimumRequests` | 10 | periods with fewer requests don't count, so one failed request out of two doesn't roll back |
|
|
296
|
+
| `period`, `evaluationPeriods` | 1 minute, 2 | consecutive periods over the threshold before rolling back |
|
|
297
|
+
|
|
298
|
+
- **One alarm per threshold in every region the endpoint serves from.** Metrics are published in the region that served the request, and an alarm can only watch metrics in its own region. The home region's alarm is a normal CloudFormation alarm; the others are created and deleted with the endpoint by a custom resource, and named `LambdaWeb-<function>-<endpoint>-5xx-rate` (or `-4xx-rate`). `endpoint.rollbackAlarms` lists them all, e.g. to add notifications to the home region's.
|
|
299
|
+
- **The alarms see the whole endpoint.** Lambda Web Function metrics have no revision dimension, so at 10% a new revision's errors are about a tenth of the endpoint's: set the threshold low enough to notice that, or alarm on metrics your application publishes with its version.
|
|
300
|
+
- **5XX includes the platform's 503s** while the endpoint scales up for a sudden burst, not only your application's errors.
|
|
301
|
+
- **A step should outlast the alarm.** CloudWatch needs `period` × `evaluationPeriods`, plus a couple of minutes of delay, to fire; the construct warns when a canary's bake or a linear interval is shorter.
|
|
288
302
|
|
|
289
303
|
The deployment runs in a few resources per stack, created on first use: two Lambda functions, a Step Functions state machine that calls them every 15 seconds, and a log group.
|
|
290
304
|
|
|
305
|
+
## Metrics
|
|
306
|
+
|
|
307
|
+
Endpoints and functions have helpers for their CloudWatch metrics (namespace `AWS/Lambda/Web`), like `lambda.Function`'s:
|
|
308
|
+
|
|
309
|
+
```ts
|
|
310
|
+
fn.endpoint.metricServerErrorRate().createAlarm(this, 'ErrorRate', { threshold: 1, evaluationPeriods: 3 });
|
|
311
|
+
fn.endpoint.metricRequestThrottles().createAlarm(this, 'Throttled', { threshold: 10, evaluationPeriods: 3 });
|
|
312
|
+
fn.metricConcurrentRequests().createAlarm(this, 'Saturated', { threshold: 60, evaluationPeriods: 3 });
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
| Endpoint | Metric |
|
|
316
|
+
|---|---|
|
|
317
|
+
| `metricRequests()` | `ExecutedRequests` (there's no separate 2XX count) |
|
|
318
|
+
| `metricClientErrors()`, `metricServerErrors()` | `HTTPResponseStatusClass4XX`, `HTTPResponseStatusClass5XX` |
|
|
319
|
+
| `metricClientErrorRate()`, `metricServerErrorRate()` | 4XX or 5XX as a % of requests (math expression) |
|
|
320
|
+
| `metricActiveEnvironments()` | `EndpointActiveEnvironments`, to compare with `maxEnvironments` |
|
|
321
|
+
| `metricRequestThrottles()`, `metricEnvironmentThrottles()` | 429s from `rateLimit` and from `maxEnvironments` |
|
|
322
|
+
|
|
323
|
+
| Function | Metric |
|
|
324
|
+
|---|---|
|
|
325
|
+
| `metricConcurrentRequests()` | `EnvironmentConcurrentRequests` (maximum), to compare with `maxConcurrencyPerEnvironment` |
|
|
326
|
+
| `metricMaxMemoryUsage()` | `EnvironmentMaxMemoryUsage` (maximum) |
|
|
327
|
+
| `metricActiveTime()`, `metricCpuTime()`, `metricResponseBytes()` | `EnvironmentActiveTime`, `EnvironmentCPUTime`, `ResponseBytes` |
|
|
328
|
+
|
|
329
|
+
- **Metrics stay in the region that served the request**; unlike logs, they aren't copied to the home region. Every helper takes `region`. Dashboards can show any region, but an alarm can only use metrics from its own stack's region.
|
|
330
|
+
- 4XX, 5XX and the throttle metrics only exist once they're non-zero: alarm on them with `treatMissingData: NOT_BREACHING`. The rate helpers count missing errors as 0.
|
|
331
|
+
- There's no latency metric. Publish one from your application (e.g. with the CloudWatch embedded metric format).
|
|
332
|
+
|
|
291
333
|
## Logs in every region
|
|
292
334
|
|
|
293
335
|
Web Functions log in two tiers:
|