@composurecdk/bedrock 0.10.1
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/LICENSE +21 -0
- package/README.md +115 -0
- package/dist/commonjs/grants.d.ts +27 -0
- package/dist/commonjs/grants.d.ts.map +1 -0
- package/dist/commonjs/grants.js +42 -0
- package/dist/commonjs/grants.js.map +1 -0
- package/dist/commonjs/index.d.ts +8 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +18 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/inference-profile.d.ts +88 -0
- package/dist/commonjs/inference-profile.d.ts.map +1 -0
- package/dist/commonjs/inference-profile.js +125 -0
- package/dist/commonjs/inference-profile.js.map +1 -0
- package/dist/commonjs/inference-target.d.ts +41 -0
- package/dist/commonjs/inference-target.d.ts.map +1 -0
- package/dist/commonjs/inference-target.js +104 -0
- package/dist/commonjs/inference-target.js.map +1 -0
- package/dist/commonjs/model-alarm-builder.d.ts +67 -0
- package/dist/commonjs/model-alarm-builder.d.ts.map +1 -0
- package/dist/commonjs/model-alarm-builder.js +58 -0
- package/dist/commonjs/model-alarm-builder.js.map +1 -0
- package/dist/commonjs/model-alarm-config.d.ts +83 -0
- package/dist/commonjs/model-alarm-config.d.ts.map +1 -0
- package/dist/commonjs/model-alarm-config.js +3 -0
- package/dist/commonjs/model-alarm-config.js.map +1 -0
- package/dist/commonjs/model-alarm-defaults.d.ts +22 -0
- package/dist/commonjs/model-alarm-defaults.d.ts.map +1 -0
- package/dist/commonjs/model-alarm-defaults.js +30 -0
- package/dist/commonjs/model-alarm-defaults.js.map +1 -0
- package/dist/commonjs/model-alarms.d.ts +20 -0
- package/dist/commonjs/model-alarms.d.ts.map +1 -0
- package/dist/commonjs/model-alarms.js +115 -0
- package/dist/commonjs/model-alarms.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/esm/grants.d.ts +27 -0
- package/dist/esm/grants.d.ts.map +1 -0
- package/dist/esm/grants.js +39 -0
- package/dist/esm/grants.js.map +1 -0
- package/dist/esm/index.d.ts +8 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/inference-profile.d.ts +88 -0
- package/dist/esm/inference-profile.d.ts.map +1 -0
- package/dist/esm/inference-profile.js +121 -0
- package/dist/esm/inference-profile.js.map +1 -0
- package/dist/esm/inference-target.d.ts +41 -0
- package/dist/esm/inference-target.d.ts.map +1 -0
- package/dist/esm/inference-target.js +98 -0
- package/dist/esm/inference-target.js.map +1 -0
- package/dist/esm/model-alarm-builder.d.ts +67 -0
- package/dist/esm/model-alarm-builder.d.ts.map +1 -0
- package/dist/esm/model-alarm-builder.js +55 -0
- package/dist/esm/model-alarm-builder.js.map +1 -0
- package/dist/esm/model-alarm-config.d.ts +83 -0
- package/dist/esm/model-alarm-config.d.ts.map +1 -0
- package/dist/esm/model-alarm-config.js +2 -0
- package/dist/esm/model-alarm-config.js.map +1 -0
- package/dist/esm/model-alarm-defaults.d.ts +22 -0
- package/dist/esm/model-alarm-defaults.d.ts.map +1 -0
- package/dist/esm/model-alarm-defaults.js +27 -0
- package/dist/esm/model-alarm-defaults.js.map +1 -0
- package/dist/esm/model-alarms.d.ts +20 -0
- package/dist/esm/model-alarms.d.ts.map +1 -0
- package/dist/esm/model-alarms.js +111 -0
- package/dist/esm/model-alarms.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/package.json +87 -0
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import type { Alarm } from "aws-cdk-lib/aws-cloudwatch";
|
|
2
|
+
import type { IConstruct } from "constructs";
|
|
3
|
+
import { COPY_STATE, type Lifecycle, type Resolvable } from "@composurecdk/core";
|
|
4
|
+
import { type ITaggedBuilder } from "@composurecdk/cloudformation";
|
|
5
|
+
import { AlarmDefinitionBuilder } from "@composurecdk/cloudwatch";
|
|
6
|
+
import type { ModelAlarmConfig } from "./model-alarm-config.js";
|
|
7
|
+
import { type ModelAlarmTarget, type ModelMetrics } from "./model-alarms.js";
|
|
8
|
+
/** Configuration properties for {@link createModelAlarmBuilder}. */
|
|
9
|
+
export interface ModelAlarmBuilderProps {
|
|
10
|
+
/** The foundation model or inference profile to alarm on. Required. */
|
|
11
|
+
model: Resolvable<ModelAlarmTarget>;
|
|
12
|
+
/**
|
|
13
|
+
* Configuration for the recommended alarms. Throttle, server-error and
|
|
14
|
+
* client-error alarms are created by default; latency, time-to-first-token
|
|
15
|
+
* and quota alarms are opt-in. Set to `false` to disable them; alarms added
|
|
16
|
+
* with `addAlarm()` are unaffected.
|
|
17
|
+
*
|
|
18
|
+
* No alarm actions are configured. Use `alarmActionsPolicy` or an
|
|
19
|
+
* `afterBuild` hook to route them.
|
|
20
|
+
*/
|
|
21
|
+
recommendedAlarms?: ModelAlarmConfig | false;
|
|
22
|
+
}
|
|
23
|
+
/** The build output of an {@link IModelAlarmBuilder}. */
|
|
24
|
+
export interface ModelAlarmBuilderResult {
|
|
25
|
+
/** The CloudWatch alarms created, keyed by alarm key. */
|
|
26
|
+
alarms: Record<string, Alarm>;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A fluent builder for CloudWatch alarms on an Amazon Bedrock model.
|
|
30
|
+
*
|
|
31
|
+
* @see {@link createModelAlarmBuilder}
|
|
32
|
+
*/
|
|
33
|
+
export type IModelAlarmBuilder = ITaggedBuilder<ModelAlarmBuilderProps, ModelAlarmBuilder>;
|
|
34
|
+
declare class ModelAlarmBuilder implements Lifecycle<ModelAlarmBuilderResult> {
|
|
35
|
+
#private;
|
|
36
|
+
props: Partial<ModelAlarmBuilderProps>;
|
|
37
|
+
/**
|
|
38
|
+
* Adds a custom alarm. The metric factory receives the model's
|
|
39
|
+
* {@link ModelMetrics}, e.g.
|
|
40
|
+
* `(a) => a.metric((m) => m.metric("OutputTokenCount", { statistic: "Sum" })).threshold(1e6)`.
|
|
41
|
+
*/
|
|
42
|
+
addAlarm(key: string, configure: (alarm: AlarmDefinitionBuilder<ModelMetrics>) => AlarmDefinitionBuilder<ModelMetrics>): this;
|
|
43
|
+
/** @internal — see ADR-0005. */
|
|
44
|
+
[COPY_STATE](target: ModelAlarmBuilder): void;
|
|
45
|
+
build(scope: IConstruct, id: string, context?: Record<string, object>): ModelAlarmBuilderResult;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Creates CloudWatch alarms on an Amazon Bedrock model's `AWS/Bedrock`
|
|
49
|
+
* metrics.
|
|
50
|
+
*
|
|
51
|
+
* The metrics are per model per account and Region, whichever principal
|
|
52
|
+
* invokes it, so build one per model in each Region that invokes it rather
|
|
53
|
+
* than one per caller. For a cross-Region profile, the metrics are emitted
|
|
54
|
+
* in the source Region.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* ```ts
|
|
58
|
+
* createModelAlarmBuilder()
|
|
59
|
+
* .model(haiku)
|
|
60
|
+
* .recommendedAlarms({ invocationLatency: { threshold: 10_000 } });
|
|
61
|
+
* ```
|
|
62
|
+
*
|
|
63
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genops02-bp02.html
|
|
64
|
+
*/
|
|
65
|
+
export declare function createModelAlarmBuilder(): IModelAlarmBuilder;
|
|
66
|
+
export {};
|
|
67
|
+
//# sourceMappingURL=model-alarm-builder.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarm-builder.d.ts","sourceRoot":"","sources":["../../src/model-alarm-builder.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,4BAA4B,CAAC;AACxD,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,EAAE,UAAU,EAAE,KAAK,SAAS,EAAW,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC1F,OAAO,EAAE,KAAK,cAAc,EAAiB,MAAM,8BAA8B,CAAC;AAClF,O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+
exports.createModelAlarmBuilder = createModelAlarmBuilder;
|
|
4
|
+
const core_1 = require("@composurecdk/core");
|
|
5
|
+
const cloudformation_1 = require("@composurecdk/cloudformation");
|
|
6
|
+
const cloudwatch_1 = require("@composurecdk/cloudwatch");
|
|
7
|
+
const model_alarms_js_1 = require("./model-alarms.js");
|
|
8
|
+
class ModelAlarmBuilder {
|
|
9
|
+
props = {};
|
|
10
|
+
#customAlarms = [];
|
|
11
|
+
/**
|
|
12
|
+
* Adds a custom alarm. The metric factory receives the model's
|
|
13
|
+
* {@link ModelMetrics}, e.g.
|
|
14
|
+
* `(a) => a.metric((m) => m.metric("OutputTokenCount", { statistic: "Sum" })).threshold(1e6)`.
|
|
15
|
+
*/
|
|
16
|
+
addAlarm(key, configure) {
|
|
17
|
+
this.#customAlarms.push(configure(new cloudwatch_1.AlarmDefinitionBuilder(key)));
|
|
18
|
+
return this;
|
|
19
|
+
}
|
|
20
|
+
/** @internal — see ADR-0005. */
|
|
21
|
+
[core_1.COPY_STATE](target) {
|
|
22
|
+
target.#customAlarms.push(...this.#customAlarms);
|
|
23
|
+
}
|
|
24
|
+
build(scope, id, context = {}) {
|
|
25
|
+
if (this.props.model === undefined) {
|
|
26
|
+
throw new Error(`ModelAlarmBuilder "${id}" requires a model. Call .model().`);
|
|
27
|
+
}
|
|
28
|
+
const metrics = (0, model_alarms_js_1.modelMetrics)((0, core_1.resolve)(this.props.model, context));
|
|
29
|
+
const { recommendedAlarms } = this.props;
|
|
30
|
+
const recommended = recommendedAlarms === false
|
|
31
|
+
? []
|
|
32
|
+
: (0, model_alarms_js_1.resolveModelAlarmDefinitions)(scope, metrics, recommendedAlarms);
|
|
33
|
+
const custom = this.#customAlarms.map((b) => b.resolve(metrics));
|
|
34
|
+
return { alarms: (0, cloudwatch_1.createAlarms)(scope, id, [...recommended, ...custom]) };
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Creates CloudWatch alarms on an Amazon Bedrock model's `AWS/Bedrock`
|
|
39
|
+
* metrics.
|
|
40
|
+
*
|
|
41
|
+
* The metrics are per model per account and Region, whichever principal
|
|
42
|
+
* invokes it, so build one per model in each Region that invokes it rather
|
|
43
|
+
* than one per caller. For a cross-Region profile, the metrics are emitted
|
|
44
|
+
* in the source Region.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```ts
|
|
48
|
+
* createModelAlarmBuilder()
|
|
49
|
+
* .model(haiku)
|
|
50
|
+
* .recommendedAlarms({ invocationLatency: { threshold: 10_000 } });
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genops02-bp02.html
|
|
54
|
+
*/
|
|
55
|
+
function createModelAlarmBuilder() {
|
|
56
|
+
return (0, cloudformation_1.taggedBuilder)(ModelAlarmBuilder);
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=model-alarm-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarm-builder.js","sourceRoot":"","sources":["../../src/model-alarm-builder.ts"],"names":[],"mappings":";;AAwGA,0DAEC;AAxGD,6CAA0F;AAC1F,iEAAkF;AAClF,yDAAgF;AAEhF,uDAK2B;AAgC3B,MAAM,iBAAiB;IACrB,KAAK,GAAoC,EAAE,CAAC;IACnC,aAAa,GAA2C,EAAE,CAAC;IAEpE;;;;OAIG;IACH,QAAQ,CACN,GAAW,EACX,SAEyC;QAEzC,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,mCAAsB,CAAe,GAAG,CAAC,CAAC,CAAC,CAAC;QAClF,OAAO,IAAI,CAAC;IACd,CAAC;IAED,gCAAgC;IAChC,CAAC,iBAAU,CAAC,CAAC,MAAyB;QACpC,MAAM,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC,CAAC;IACnD,CAAC;IAED,KAAK,CACH,KAAiB,EACjB,EAAU,EACV,UAAkC,EAAE;QAEpC,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,sBAAsB,EAAE,oCAAoC,CAAC,CAAC;QAChF,CAAC;QACD,MAAM,OAAO,GAAG,IAAA,8BAAY,EAAC,IAAA,cAAO,EAAC,IAAI,CAAC,KAAK,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC,CAAC;QACjE,MAAM,EAAE,iBAAiB,EAAE,GAAG,IAAI,CAAC,KAAK,CAAC;QACzC,MAAM,WAAW,GACf,iBAAiB,KAAK,KAAK;YACzB,CAAC,CAAC,EAAE;YACJ,CAAC,CAAC,IAAA,8CAA4B,EAAC,KAAK,EAAE,OAAO,EAAE,iBAAiB,CAAC,CAAC;QACtE,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,CAAC;QACjE,OAAO,EAAE,MAAM,EAAE,IAAA,yBAAY,EAAC,KAAK,EAAE,EAAE,EAAE,CAAC,GAAG,WAAW,EAAE,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC;IAC1E,CAAC;CACF;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,SAAgB,uBAAuB;IACrC,OAAO,IAAA,8BAAa,EAA4C,iBAAiB,CAAC,CAAC;AACrF,CAAC"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import type { AlarmConfig } from "@composurecdk/cloudwatch";
|
|
2
|
+
/**
|
|
3
|
+
* Configuration for an alarm whose threshold is a fraction of a quota the
|
|
4
|
+
* caller supplies.
|
|
5
|
+
*/
|
|
6
|
+
export type QuotaAlarmConfig = Omit<AlarmConfig, "threshold"> & {
|
|
7
|
+
/** The applied quota, in the metric's unit. Required when enabling the alarm. */
|
|
8
|
+
quota: number;
|
|
9
|
+
/** Threshold as a fraction of {@link quota}, in (0, 1]. */
|
|
10
|
+
thresholdPercent?: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Controls which recommended alarms are created for a model. Set an alarm to
|
|
14
|
+
* `false` to disable it, or provide a config to enable it (if opt-in) or tune
|
|
15
|
+
* it.
|
|
16
|
+
*
|
|
17
|
+
* Every alarm uses the `AWS/Bedrock` namespace, the `ModelId` dimension and a
|
|
18
|
+
* 1-minute period. Bedrock metrics aggregate every caller of the model in the
|
|
19
|
+
* account and Region.
|
|
20
|
+
*
|
|
21
|
+
* AWS publishes no recommended thresholds for Bedrock. The Well-Architected
|
|
22
|
+
* Generative AI Lens names which signals to alarm on; the thresholds are this
|
|
23
|
+
* library's.
|
|
24
|
+
*
|
|
25
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genops02-bp02.html
|
|
26
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/monitoring-runtime-metrics.html
|
|
27
|
+
*/
|
|
28
|
+
export interface ModelAlarmConfig {
|
|
29
|
+
/**
|
|
30
|
+
* Master switch: set to `false` to disable all recommended alarms.
|
|
31
|
+
* @default true
|
|
32
|
+
*/
|
|
33
|
+
enabled?: boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Alarm when requests are throttled. Throttled requests are not counted in
|
|
36
|
+
* `Invocations`, and SDK retries mean a count here was already retried.
|
|
37
|
+
*
|
|
38
|
+
* Metric: `InvocationThrottles`, statistic Sum. On by default.
|
|
39
|
+
*
|
|
40
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genops02-bp03.html
|
|
41
|
+
*/
|
|
42
|
+
invocationThrottles?: AlarmConfig | false;
|
|
43
|
+
/**
|
|
44
|
+
* Alarm on server-side (5xx) invocation errors.
|
|
45
|
+
*
|
|
46
|
+
* Metric: `InvocationServerErrors`, statistic Sum. On by default.
|
|
47
|
+
*/
|
|
48
|
+
invocationServerErrors?: AlarmConfig | false;
|
|
49
|
+
/**
|
|
50
|
+
* Alarm on client-side (4xx) invocation errors, including `AccessDenied`
|
|
51
|
+
* from a Region missing from a cross-Region grant and validation errors
|
|
52
|
+
* such as a prompt over the context window.
|
|
53
|
+
*
|
|
54
|
+
* Metric: `InvocationClientErrors`, statistic Sum. On by default.
|
|
55
|
+
*/
|
|
56
|
+
invocationClientErrors?: AlarmConfig | false;
|
|
57
|
+
/**
|
|
58
|
+
* Alarm when invocation latency, in milliseconds to the last token, exceeds
|
|
59
|
+
* the threshold. Latency depends on the model and output length, so there
|
|
60
|
+
* is no default threshold.
|
|
61
|
+
*
|
|
62
|
+
* Metric: `InvocationLatency`, statistic p90. Opt-in; threshold required.
|
|
63
|
+
*/
|
|
64
|
+
invocationLatency?: AlarmConfig | false;
|
|
65
|
+
/**
|
|
66
|
+
* Alarm when time to first token, in milliseconds, exceeds the threshold.
|
|
67
|
+
* Emitted for streaming APIs only.
|
|
68
|
+
*
|
|
69
|
+
* Metric: `TimeToFirstToken`, statistic p90. Opt-in; threshold required.
|
|
70
|
+
*/
|
|
71
|
+
timeToFirstToken?: AlarmConfig | false;
|
|
72
|
+
/**
|
|
73
|
+
* Alarm when estimated tokens-per-minute usage approaches the applied
|
|
74
|
+
* quota. AWS advises against relying on this metric alone for quota use.
|
|
75
|
+
*
|
|
76
|
+
* Metric: `EstimatedTPMQuotaUsage`, statistic Maximum. Opt-in; `quota`
|
|
77
|
+
* required.
|
|
78
|
+
*
|
|
79
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genrel01-bp01.html
|
|
80
|
+
*/
|
|
81
|
+
estimatedTpmQuotaUsage?: QuotaAlarmConfig | false;
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=model-alarm-config.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarm-config.d.ts","sourceRoot":"","sources":["../../src/model-alarm-config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAE5D;;;GAGG;AACH,MAAM,MAAM,gBAAgB,GAAG,IAAI,CAAC,WAAW,EAAE,WAAW,CAAC,GAAG;IAC9D,iFAAiF;IACjF,KAAK,EAAE,MAAM,CAAC;IACd,2DAA2D;IAC3D,gBAAgB,CAAC,EAAE,MAAM,CAAC;CAC3B,CAAC;AAEF;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,gBAAgB;IAC/B;;;OAGG;IACH,OAAO,CAAC,EAAE,OAAO,CAAC;IAElB;;;;;;;OAOG;IACH,mBAAmB,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAE1C;;;;OAIG;IACH,sBAAsB,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAE7C;;;;;;OAMG;IACH,sBAAsB,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAE7C;;;;;;OAMG;IACH,iBAAiB,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAExC;;;;;OAKG;IACH,gBAAgB,CAAC,EAAE,WAAW,GAAG,KAAK,CAAC;IAEvC;;;;;;;;OAQG;IACH,sBAAsB,CAAC,EAAE,gBAAgB,GAAG,KAAK,CAAC;CACnD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarm-config.js","sourceRoot":"","sources":["../../src/model-alarm-config.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AlarmConfigDefaults } from "@composurecdk/cloudwatch";
|
|
2
|
+
interface ModelAlarmDefaults {
|
|
3
|
+
enabled: true;
|
|
4
|
+
invocationThrottles: AlarmConfigDefaults;
|
|
5
|
+
invocationServerErrors: AlarmConfigDefaults;
|
|
6
|
+
invocationClientErrors: AlarmConfigDefaults;
|
|
7
|
+
/** Applied to the opt-in alarms; their threshold comes from the caller. */
|
|
8
|
+
optIn: Omit<AlarmConfigDefaults, "threshold">;
|
|
9
|
+
estimatedTpmQuotaUsage: {
|
|
10
|
+
thresholdPercent: number;
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Default alarm configuration for a Bedrock model. AWS publishes no Bedrock
|
|
15
|
+
* thresholds; these follow the shape of AWS's recommended Lambda `Errors` and
|
|
16
|
+
* `Throttles` alarms.
|
|
17
|
+
*
|
|
18
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genops02-bp02.html
|
|
19
|
+
*/
|
|
20
|
+
export declare const MODEL_ALARM_DEFAULTS: ModelAlarmDefaults;
|
|
21
|
+
export {};
|
|
22
|
+
//# sourceMappingURL=model-alarm-defaults.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarm-defaults.d.ts","sourceRoot":"","sources":["../../src/model-alarm-defaults.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAYpE,UAAU,kBAAkB;IAC1B,OAAO,EAAE,IAAI,CAAC;IACd,mBAAmB,EAAE,mBAAmB,CAAC;IACzC,sBAAsB,EAAE,mBAAmB,CAAC;IAC5C,sBAAsB,EAAE,mBAAmB,CAAC;IAC5C,2EAA2E;IAC3E,KAAK,EAAE,IAAI,CAAC,mBAAmB,EAAE,WAAW,CAAC,CAAC;IAC9C,sBAAsB,EAAE;QAAE,gBAAgB,EAAE,MAAM,CAAA;KAAE,CAAC;CACtD;AAED;;;;;;GAMG;AACH,eAAO,MAAM,oBAAoB,EAAE,kBAQlC,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MODEL_ALARM_DEFAULTS = void 0;
|
|
4
|
+
const aws_cloudwatch_1 = require("aws-cdk-lib/aws-cloudwatch");
|
|
5
|
+
/**
|
|
6
|
+
* 3 breaching minutes out of 5: SDK retries absorb isolated throttles and
|
|
7
|
+
* transient errors, so a single datapoint is noise. No traffic emits no data.
|
|
8
|
+
*/
|
|
9
|
+
const SUSTAINED = {
|
|
10
|
+
evaluationPeriods: 5,
|
|
11
|
+
datapointsToAlarm: 3,
|
|
12
|
+
treatMissingData: aws_cloudwatch_1.TreatMissingData.NOT_BREACHING,
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Default alarm configuration for a Bedrock model. AWS publishes no Bedrock
|
|
16
|
+
* thresholds; these follow the shape of AWS's recommended Lambda `Errors` and
|
|
17
|
+
* `Throttles` alarms.
|
|
18
|
+
*
|
|
19
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/genops02-bp02.html
|
|
20
|
+
*/
|
|
21
|
+
exports.MODEL_ALARM_DEFAULTS = {
|
|
22
|
+
enabled: true,
|
|
23
|
+
invocationThrottles: { threshold: 0, ...SUSTAINED },
|
|
24
|
+
invocationServerErrors: { threshold: 0, ...SUSTAINED },
|
|
25
|
+
invocationClientErrors: { threshold: 0, ...SUSTAINED },
|
|
26
|
+
optIn: SUSTAINED,
|
|
27
|
+
/** Leaves 20% headroom to act before requests are throttled. */
|
|
28
|
+
estimatedTpmQuotaUsage: { thresholdPercent: 0.8 },
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=model-alarm-defaults.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarm-defaults.js","sourceRoot":"","sources":["../../src/model-alarm-defaults.ts"],"names":[],"mappings":";;;AAAA,+DAA8D;AAG9D;;;GAGG;AACH,MAAM,SAAS,GAAG;IAChB,iBAAiB,EAAE,CAAC;IACpB,iBAAiB,EAAE,CAAC;IACpB,gBAAgB,EAAE,iCAAgB,CAAC,aAAa;CACjD,CAAC;AAYF;;;;;;GAMG;AACU,QAAA,oBAAoB,GAAuB;IACtD,OAAO,EAAE,IAAI;IACb,mBAAmB,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,GAAG,SAAS,EAAE;IACnD,sBAAsB,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,GAAG,SAAS,EAAE;IACtD,sBAAsB,EAAE,EAAE,SAAS,EAAE,CAAC,EAAE,GAAG,SAAS,EAAE;IACtD,KAAK,EAAE,SAAS;IAChB,gEAAgE;IAChE,sBAAsB,EAAE,EAAE,gBAAgB,EAAE,GAAG,EAAE;CAClD,CAAC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { FoundationModelIdentifier } from "aws-cdk-lib/aws-bedrock";
|
|
2
|
+
import { Metric, type MetricOptions } from "aws-cdk-lib/aws-cloudwatch";
|
|
3
|
+
import type { IConstruct } from "constructs";
|
|
4
|
+
import type { AlarmDefinition } from "@composurecdk/cloudwatch";
|
|
5
|
+
import type { InferenceProfile } from "./inference-profile.js";
|
|
6
|
+
import type { ModelAlarmConfig } from "./model-alarm-config.js";
|
|
7
|
+
/** A model whose `AWS/Bedrock` metrics an alarm can read. */
|
|
8
|
+
export type ModelAlarmTarget = FoundationModelIdentifier | InferenceProfile;
|
|
9
|
+
/** `AWS/Bedrock` metrics for one model, for alarms added with `addAlarm()`. */
|
|
10
|
+
export interface ModelMetrics {
|
|
11
|
+
/** The `ModelId` dimension value: the model or inference profile id invoked. */
|
|
12
|
+
readonly modelId: string;
|
|
13
|
+
/** An `AWS/Bedrock` metric dimensioned by {@link modelId}. */
|
|
14
|
+
metric(metricName: string, options?: MetricOptions): Metric;
|
|
15
|
+
}
|
|
16
|
+
/** Returns the `AWS/Bedrock` metrics for `target`. */
|
|
17
|
+
export declare function modelMetrics(target: ModelAlarmTarget): ModelMetrics;
|
|
18
|
+
/** Resolves the recommended alarm configuration for a model's metrics. */
|
|
19
|
+
export declare function resolveModelAlarmDefinitions(scope: IConstruct, metrics: ModelMetrics, config: ModelAlarmConfig | undefined): AlarmDefinition[];
|
|
20
|
+
//# sourceMappingURL=model-alarms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarms.d.ts","sourceRoot":"","sources":["../../src/model-alarms.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAC;AACzE,OAAO,EAAsB,MAAM,EAAE,KAAK,aAAa,EAAE,MAAM,4BAA4B,CAAC;AAC5F,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAC7C,OAAO,KAAK,EAEV,eAAe,EAEhB,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,wBAAwB,CAAC;AAE/D,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AAGhE,6DAA6D;AAC7D,MAAM,MAAM,gBAAgB,GAAG,yBAAyB,GAAG,gBAAgB,CAAC;AAE5E,+EAA+E;AAC/E,MAAM,WAAW,YAAY;IAC3B,gFAAgF;IAChF,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,8DAA8D;IAC9D,MAAM,CAAC,UAAU,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,aAAa,GAAG,MAAM,CAAC;CAC7D;AAID,sDAAsD;AACtD,wBAAgB,YAAY,CAAC,MAAM,EAAE,gBAAgB,GAAG,YAAY,CAanE;AA6GD,0EAA0E;AAC1E,wBAAgB,4BAA4B,CAC1C,KAAK,EAAE,UAAU,EACjB,OAAO,EAAE,YAAY,EACrB,MAAM,EAAE,gBAAgB,GAAG,SAAS,GACnC,eAAe,EAAE,CA6BnB"}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.modelMetrics = modelMetrics;
|
|
4
|
+
exports.resolveModelAlarmDefinitions = resolveModelAlarmDefinitions;
|
|
5
|
+
const aws_cdk_lib_1 = require("aws-cdk-lib");
|
|
6
|
+
const aws_cloudwatch_1 = require("aws-cdk-lib/aws-cloudwatch");
|
|
7
|
+
const cloudwatch_1 = require("@composurecdk/cloudwatch");
|
|
8
|
+
const inference_target_js_1 = require("./inference-target.js");
|
|
9
|
+
const model_alarm_defaults_js_1 = require("./model-alarm-defaults.js");
|
|
10
|
+
const PERIOD = aws_cdk_lib_1.Duration.minutes(1);
|
|
11
|
+
/** Returns the `AWS/Bedrock` metrics for `target`. */
|
|
12
|
+
function modelMetrics(target) {
|
|
13
|
+
const modelId = (0, inference_target_js_1.isInferenceProfile)(target) ? target.profileId : target.modelId;
|
|
14
|
+
return {
|
|
15
|
+
modelId,
|
|
16
|
+
metric: (metricName, options) => new aws_cloudwatch_1.Metric({
|
|
17
|
+
namespace: "AWS/Bedrock",
|
|
18
|
+
metricName,
|
|
19
|
+
dimensionsMap: { ModelId: modelId },
|
|
20
|
+
period: PERIOD,
|
|
21
|
+
...options,
|
|
22
|
+
}),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
const THRESHOLD_ALARMS = {
|
|
26
|
+
invocationThrottles: {
|
|
27
|
+
metricName: "InvocationThrottles",
|
|
28
|
+
statistic: "Sum",
|
|
29
|
+
defaults: model_alarm_defaults_js_1.MODEL_ALARM_DEFAULTS.invocationThrottles,
|
|
30
|
+
describe: (id, t) => `Bedrock is throttling requests to ${id}; the account's quota for the model is exhausted. ` +
|
|
31
|
+
`Threshold: > ${String(t)} per minute.`,
|
|
32
|
+
},
|
|
33
|
+
invocationServerErrors: {
|
|
34
|
+
metricName: "InvocationServerErrors",
|
|
35
|
+
statistic: "Sum",
|
|
36
|
+
defaults: model_alarm_defaults_js_1.MODEL_ALARM_DEFAULTS.invocationServerErrors,
|
|
37
|
+
describe: (id, t) => `Bedrock is returning server errors for ${id}. Threshold: > ${String(t)} per minute.`,
|
|
38
|
+
},
|
|
39
|
+
invocationClientErrors: {
|
|
40
|
+
metricName: "InvocationClientErrors",
|
|
41
|
+
statistic: "Sum",
|
|
42
|
+
defaults: model_alarm_defaults_js_1.MODEL_ALARM_DEFAULTS.invocationClientErrors,
|
|
43
|
+
describe: (id, t) => `Requests to ${id} are failing with client errors, such as AccessDenied or validation ` +
|
|
44
|
+
`errors. Threshold: > ${String(t)} per minute.`,
|
|
45
|
+
},
|
|
46
|
+
invocationLatency: {
|
|
47
|
+
metricName: "InvocationLatency",
|
|
48
|
+
statistic: "p90",
|
|
49
|
+
describe: (id, t) => `p90 invocation latency for ${id} exceeds ${String(t)} ms.`,
|
|
50
|
+
},
|
|
51
|
+
timeToFirstToken: {
|
|
52
|
+
metricName: "TimeToFirstToken",
|
|
53
|
+
statistic: "p90",
|
|
54
|
+
describe: (id, t) => `p90 time to first token for ${id} exceeds ${String(t)} ms.`,
|
|
55
|
+
},
|
|
56
|
+
};
|
|
57
|
+
function toDefinition(key, metric, cfg, description) {
|
|
58
|
+
return {
|
|
59
|
+
key,
|
|
60
|
+
alarmName: cfg.alarmName,
|
|
61
|
+
metric,
|
|
62
|
+
threshold: cfg.threshold,
|
|
63
|
+
comparisonOperator: aws_cloudwatch_1.ComparisonOperator.GREATER_THAN_THRESHOLD,
|
|
64
|
+
evaluationPeriods: cfg.evaluationPeriods,
|
|
65
|
+
datapointsToAlarm: cfg.datapointsToAlarm,
|
|
66
|
+
treatMissingData: cfg.treatMissingData,
|
|
67
|
+
description,
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
function quotaAlarm(scope, metrics, config) {
|
|
71
|
+
if (!config)
|
|
72
|
+
return [];
|
|
73
|
+
const quota = (0, cloudwatch_1.resolveAlarmThresholdBasis)({
|
|
74
|
+
scope,
|
|
75
|
+
value: config.quota,
|
|
76
|
+
resolve: (q) => q,
|
|
77
|
+
warningId: "@composurecdk/bedrock:token-tpm-quota-alarm",
|
|
78
|
+
alarmLabel: "Bedrock estimated TPM quota usage",
|
|
79
|
+
suppressHint: "recommendedAlarms({ estimatedTpmQuotaUsage: false })",
|
|
80
|
+
});
|
|
81
|
+
if (quota === undefined)
|
|
82
|
+
return [];
|
|
83
|
+
const percent = config.thresholdPercent ?? model_alarm_defaults_js_1.MODEL_ALARM_DEFAULTS.estimatedTpmQuotaUsage.thresholdPercent;
|
|
84
|
+
if (!(quota > 0)) {
|
|
85
|
+
throw new Error(`estimatedTpmQuotaUsage: quota must be a positive number, got ${String(quota)}.`);
|
|
86
|
+
}
|
|
87
|
+
if (!(percent > 0 && percent <= 1)) {
|
|
88
|
+
throw new Error(`estimatedTpmQuotaUsage: thresholdPercent must be in (0, 1], got ${String(percent)}.`);
|
|
89
|
+
}
|
|
90
|
+
const cfg = (0, cloudwatch_1.resolveAlarmConfig)({ ...config, threshold: Math.floor(quota * percent) }, { ...model_alarm_defaults_js_1.MODEL_ALARM_DEFAULTS.optIn, threshold: 0 });
|
|
91
|
+
return [
|
|
92
|
+
toDefinition("estimatedTpmQuotaUsage", metrics.metric("EstimatedTPMQuotaUsage", { statistic: "Maximum" }), cfg, `Estimated token usage for ${metrics.modelId} exceeds ${String(percent * 100)}% of the ` +
|
|
93
|
+
`${String(quota)} tokens-per-minute quota.`),
|
|
94
|
+
];
|
|
95
|
+
}
|
|
96
|
+
/** Resolves the recommended alarm configuration for a model's metrics. */
|
|
97
|
+
function resolveModelAlarmDefinitions(scope, metrics, config) {
|
|
98
|
+
if (config?.enabled === false)
|
|
99
|
+
return [];
|
|
100
|
+
const definitions = Object.entries(THRESHOLD_ALARMS).flatMap(([key, spec]) => {
|
|
101
|
+
const userConfig = config?.[key];
|
|
102
|
+
if (userConfig === false || (userConfig === undefined && !spec.defaults))
|
|
103
|
+
return [];
|
|
104
|
+
if (!spec.defaults && userConfig?.threshold === undefined) {
|
|
105
|
+
throw new Error(`The "${key}" alarm has no default threshold. Supply one, e.g. ` +
|
|
106
|
+
`recommendedAlarms({ ${key}: { threshold: … } }).`);
|
|
107
|
+
}
|
|
108
|
+
const cfg = (0, cloudwatch_1.resolveAlarmConfig)(userConfig, spec.defaults ?? { ...model_alarm_defaults_js_1.MODEL_ALARM_DEFAULTS.optIn, threshold: 0 });
|
|
109
|
+
return [
|
|
110
|
+
toDefinition(key, metrics.metric(spec.metricName, { statistic: spec.statistic }), cfg, spec.describe(metrics.modelId, cfg.threshold)),
|
|
111
|
+
];
|
|
112
|
+
});
|
|
113
|
+
return [...definitions, ...quotaAlarm(scope, metrics, config?.estimatedTpmQuotaUsage)];
|
|
114
|
+
}
|
|
115
|
+
//# sourceMappingURL=model-alarms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"model-alarms.js","sourceRoot":"","sources":["../../src/model-alarms.ts"],"names":[],"mappings":";;AA6BA,oCAaC;AA8GD,oEAiCC;AAzLD,6CAAuC;AAEvC,+DAA4F;AAO5F,yDAA0F;AAE1F,+DAA2D;AAE3D,uEAAiE;AAajE,MAAM,MAAM,GAAG,sBAAQ,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC;AAEnC,sDAAsD;AACtD,SAAgB,YAAY,CAAC,MAAwB;IACnD,MAAM,OAAO,GAAG,IAAA,wCAAkB,EAAC,MAAM,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC;IAC/E,OAAO;QACL,OAAO;QACP,MAAM,EAAE,CAAC,UAAU,EAAE,OAAO,EAAE,EAAE,CAC9B,IAAI,uBAAM,CAAC;YACT,SAAS,EAAE,aAAa;YACxB,UAAU;YACV,aAAa,EAAE,EAAE,OAAO,EAAE,OAAO,EAAE;YACnC,MAAM,EAAE,MAAM;YACd,GAAG,OAAO;SACX,CAAC;KACL,CAAC;AACJ,CAAC;AAYD,MAAM,gBAAgB,GAAyC;IAC7D,mBAAmB,EAAE;QACnB,UAAU,EAAE,qBAAqB;QACjC,SAAS,EAAE,KAAK;QAChB,QAAQ,EAAE,8CAAoB,CAAC,mBAAmB;QAClD,QAAQ,EAAE,CAAC,EAAE,EAAE,CAAC,EAAE,EAAE,CAClB,qCAAqC,EAAE,oDAAoD;YAC3F,gBAAgB,MAAM,CAAC,CAAC,CAAC,cAAc;KAC1C;IACD,sBAAsB,EAAE;QACtB,UAAU,EAAE,wBAAwB;QACpC,SAAS,EAAE,KAAK;QA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-0,0 +1,27 @@
|
|
|
1
|
+
import { type IGrantable } from "aws-cdk-lib/aws-iam";
|
|
2
|
+
import { type Grant, type Resolvable } from "@composurecdk/core";
|
|
3
|
+
import { type InferenceTarget } from "./inference-target.js";
|
|
4
|
+
/**
|
|
5
|
+
* Consumer-side grant helpers for invoking Amazon Bedrock models. Pass one to
|
|
6
|
+
* a grantee builder's `grant(...)` — e.g.
|
|
7
|
+
* `createFunctionBuilder().grant(modelGrants.invoke(profile))`.
|
|
8
|
+
*
|
|
9
|
+
* A model is not a construct, so there is no native `grant*` method to
|
|
10
|
+
* delegate to; the statements are those the Bedrock user guide prescribes
|
|
11
|
+
* (ADR-0013 addendum).
|
|
12
|
+
*/
|
|
13
|
+
export declare const modelGrants: {
|
|
14
|
+
/**
|
|
15
|
+
* Invoke the target through `InvokeModel`, `Converse` or their streaming
|
|
16
|
+
* variants.
|
|
17
|
+
*
|
|
18
|
+
* For a cross-Region profile this also grants the foundation model in the
|
|
19
|
+
* source Region and every Region the profile routes to, conditioned on
|
|
20
|
+
* `bedrock:InferenceProfileArn` so those models are reachable only through
|
|
21
|
+
* the profile.
|
|
22
|
+
*
|
|
23
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/gensec01-bp01.html
|
|
24
|
+
*/
|
|
25
|
+
invoke: (target: Resolvable<InferenceTarget>) => Grant<IGrantable>;
|
|
26
|
+
};
|
|
27
|
+
//# sourceMappingURL=grants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grants.d.ts","sourceRoot":"","sources":["../../src/grants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAqB,KAAK,UAAU,EAAE,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAE,KAAK,KAAK,EAAY,KAAK,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,KAAK,eAAe,EAAiC,MAAM,uBAAuB,CAAC;AAU5F;;;;;;;;GAQG;AACH,eAAO,MAAM,WAAW;IACtB;;;;;;;;;;OAUG;qBACc,UAAU,CAAC,eAAe,CAAC,KAAG,KAAK,CAAC,UAAU,CAAC;CAOjE,CAAC"}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { Grant as IamGrant } from "aws-cdk-lib/aws-iam";
|
|
2
|
+
import { grantVia } from "@composurecdk/core";
|
|
3
|
+
import { invocationStatements, scopeOf } from "./inference-target.js";
|
|
4
|
+
/**
|
|
5
|
+
* Streaming operations (`InvokeModelWithResponseStream`, `ConverseStream`)
|
|
6
|
+
* require both actions.
|
|
7
|
+
*
|
|
8
|
+
* @see https://docs.aws.amazon.com/service-authorization/latest/reference/list_amazonbedrock.html
|
|
9
|
+
*/
|
|
10
|
+
const INVOKE_ACTIONS = ["bedrock:InvokeModel", "bedrock:InvokeModelWithResponseStream"];
|
|
11
|
+
/**
|
|
12
|
+
* Consumer-side grant helpers for invoking Amazon Bedrock models. Pass one to
|
|
13
|
+
* a grantee builder's `grant(...)` — e.g.
|
|
14
|
+
* `createFunctionBuilder().grant(modelGrants.invoke(profile))`.
|
|
15
|
+
*
|
|
16
|
+
* A model is not a construct, so there is no native `grant*` method to
|
|
17
|
+
* delegate to; the statements are those the Bedrock user guide prescribes
|
|
18
|
+
* (ADR-0013 addendum).
|
|
19
|
+
*/
|
|
20
|
+
export const modelGrants = {
|
|
21
|
+
/**
|
|
22
|
+
* Invoke the target through `InvokeModel`, `Converse` or their streaming
|
|
23
|
+
* variants.
|
|
24
|
+
*
|
|
25
|
+
* For a cross-Region profile this also grants the foundation model in the
|
|
26
|
+
* source Region and every Region the profile routes to, conditioned on
|
|
27
|
+
* `bedrock:InferenceProfileArn` so those models are reachable only through
|
|
28
|
+
* the profile.
|
|
29
|
+
*
|
|
30
|
+
* @see https://docs.aws.amazon.com/wellarchitected/latest/generative-ai-lens/gensec01-bp01.html
|
|
31
|
+
*/
|
|
32
|
+
invoke: (target) => grantVia(target, (resolved, grantee) => {
|
|
33
|
+
const statements = invocationStatements(resolved, scopeOf(grantee.grantPrincipal));
|
|
34
|
+
for (const { resourceArns, conditions } of statements) {
|
|
35
|
+
IamGrant.addToPrincipal({ grantee, actions: INVOKE_ACTIONS, resourceArns, conditions });
|
|
36
|
+
}
|
|
37
|
+
}),
|
|
38
|
+
};
|
|
39
|
+
//# sourceMappingURL=grants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grants.js","sourceRoot":"","sources":["../../src/grants.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,IAAI,QAAQ,EAAmB,MAAM,qBAAqB,CAAC;AACzE,OAAO,EAAc,QAAQ,EAAmB,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAwB,oBAAoB,EAAE,OAAO,EAAE,MAAM,uBAAuB,CAAC;AAE5F;;;;;GAKG;AACH,MAAM,cAAc,GAAG,CAAC,qBAAqB,EAAE,uCAAuC,CAAC,CAAC;AAExF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,WAAW,GAAG;IACzB;;;;;;;;;;OAUG;IACH,MAAM,EAAE,CAAC,MAAmC,EAAqB,EAAE,CACjE,QAAQ,CAAC,MAAM,EAAE,CAAC,QAAQ,EAAE,OAAmB,EAAE,EAAE;QACjD,MAAM,UAAU,GAAG,oBAAoB,CAAC,QAAQ,EAAE,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC;QACnF,KAAK,MAAM,EAAE,YAAY,EAAE,UAAU,EAAE,IAAI,UAAU,EAAE,CAAC;YACtD,QAAQ,CAAC,cAAc,CAAC,EAAE,OAAO,EAAE,OAAO,EAAE,cAAc,EAAE,YAAY,EAAE,UAAU,EAAE,CAAC,CAAC;QAC1F,CAAC;IACH,CAAC,CAAC;CACL,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export { foundationModelFor, type GeographicInferenceProfile, type GeographicInferenceProfileOptions, type GlobalInferenceProfile, INFERENCE_PROFILE_GEOGRAPHIES, type InferenceProfile, type InferenceProfileGeography, inferenceProfile, } from "./inference-profile.js";
|
|
2
|
+
export { type InferenceTarget, invocationArns } from "./inference-target.js";
|
|
3
|
+
export { modelGrants } from "./grants.js";
|
|
4
|
+
export { createModelAlarmBuilder, type IModelAlarmBuilder, type ModelAlarmBuilderProps, type ModelAlarmBuilderResult, } from "./model-alarm-builder.js";
|
|
5
|
+
export { type ModelAlarmConfig, type QuotaAlarmConfig } from "./model-alarm-config.js";
|
|
6
|
+
export { MODEL_ALARM_DEFAULTS } from "./model-alarm-defaults.js";
|
|
7
|
+
export { type ModelAlarmTarget, type ModelMetrics, modelMetrics } from "./model-alarms.js";
|
|
8
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAClB,KAAK,0BAA0B,EAC/B,KAAK,iCAAiC,EACtC,KAAK,sBAAsB,EAC3B,6BAA6B,EAC7B,KAAK,gBAAgB,EACrB,KAAK,yBAAyB,EAC9B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAE,KAAK,eAAe,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACL,uBAAuB,EACvB,KAAK,kBAAkB,EACvB,KAAK,sBAAsB,EAC3B,KAAK,uBAAuB,GAC7B,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,gBAAgB,EAAE,MAAM,yBAAyB,CAAC;AACvF,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAAE,KAAK,gBAAgB,EAAE,KAAK,YAAY,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { foundationModelFor, INFERENCE_PROFILE_GEOGRAPHIES, inferenceProfile, } from "./inference-profile.js";
|
|
2
|
+
export { invocationArns } from "./inference-target.js";
|
|
3
|
+
export { modelGrants } from "./grants.js";
|
|
4
|
+
export { createModelAlarmBuilder, } from "./model-alarm-builder.js";
|
|
5
|
+
export { MODEL_ALARM_DEFAULTS } from "./model-alarm-defaults.js";
|
|
6
|
+
export { modelMetrics } from "./model-alarms.js";
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,kBAAkB,EAIlB,6BAA6B,EAG7B,gBAAgB,GACjB,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAAwB,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC7E,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACL,uBAAuB,GAIxB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AACjE,OAAO,EAA4C,YAAY,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { FoundationModelIdentifier } from "aws-cdk-lib/aws-bedrock";
|
|
2
|
+
/**
|
|
3
|
+
* Geography prefixes of Amazon Bedrock's system-defined cross-Region inference
|
|
4
|
+
* profiles, e.g. the `eu` in `eu.anthropic.claude-haiku-4-5-20251001-v1:0`.
|
|
5
|
+
*
|
|
6
|
+
* {@link inferenceProfile.parse} recognises a profile id by one of these
|
|
7
|
+
* prefixes. {@link inferenceProfile.geographic} also accepts a geography not
|
|
8
|
+
* yet listed here.
|
|
9
|
+
*
|
|
10
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/geographic-cross-region-inference.html
|
|
11
|
+
*/
|
|
12
|
+
export declare const INFERENCE_PROFILE_GEOGRAPHIES: readonly ["us", "eu", "apac", "jp", "au", "us-gov"];
|
|
13
|
+
/** A known geography prefix. See {@link INFERENCE_PROFILE_GEOGRAPHIES}. */
|
|
14
|
+
export type InferenceProfileGeography = (typeof INFERENCE_PROFILE_GEOGRAPHIES)[number];
|
|
15
|
+
/**
|
|
16
|
+
* A system-defined inference profile that routes requests to Regions within
|
|
17
|
+
* one geography.
|
|
18
|
+
*
|
|
19
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/geographic-cross-region-inference.html
|
|
20
|
+
*/
|
|
21
|
+
export interface GeographicInferenceProfile {
|
|
22
|
+
readonly kind: "geographic";
|
|
23
|
+
readonly geography: InferenceProfileGeography | (string & {});
|
|
24
|
+
/** The foundation model the profile serves. */
|
|
25
|
+
readonly model: FoundationModelIdentifier;
|
|
26
|
+
/** The id to invoke, e.g. `eu.anthropic.claude-haiku-4-5-20251001-v1:0`. */
|
|
27
|
+
readonly profileId: string;
|
|
28
|
+
/**
|
|
29
|
+
* Every Region the profile may route to from the source Region, as listed
|
|
30
|
+
* by `aws bedrock get-inference-profile` or the model's page in the Bedrock
|
|
31
|
+
* user guide. The source Region is always granted, listed or not.
|
|
32
|
+
*/
|
|
33
|
+
readonly routingRegions: readonly string[];
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* A system-defined inference profile that routes requests to any supported
|
|
37
|
+
* commercial Region.
|
|
38
|
+
*
|
|
39
|
+
* @see https://docs.aws.amazon.com/bedrock/latest/userguide/global-cross-region-inference.html
|
|
40
|
+
*/
|
|
41
|
+
export interface GlobalInferenceProfile {
|
|
42
|
+
readonly kind: "global";
|
|
43
|
+
/** The foundation model the profile serves. */
|
|
44
|
+
readonly model: FoundationModelIdentifier;
|
|
45
|
+
/** The id to invoke, e.g. `global.anthropic.claude-haiku-4-5-20251001-v1:0`. */
|
|
46
|
+
readonly profileId: string;
|
|
47
|
+
}
|
|
48
|
+
/** A system-defined cross-Region inference profile. */
|
|
49
|
+
export type InferenceProfile = GeographicInferenceProfile | GlobalInferenceProfile;
|
|
50
|
+
/** Options for {@link inferenceProfile.geographic}. */
|
|
51
|
+
export type GeographicInferenceProfileOptions = Pick<GeographicInferenceProfile, "model" | "geography" | "routingRegions">;
|
|
52
|
+
declare function geographic(options: GeographicInferenceProfileOptions): GeographicInferenceProfile;
|
|
53
|
+
declare function global(model: FoundationModelIdentifier): GlobalInferenceProfile;
|
|
54
|
+
declare function parse(profileId: string): GlobalInferenceProfile;
|
|
55
|
+
declare function parse(profileId: string, routingRegions: readonly string[]): InferenceProfile;
|
|
56
|
+
/**
|
|
57
|
+
* Factories for {@link InferenceProfile} values.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```ts
|
|
61
|
+
* const haiku = inferenceProfile.geographic({
|
|
62
|
+
* model: FoundationModelIdentifier.ANTHROPIC_CLAUDE_HAIKU_4_5_20251001_V1_0,
|
|
63
|
+
* geography: "eu",
|
|
64
|
+
* routingRegions: ["eu-central-1", "eu-north-1", "eu-south-1", "eu-south-2", "eu-west-1", "eu-west-3"],
|
|
65
|
+
* });
|
|
66
|
+
* haiku.profileId; // "eu.anthropic.claude-haiku-4-5-20251001-v1:0"
|
|
67
|
+
* ```
|
|
68
|
+
*/
|
|
69
|
+
export declare const inferenceProfile: {
|
|
70
|
+
/** A profile routing within one geography. */
|
|
71
|
+
geographic: typeof geographic;
|
|
72
|
+
/** A profile routing to any supported commercial Region. */
|
|
73
|
+
global: typeof global;
|
|
74
|
+
/**
|
|
75
|
+
* Reads a profile id such as `eu.anthropic.claude-haiku-4-5-20251001-v1:0`.
|
|
76
|
+
* Throws unless the id starts with a known geography or `global`. A
|
|
77
|
+
* geographic id needs its `routingRegions`; a global id takes none.
|
|
78
|
+
*/
|
|
79
|
+
parse: typeof parse;
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* The foundation model a system-defined inference profile serves:
|
|
83
|
+
* `eu.anthropic.x` → `anthropic.x`. Throws unless the id starts with a known
|
|
84
|
+
* geography or `global`.
|
|
85
|
+
*/
|
|
86
|
+
export declare function foundationModelFor(profileId: string): FoundationModelIdentifier;
|
|
87
|
+
export {};
|
|
88
|
+
//# sourceMappingURL=inference-profile.d.ts.map
|