@aws-cdk/aws-bedrock-agentcore-alpha 2.253.1-alpha.0 → 2.255.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (81) hide show
  1. package/.jsii +16960 -11718
  2. package/.jsii.tabl.json.gz +0 -0
  3. package/README.md +57 -2910
  4. package/lib/evaluation/custom-evaluator.d.ts +9 -0
  5. package/lib/evaluation/custom-evaluator.js +8 -2
  6. package/lib/evaluation/data-source.js +1 -1
  7. package/lib/evaluation/evaluator-base.js +1 -1
  8. package/lib/evaluation/evaluator-config.js +2 -2
  9. package/lib/evaluation/evaluator.js +1 -1
  10. package/lib/evaluation/online-evaluation-base.js +1 -1
  11. package/lib/evaluation/online-evaluation.d.ts +9 -0
  12. package/lib/evaluation/online-evaluation.js +8 -2
  13. package/lib/evaluation/types.js +5 -5
  14. package/lib/evaluation/validation-helpers.d.ts +8 -0
  15. package/lib/evaluation/validation-helpers.js +62 -1
  16. package/lib/gateway/gateway-base.js +1 -1
  17. package/lib/gateway/gateway.d.ts +3 -2
  18. package/lib/gateway/gateway.js +3 -3
  19. package/lib/gateway/inbound-auth/authorizer.js +4 -4
  20. package/lib/gateway/inbound-auth/custom-claim.js +1 -1
  21. package/lib/gateway/interceptor.js +1 -1
  22. package/lib/gateway/outbound-auth/api-key.d.ts +13 -4
  23. package/lib/gateway/outbound-auth/api-key.js +63 -21
  24. package/lib/gateway/outbound-auth/credential-provider.d.ts +48 -4
  25. package/lib/gateway/outbound-auth/credential-provider.js +49 -2
  26. package/lib/gateway/outbound-auth/iam-role.d.ts +4 -3
  27. package/lib/gateway/outbound-auth/iam-role.js +3 -5
  28. package/lib/gateway/outbound-auth/oauth.d.ts +11 -3
  29. package/lib/gateway/outbound-auth/oauth.js +70 -20
  30. package/lib/gateway/perms.d.ts +15 -3
  31. package/lib/gateway/perms.js +24 -9
  32. package/lib/gateway/protocol.js +2 -2
  33. package/lib/gateway/targets/schema/api-schema.js +4 -4
  34. package/lib/gateway/targets/schema/tool-schema.js +4 -4
  35. package/lib/gateway/targets/target-base.js +1 -1
  36. package/lib/gateway/targets/target-configuration.js +6 -6
  37. package/lib/gateway/targets/target.js +3 -3
  38. package/lib/identity/api-key-credential-provider.d.ts +217 -0
  39. package/lib/identity/api-key-credential-provider.js +249 -0
  40. package/lib/identity/grant-helpers.d.ts +74 -0
  41. package/lib/identity/grant-helpers.js +132 -0
  42. package/lib/identity/oauth2-credential-provider.d.ts +679 -0
  43. package/lib/identity/oauth2-credential-provider.js +863 -0
  44. package/lib/identity/perms.d.ts +119 -0
  45. package/lib/identity/perms.js +178 -0
  46. package/lib/identity/validation-helpers.d.ts +61 -0
  47. package/lib/identity/validation-helpers.js +156 -0
  48. package/lib/identity/workload-identity.d.ts +186 -0
  49. package/lib/identity/workload-identity.js +212 -0
  50. package/lib/index.d.ts +4 -0
  51. package/lib/index.js +8 -1
  52. package/lib/memory/memory-strategy.js +1 -1
  53. package/lib/memory/memory.js +2 -2
  54. package/lib/memory/strategies/managed-strategy.js +1 -1
  55. package/lib/memory/strategies/self-managed-strategy.js +1 -1
  56. package/lib/memory/validation-helpers.js +2 -2
  57. package/lib/network/network-configuration.js +4 -4
  58. package/lib/policy/perms.d.ts +0 -6
  59. package/lib/policy/perms.js +1 -7
  60. package/lib/policy/policy-base.js +1 -1
  61. package/lib/policy/policy-engine-base.js +1 -1
  62. package/lib/policy/policy-engine.js +1 -1
  63. package/lib/policy/policy-statement.d.ts +2 -1
  64. package/lib/policy/policy-statement.js +14 -12
  65. package/lib/policy/policy-types.js +1 -1
  66. package/lib/policy/policy.js +1 -1
  67. package/lib/runtime/inbound-auth/custom-claim.js +1 -1
  68. package/lib/runtime/inbound-auth/runtime-authorizer-configuration.js +1 -1
  69. package/lib/runtime/observability.js +2 -2
  70. package/lib/runtime/runtime-artifact.d.ts +11 -3
  71. package/lib/runtime/runtime-artifact.js +12 -4
  72. package/lib/runtime/runtime-base.js +1 -1
  73. package/lib/runtime/runtime-endpoint-base.js +1 -1
  74. package/lib/runtime/runtime-endpoint.js +1 -1
  75. package/lib/runtime/runtime.js +1 -1
  76. package/lib/runtime/types.d.ts +7 -1
  77. package/lib/runtime/types.js +7 -1
  78. package/lib/tools/browser.js +2 -2
  79. package/lib/tools/code-interpreter.js +2 -2
  80. package/package.json +18 -10
  81. package/rosetta/policy.ts-fixture +11 -0
package/README.md CHANGED
@@ -16,2591 +16,44 @@
16
16
 
17
17
  <!--END STABILITY BANNER-->
18
18
 
19
- | **Language** | **Package** |
20
- | :--------------------------------------------------------------------------------------------- | --------------------------------------- |
21
- | ![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) TypeScript | `@aws-cdk/aws-bedrock-agentcore-alpha` |
22
-
23
- [Amazon Bedrock AgentCore](https://aws.amazon.com/bedrock/agentcore/) enables you to deploy and operate highly capable AI agents securely, at scale. It offers infrastructure purpose-built for dynamic agent workloads, powerful tools to enhance agents, and essential controls for real-world deployment. AgentCore services can be used together or independently and work with any framework including CrewAI, LangGraph, LlamaIndex, and Strands Agents, as well as any foundation model in or outside of Amazon Bedrock, giving you ultimate flexibility. AgentCore eliminates the undifferentiated heavy lifting of building specialized agent infrastructure, so you can accelerate agents to production.
24
-
25
- This construct library facilitates the deployment of Bedrock AgentCore primitives, enabling you to create sophisticated AI applications that can interact with your systems and data sources.
26
-
27
- > **Note:** Users need to ensure their CDK deployment role has the `iam:CreateServiceLinkedRole` permission for AgentCore service-linked roles.
28
-
29
- ## Table of contents
30
-
31
- - [Amazon Bedrock AgentCore Construct Library](#amazon-bedrock-agentcore-construct-library)
32
- - [Table of contents](#table-of-contents)
33
- - [AgentCore Runtime](#agentcore-runtime)
34
- - [Runtime Endpoints](#runtime-endpoints)
35
- - [AgentCore Runtime Properties](#agentcore-runtime-properties)
36
- - [Runtime Endpoint Properties](#runtime-endpoint-properties)
37
- - [Creating a Runtime](#creating-a-runtime)
38
- - [Option 1: Use an existing image in ECR](#option-1-use-an-existing-image-in-ecr)
39
- - [Option 2: Use a local asset](#option-2-use-a-local-asset)
40
- - [Option 3: Use direct code deployment](#option-3-use-direct-code-deployment)
41
- - [Option 4: Use an ECR container image URI](#option-4-use-an-ecr-container-image-uri)
42
- - [Granting Permissions to Invoke Bedrock Models or Inference Profiles](#granting-permissions-to-invoke-bedrock-models-or-inference-profiles)
43
- - [Runtime Versioning](#runtime-versioning)
44
- - [Managing Endpoints and Versions](#managing-endpoints-and-versions)
45
- - [Step 1: Initial Deployment](#step-1-initial-deployment)
46
- - [Step 2: Creating Custom Endpoints](#step-2-creating-custom-endpoints)
47
- - [Step 3: Runtime Update Deployment](#step-3-runtime-update-deployment)
48
- - [Step 4: Testing with Staging Endpoints](#step-4-testing-with-staging-endpoints)
49
- - [Step 5: Promoting to Production](#step-5-promoting-to-production)
50
- - [Creating Standalone Runtime Endpoints](#creating-standalone-runtime-endpoints)
51
- - [Example: Creating an endpoint for an existing runtime](#example-creating-an-endpoint-for-an-existing-runtime)
52
- - [Runtime Authentication Configuration](#runtime-authentication-configuration)
53
- - [IAM Authentication (Default)](#iam-authentication-default)
54
- - [Cognito Authentication](#cognito-authentication)
55
- - [JWT Authentication](#jwt-authentication)
56
- - [OAuth Authentication](#oauth-authentication)
57
- - [Using a Custom IAM Role](#using-a-custom-iam-role)
58
- - [Runtime Network Configuration](#runtime-network-configuration)
59
- - [Public Network Mode (Default)](#public-network-mode-default)
60
- - [VPC Network Mode](#vpc-network-mode)
61
- - [Managing Security Groups with VPC Configuration](#managing-security-groups-with-vpc-configuration)
62
- - [Runtime IAM Permissions](#runtime-iam-permissions)
63
- - [Other configuration](#other-configuration)
64
- - [Lifecycle configuration](#lifecycle-configuration)
65
- - [Request header configuration](#request-header-configuration)
66
- - [Browser](#browser)
67
- - [Browser Network modes](#browser-network-modes)
68
- - [Browser Properties](#browser-properties)
69
- - [Basic Browser Creation](#basic-browser-creation)
70
- - [Browser with Tags](#browser-with-tags)
71
- - [Browser with VPC](#browser-with-vpc)
72
- - [Browser with Recording Configuration](#browser-with-recording-configuration)
73
- - [Browser with Custom Execution Role](#browser-with-custom-execution-role)
74
- - [Browser with S3 Recording and Permissions](#browser-with-s3-recording-and-permissions)
75
- - [Browser with Browser signing](#browser-with-browser-signing)
76
- - [Browser IAM Permissions](#browser-iam-permissions)
77
- - [Code Interpreter](#code-interpreter)
78
- - [Code Interpreter Network Modes](#code-interpreter-network-modes)
79
- - [Code Interpreter Properties](#code-interpreter-properties)
80
- - [Basic Code Interpreter Creation](#basic-code-interpreter-creation)
81
- - [Code Interpreter with VPC](#code-interpreter-with-vpc)
82
- - [Code Interpreter with Sandbox Network Mode](#code-interpreter-with-sandbox-network-mode)
83
- - [Code Interpreter with Custom Execution Role](#code-interpreter-with-custom-execution-role)
84
- - [Code Interpreter IAM Permissions](#code-interpreter-iam-permissions)
85
- - [Code interpreter with tags](#code-interpreter-with-tags)
86
- - [Gateway](#gateway)
87
- - [Gateway Properties](#gateway-properties)
88
- - [Basic Gateway Creation](#basic-gateway-creation)
89
- - [Protocol configuration](#protocol-configuration)
90
- - [Inbound authorization](#inbound-authorization)
91
- - [Gateway with KMS Encryption](#gateway-with-kms-encryption)
92
- - [Gateway with Custom Execution Role](#gateway-with-custom-execution-role)
93
- - [Gateway IAM Permissions](#gateway-iam-permissions)
94
- - [Gateway Target](#gateway-target)
95
- - [Gateway Target Properties](#gateway-target-properties)
96
- - [Targets types](#targets-types)
97
- - [Understanding Tool Naming](#understanding-tool-naming)
98
- - [Tools schema For Lambda target](#tools-schema-for-lambda-target)
99
- - [Api schema For OpenAPI and Smithy target](#api-schema-for-openapi-and-smithy-target)
100
- - [Outbound auth](#outbound-auth)
101
- - [Basic Gateway Target Creation](#basic-gateway-target-creation)
102
- - [Using addTarget methods (Recommended)](#using-addtarget-methods-recommended)
103
- - [Using static factory methods](#using-static-factory-methods)
104
- - [Advanced Usage: Direct Configuration for gateway target](#advanced-usage-direct-configuration-for-gateway-target)
105
- - [Configuration Factory Methods](#configuration-factory-methods)
106
- - [Example: Lambda Target with Custom Configuration](#example-lambda-target-with-custom-configuration)
107
- - [Gateway Target IAM Permissions](#gateway-target-iam-permissions)
108
- - [Memory](#memory)
109
- - [Memory Properties](#memory-properties)
110
- - [Basic Memory Creation](#basic-memory-creation)
111
- - [LTM Memory Extraction Stategies](#ltm-memory-extraction-stategies)
112
- - [Memory with Built-in Strategies](#memory-with-built-in-strategies)
113
- - [Memory with custom Strategies](#memory-with-custom-strategies)
114
- - [Memory with Custom Execution Role](#memory-with-custom-execution-role)
115
- - [Memory with self-managed Strategies](#memory-with-self-managed-strategies)
116
- - [Memory Strategy Methods](#memory-strategy-methods)
117
- - [Policy](#policy)
118
- - [PolicyEngine Properties](#policyengine-properties)
119
- - [Policy Properties](#policy-properties)
120
- - [Basic PolicyEngine and Policy Creation](#basic-policyengine-and-policy-creation)
121
- - [Associating a Policy Engine with a Gateway](#associating-a-policy-engine-with-a-gateway)
122
- - [Type-Safe Policy Builder](#type-safe-policy-builder)
123
- - [PolicyEngine with KMS Encryption](#policyengine-with-kms-encryption)
124
- - [Policy Validation Modes](#policy-validation-modes)
125
- - [Online Evaluation](#online-evaluation)
126
- - [Online Evaluation Properties](#online-evaluation-properties)
127
- - [Basic Online Evaluation Creation](#basic-online-evaluation-creation)
128
- - [Built-in Evaluators](#built-in-evaluators)
129
- - [Custom Evaluators](#custom-evaluators)
130
- - [LLM-as-a-Judge Evaluator](#llm-as-a-judge-evaluator)
131
- - [Code-Based Evaluator](#code-based-evaluator)
132
- - [Using Custom Evaluators with Online Evaluation](#using-custom-evaluators-with-online-evaluation)
133
- - [Data Source Configuration](#data-source-configuration)
134
- - [Sampling and Filtering](#sampling-and-filtering)
135
- - [Online Evaluation with Custom Execution Role](#online-evaluation-with-custom-execution-role)
136
- - [Online Evaluation IAM Permissions](#online-evaluation-iam-permissions)
137
-
138
- ## AgentCore Runtime
139
-
140
- The AgentCore Runtime construct enables you to deploy containerized agents on Amazon Bedrock AgentCore.
141
- This L2 construct simplifies runtime creation just pass your ECR repository name
142
- and the construct handles all the configuration with sensible defaults.
143
-
144
- ### Runtime Endpoints
145
-
146
- Endpoints provide a stable way to invoke specific versions of your agent runtime, enabling controlled deployments across different environments.
147
- When you create an agent runtime, Amazon Bedrock AgentCore automatically creates a "DEFAULT" endpoint which always points to the latest version
148
- of runtime.
149
-
150
- You can create additional endpoints in two ways:
151
-
152
- 1. **Using Runtime.addEndpoint()** - Convenient method when creating endpoints alongside the runtime.
153
- 2. **Using RuntimeEndpoint** - Flexible approach for existing runtimes.
154
-
155
- For example, you might keep a "production" endpoint on a stable version while testing newer versions
156
- through a "staging" endpoint. This separation allows you to test changes thoroughly before promoting them
157
- to production by simply updating the endpoint to point to the newer version.
158
-
159
- ### AgentCore Runtime Properties
160
-
161
- | Name | Type | Required | Description |
162
- |------|------|----------|-------------|
163
- | `runtimeName` | `string` | No | The name of the agent runtime. Valid characters are a-z, A-Z, 0-9, _ (underscore). Must start with a letter and can be up to 48 characters long. If not provided, a unique name will be auto-generated |
164
- | `agentRuntimeArtifact` | `AgentRuntimeArtifact` | Yes | The artifact configuration for the agent runtime containing the container configuration with ECR URI |
165
- | `executionRole` | `iam.IRole` | No | The IAM role that provides permissions for the agent runtime. If not provided, a role will be created automatically |
166
- | `networkConfiguration` | `NetworkConfiguration` | No | Network configuration for the agent runtime. Defaults to `RuntimeNetworkConfiguration.usingPublicNetwork()` |
167
- | `description` | `string` | No | Optional description for the agent runtime |
168
- | `protocolConfiguration` | `ProtocolType` | No | Protocol configuration for the agent runtime. Defaults to `ProtocolType.HTTP` |
169
- | `authorizerConfiguration` | `RuntimeAuthorizerConfiguration` | No | Authorizer configuration for the agent runtime. Use `RuntimeAuthorizerConfiguration` static methods to create configurations for IAM, Cognito, JWT, or OAuth authentication |
170
- | `environmentVariables` | `{ [key: string]: string }` | No | Environment variables for the agent runtime. Maximum 50 environment variables |
171
- | `tags` | `{ [key: string]: string }` | No | Tags for the agent runtime. A list of key:value pairs of tags to apply to this Runtime resource |
172
- | `lifecycleConfiguration` | LifecycleConfiguration | No | The life cycle configuration for the AgentCore Runtime. Defaults to 900 seconds (15 minutes) for idle, 28800 seconds (8 hours) for max life time |
173
- | `requestHeaderConfiguration` | RequestHeaderConfiguration | No | Configuration for HTTP request headers that will be passed through to the runtime. Defaults to no configuration |
174
-
175
- ### Runtime Endpoint Properties
176
-
177
- | Name | Type | Required | Description |
178
- |------|------|----------|-------------|
179
- | `endpointName` | `string` | No | The name of the runtime endpoint. Valid characters are a-z, A-Z, 0-9, _ (underscore). Must start with a letter and can be up to 48 characters long. If not provided, a unique name will be auto-generated |
180
- | `agentRuntimeId` | `string` | Yes | The Agent Runtime ID for this endpoint |
181
- | `agentRuntimeVersion` | `string` | Yes | The Agent Runtime version for this endpoint. Must be between 1 and 5 characters long.|
182
- | `description` | `string` | No | Optional description for the runtime endpoint |
183
- | `tags` | `{ [key: string]: string }` | No | Tags for the runtime endpoint |
184
-
185
- ### Creating a Runtime
186
-
187
- #### Option 1: Use an existing image in ECR
188
-
189
- Reference an image available within ECR.
190
-
191
- ```typescript fixture=default
192
- const repository = new ecr.Repository(this, "TestRepository", {
193
- repositoryName: "test-agent-runtime",
194
- });
195
-
196
- // The runtime by default create ECR permission only for the repository available in the account the stack is being deployed
197
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
198
-
199
- // Create runtime using the built image
200
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
201
- runtimeName: "myAgent",
202
- agentRuntimeArtifact: agentRuntimeArtifact
203
- });
204
- ```
205
-
206
- #### Option 2: Use a local asset
207
-
208
- Reference a local directory containing a Dockerfile.
209
- Images are built from a local Docker context directory (with a Dockerfile), uploaded to Amazon Elastic Container Registry (ECR)
210
- by the CDK toolkit,and can be naturally referenced in your CDK app.
211
-
212
- ```typescript fixture=default
213
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromAsset(
214
- path.join(__dirname, "path to agent dockerfile directory")
215
- );
216
-
217
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
218
- runtimeName: "myAgent",
219
- agentRuntimeArtifact: agentRuntimeArtifact,
220
- });
221
- ```
222
-
223
- #### Option 3: Use direct code deployment
224
-
225
- With the container deployment method, developers create a Dockerfile, build ARM-compatible containers, manage ECR repositories, and upload containers for code changes. This works well where container DevOps pipelines have already been established to automate deployments.
226
-
227
- However, customers looking for fully managed deployments can benefit from direct code deployment, which can significantly improve developer time and productivity. Direct code deployment provides a secure and scalable path forward for rapid prototyping agent capabilities to deploying production workloads at scale.
228
-
229
- With direct code deployment, developers create a zip archive of code and dependencies, upload to Amazon S3, and configure the bucket in the agent configuration. A ZIP archive containing Linux arm64 dependencies needs to be uploaded to S3 as a pre-requisite to Create Agent Runtime.
230
-
231
- For more information, please refer to the [documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-get-started-code-deploy.html).
232
-
233
- ```typescript fixture=default
234
- // S3 bucket containing the agent core
235
- const codeBucket = new s3.Bucket(this, "AgentCode", {
236
- bucketName: "my-code-bucket",
237
- removalPolicy: RemovalPolicy.DESTROY, // For demo purposes
238
- });
239
-
240
- // the bucket above needs to contain the agent code
241
-
242
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromS3(
243
- {
244
- bucketName: codeBucket.bucketName,
245
- objectKey: 'deployment_package.zip',
246
- },
247
- agentcore.AgentCoreRuntime.PYTHON_3_12,
248
- ['opentelemetry-instrument', 'main.py']
249
- );
250
-
251
- const runtimeInstance = new agentcore.Runtime(this, "MyAgentRuntime", {
252
- runtimeName: "myAgent",
253
- agentRuntimeArtifact: agentRuntimeArtifact,
254
- });
255
- ```
256
-
257
- Alternatively, you can use local code assets that will be automatically packaged and uploaded to a CDK-managed S3 bucket:
258
-
259
- ```typescript fixture=default
260
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromCodeAsset({
261
- path: path.join(__dirname, 'path/to/agent/code'),
262
- runtime: agentcore.AgentCoreRuntime.PYTHON_3_12,
263
- entrypoint: ['opentelemetry-instrument', 'main.py'],
264
- });
265
-
266
- const runtimeInstance = new agentcore.Runtime(this, "MyAgentRuntime", {
267
- runtimeName: "myAgent",
268
- agentRuntimeArtifact: agentRuntimeArtifact,
269
- });
270
- ```
271
-
272
- #### Option 4: Use an ECR container image URI
273
-
274
- Reference an ECR container image directly by its URI. This is useful when you have a pre-existing ECR image URI from CloudFormation parameters or cross-stack references. No IAM permissions are automatically granted - you must ensure the runtime has ECR pull permissions.
275
-
276
- ```typescript fixture=default
277
- // Direct URI reference
278
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromImageUri(
279
- "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-agent:v1.0.0"
280
- );
281
-
282
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
283
- runtimeName: "myAgent",
284
- agentRuntimeArtifact: agentRuntimeArtifact,
285
- });
286
- ```
287
-
288
- You can also use CloudFormation parameters or references:
289
-
290
- ```typescript fixture=default
291
- // Using a CloudFormation parameter
292
- const imageUriParam = new cdk.CfnParameter(this, "ImageUri", {
293
- type: "String",
294
- description: "Container image URI for the agent runtime",
295
- });
296
-
297
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromImageUri(
298
- imageUriParam.valueAsString
299
- );
300
-
301
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
302
- runtimeName: "myAgent",
303
- agentRuntimeArtifact: agentRuntimeArtifact,
304
- });
305
- ```
306
-
307
- ### Granting Permissions to Invoke Bedrock Models or Inference Profiles
308
-
309
- To grant the runtime permissions to invoke Bedrock models or inference profiles:
310
-
311
- ```typescript fixture=default
312
- // Note: This example uses @aws-cdk/aws-bedrock-alpha which must be installed separately
313
- declare const runtime: agentcore.Runtime;
314
-
315
- // Define the Bedrock Foundation Model
316
- const model = bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_7_SONNET_V1_0;
317
-
318
- // Grant the runtime permissions to invoke the model
319
- model.grantInvoke(runtime);
320
-
321
- // Create a cross-region inference profile for Claude 3.7 Sonnet
322
- const inferenceProfile = bedrock.CrossRegionInferenceProfile.fromConfig({
323
- geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
324
- model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_7_SONNET_V1_0
325
- });
326
-
327
- // Grant the runtime permissions to invoke the inference profile
328
- inferenceProfile.grantInvoke(runtime);
329
- ```
330
-
331
- ### Runtime Versioning
332
-
333
- Amazon Bedrock AgentCore automatically manages runtime versioning to ensure safe deployments and rollback capabilities.
334
- When you create an agent runtime, AgentCore automatically creates version 1 (V1). Each subsequent update to the
335
- runtime configuration (such as updating the container image, modifying network settings, or changing protocol configurations)
336
- creates a new immutable version. These versions contain complete, self-contained configurations that can be referenced by endpoints,
337
- allowing you to maintain different versions for different environments or gradually roll out updates.
338
-
339
- #### Managing Endpoints and Versions
340
-
341
- Amazon Bedrock AgentCore automatically manages runtime versioning to provide safe deployments and rollback capabilities. You can follow
342
- the steps below to understand how to use versioning with runtime for controlled deployments across different environments.
343
-
344
- ##### Step 1: Initial Deployment
345
-
346
- When you first create an agent runtime, AgentCore automatically creates Version 1 of your runtime. At this point, a DEFAULT endpoint is
347
- automatically created that points to Version 1. This DEFAULT endpoint serves as the main access point for your runtime.
348
-
349
- ```typescript fixture=default
350
- const repository = new ecr.Repository(this, "TestRepository", {
351
- repositoryName: "test-agent-runtime",
352
- });
353
-
354
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
355
- runtimeName: "myAgent",
356
- agentRuntimeArtifact: agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"),
357
- });
358
- ```
359
-
360
- ##### Step 2: Creating Custom Endpoints
361
-
362
- After the initial deployment, you can create additional endpoints for different environments. For example, you might create a "production"
363
- endpoint that explicitly points to Version 1. This allows you to maintain stable access points for specific environments while keeping the
364
- flexibility to test newer versions elsewhere.
365
-
366
- ```typescript fixture=default
367
- const repository = new ecr.Repository(this, "TestRepository", {
368
- repositoryName: "test-agent-runtime",
369
- });
370
-
371
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
372
- runtimeName: "myAgent",
373
- agentRuntimeArtifact: agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"),
374
- });
375
-
376
- const prodEndpoint = runtime.addEndpoint("production", {
377
- version: "1",
378
- description: "Stable production endpoint - pinned to v1"
379
- });
380
- ```
381
-
382
- ##### Step 3: Runtime Update Deployment
383
-
384
- When you update the runtime configuration (such as updating the container image, modifying network settings, or changing protocol
385
- configurations), AgentCore automatically creates a new version (Version 2). Upon this update:
386
-
387
- - Version 2 is created automatically with the new configuration
388
- - The DEFAULT endpoint automatically updates to point to Version 2
389
- - Any explicitly pinned endpoints (like the production endpoint) remain on their specified versions
390
-
391
- ```typescript fixture=default
392
- const repository = new ecr.Repository(this, "TestRepository", {
393
- repositoryName: "test-agent-runtime",
394
- });
395
-
396
- const agentRuntimeArtifactNew = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v2.0.0");
397
-
398
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
399
- runtimeName: "myAgent",
400
- agentRuntimeArtifact: agentRuntimeArtifactNew,
401
- });
402
- ```
403
-
404
- ##### Step 4: Testing with Staging Endpoints
405
-
406
- Once Version 2 exists, you can create a staging endpoint that points to the new version. This staging endpoint allows you to test the
407
- new version in a controlled environment before promoting it to production. This separation ensures that production traffic continues
408
- to use the stable version while you validate the new version.
409
-
410
- ```typescript fixture=default
411
- const repository = new ecr.Repository(this, "TestRepository", {
412
- repositoryName: "test-agent-runtime",
413
- });
414
-
415
- const agentRuntimeArtifactNew = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v2.0.0");
416
-
417
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
418
- runtimeName: "myAgent",
419
- agentRuntimeArtifact: agentRuntimeArtifactNew,
420
- });
421
-
422
- const stagingEndpoint = runtime.addEndpoint("staging", {
423
- version: "2",
424
- description: "Staging environment for testing new version"
425
- });
426
- ```
427
-
428
- ##### Step 5: Promoting to Production
429
-
430
- After thoroughly testing the new version through the staging endpoint, you can update the production endpoint to point to Version 2.
431
- This controlled promotion process ensures that you can validate changes before they affect production traffic.
432
-
433
- ```typescript fixture=default
434
- const repository = new ecr.Repository(this, "TestRepository", {
435
- repositoryName: "test-agent-runtime",
436
- });
437
-
438
- const agentRuntimeArtifactNew = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v2.0.0");
439
-
440
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
441
- runtimeName: "myAgent",
442
- agentRuntimeArtifact: agentRuntimeArtifactNew,
443
- });
444
-
445
- const prodEndpoint = runtime.addEndpoint("production", {
446
- version: "2", // New version added here
447
- description: "Stable production endpoint"
448
- });
449
- ```
450
-
451
- ### Creating Standalone Runtime Endpoints
452
-
453
- RuntimeEndpoint can also be created as a standalone resource.
454
-
455
- #### Example: Creating an endpoint for an existing runtime
456
-
457
- ```typescript fixture=default
458
- // Reference an existing runtime by its ID
459
- const existingRuntimeId = "abc123-runtime-id"; // The ID of an existing runtime
460
-
461
- // Create a standalone endpoint
462
- const endpoint = new agentcore.RuntimeEndpoint(this, "MyEndpoint", {
463
- endpointName: "production",
464
- agentRuntimeId: existingRuntimeId,
465
- agentRuntimeVersion: "1", // Specify which version to use
466
- description: "Production endpoint for existing runtime"
467
- });
468
- ```
469
-
470
- ### Runtime Authentication Configuration
471
-
472
- The AgentCore Runtime supports multiple authentication modes to secure access to your agent endpoints. Authentication is configured during runtime creation using the `RuntimeAuthorizerConfiguration` class's static factory methods.
473
-
474
- #### IAM Authentication (Default)
475
-
476
- IAM authentication is the default mode, when no authorizerConfiguration is set then the underlying service use IAM.
477
-
478
- #### Cognito Authentication
479
-
480
- To configure AWS Cognito User Pool authentication:
481
-
482
- ```typescript fixture=default
483
- declare const userPool: cognito.UserPool;
484
- declare const userPoolClient: cognito.UserPoolClient;
485
- declare const anotherUserPoolClient: cognito.UserPoolClient;
486
-
487
- const repository = new ecr.Repository(this, "TestRepository", {
488
- repositoryName: "test-agent-runtime",
489
- });
490
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
491
-
492
- // Optional: Create custom claims for additional validation
493
- const customClaims = [
494
- agentcore.RuntimeCustomClaim.withStringValue('department', 'engineering'),
495
- agentcore.RuntimeCustomClaim.withStringArrayValue('roles', ['admin'], agentcore.CustomClaimOperator.CONTAINS),
496
- agentcore.RuntimeCustomClaim.withStringArrayValue('permissions', ['read', 'write'], agentcore.CustomClaimOperator.CONTAINS_ANY),
497
- ];
498
-
499
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
500
- runtimeName: "myAgent",
501
- agentRuntimeArtifact: agentRuntimeArtifact,
502
- authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingCognito(
503
- userPool, // User Pool (required)
504
- [userPoolClient, anotherUserPoolClient], // User Pool Clients
505
- ["audience1"], // Allowed Audiences (optional)
506
- ["read", "write"], // Allowed Scopes (optional)
507
- customClaims, // Custom claims (optional) - see Custom Claims Validation section
508
- ),
509
- });
510
- ```
511
-
512
- You can configure:
513
-
514
- - User Pool: The Cognito User Pool that issues JWT tokens
515
- - User Pool Clients: One or more Cognito User Pool App Clients that are allowed to access the runtime
516
- - Allowed audiences: Used to validate that the audiences specified in the Cognito token match or are a subset of the audiences specified in the AgentCore Runtime
517
- - Allowed scopes: Allow access only if the token contains at least one of the required scopes configured here
518
- - Custom claims: A set of rules to match specific claims in the incoming token against predefined values for validating JWT tokens
519
-
520
- #### JWT Authentication
521
-
522
- To configure custom JWT authentication with your own OpenID Connect (OIDC) provider:
523
-
524
- ```typescript fixture=default
525
- const repository = new ecr.Repository(this, "TestRepository", {
526
- repositoryName: "test-agent-runtime",
527
- });
528
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
529
-
530
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
531
- runtimeName: "myAgent",
532
- agentRuntimeArtifact: agentRuntimeArtifact,
533
- authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingJWT(
534
- "https://example.com/.well-known/openid-configuration", // Discovery URL (required)
535
- ["client1", "client2"], // Allowed Client IDs (optional)
536
- ["audience1"], // Allowed Audiences (optional)
537
- ["read", "write"], // Allowed Scopes (optional)
538
- // Custom claims (optional) - see Custom Claims Validation section below
539
- ),
540
- });
541
- ```
542
-
543
- You can configure:
544
-
545
- - Discovery URL: Enter the Discovery URL from your identity provider (e.g. Okta, Cognito, etc.), typically found in that provider's documentation. This allows your Agent or Tool to fetch login, downstream resource token, and verification settings.
546
- - Allowed audiences: This is used to validate that the audiences specified for the OAuth token matches or are a subset of the audiences specified in the AgentCore Runtime.
547
- - Allowed clients: This is used to validate that the public identifier of the client, as specified in the authorization token, is allowed to access the AgentCore Runtime.
548
- - Allowed scopes: Allow access only if the token contains at least one of the required scopes configured here.
549
- - Custom claims: A set of rules to match specific claims in the incoming token against predefined values for validating JWT tokens.
550
-
551
- **Note**: The discovery URL must end with `/.well-known/openid-configuration`.
552
-
553
- ##### Custom Claims Validation
554
-
555
- Custom claims allow you to validate additional fields in JWT tokens beyond the standard audience, client, and scope validations. You can create custom claims using the `RuntimeCustomClaim` class:
556
-
557
- ```typescript fixture=default
558
- const repository = new ecr.Repository(this, "TestRepository", {
559
- repositoryName: "test-agent-runtime",
560
- });
561
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
562
-
563
- // String claim - validates that the claim exactly equals the specified value
564
- // Uses EQUALS operator automatically
565
- const departmentClaim = agentcore.RuntimeCustomClaim.withStringValue('department', 'engineering');
566
-
567
- // String array claim with CONTAINS operator (default)
568
- // Validates that the claim array contains a specific string value
569
- // IMPORTANT: CONTAINS requires exactly one value in the array parameter
570
- const rolesClaim = agentcore.RuntimeCustomClaim.withStringArrayValue('roles', ['admin']);
571
-
572
- // String array claim with CONTAINS_ANY operator
573
- // Validates that the claim array contains at least one of the specified values
574
- // Use this when you want to check for multiple possible values
575
- const permissionsClaim = agentcore.RuntimeCustomClaim.withStringArrayValue(
576
- 'permissions',
577
- ['read', 'write'],
578
- agentcore.CustomClaimOperator.CONTAINS_ANY
579
- );
580
-
581
- // Use custom claims in authorizer configuration
582
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
583
- runtimeName: "myAgent",
584
- agentRuntimeArtifact: agentRuntimeArtifact,
585
- authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingJWT(
586
- "https://example.com/.well-known/openid-configuration",
587
- ["client1", "client2"],
588
- ["audience1"],
589
- ["read", "write"],
590
- [departmentClaim, rolesClaim, permissionsClaim] // Custom claims
591
- ),
592
- });
593
- ```
594
-
595
- **Custom Claim Rules**:
596
-
597
- - **String claims**: Must use the `EQUALS` operator (automatically set). The claim value must exactly match the specified string.
598
- - **String array claims**: Can use `CONTAINS` (default) or `CONTAINS_ANY` operators:
599
- - **`CONTAINS`**: Checks if the claim array contains a specific string value. **Requires exactly one value** in the array parameter. For example, `['admin']` will check if the token's claim array contains the string `'admin'`.
600
- - **`CONTAINS_ANY`**: Checks if the claim array contains at least one of the provided string values. Use this when you want to validate against multiple possible values. For example, `['read', 'write']` will check if the token's claim array contains either `'read'` or `'write'`.
601
-
602
- **Example Use Cases**:
603
-
604
- - Use `CONTAINS` when you need to verify a user has a specific role: `RuntimeCustomClaim.withStringArrayValue('roles', ['admin'])`
605
- - Use `CONTAINS_ANY` when you need to verify a user has any of several permissions: `RuntimeCustomClaim.withStringArrayValue('permissions', ['read', 'write'], CustomClaimOperator.CONTAINS_ANY)`
606
-
607
- #### OAuth Authentication
608
-
609
- To configure OAuth 2.0 authentication:
610
-
611
- ```typescript fixture=default
612
- const repository = new ecr.Repository(this, "TestRepository", {
613
- repositoryName: "test-agent-runtime",
614
- });
615
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
616
-
617
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
618
- runtimeName: "myAgent",
619
- agentRuntimeArtifact: agentRuntimeArtifact,
620
- authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingOAuth(
621
- "https://github.com/.well-known/openid-configuration", // Discovery URL (required)
622
- "oauth_client_123", // OAuth Client ID (required)
623
- ["audience1"], // Allowed Audiences (optional)
624
- ["openid", "profile"], // Allowed Scopes (optional)
625
- // Custom claims (optional) - see Custom Claims Validation section
626
- ),
627
- });
628
- ```
629
-
630
- #### Using a Custom IAM Role
631
-
632
- Instead of using the auto-created execution role, you can provide your own IAM role with specific permissions:
633
- The auto-created role includes all necessary baseline permissions for ECR access, CloudWatch logging, and X-Ray tracing. When providing a custom role, ensure these permissions are included.
634
-
635
- ### Runtime Network Configuration
636
-
637
- The AgentCore Runtime supports two network modes for deployment:
638
-
639
- #### Public Network Mode (Default)
640
-
641
- By default, runtimes are deployed in PUBLIC network mode, which provides internet access suitable for less sensitive or open-use scenarios:
642
-
643
- ```typescript fixture=default
644
- const repository = new ecr.Repository(this, "TestRepository", {
645
- repositoryName: "test-agent-runtime",
646
- });
647
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
648
-
649
- // Explicitly using public network (this is the default)
650
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
651
- runtimeName: "myAgent",
652
- agentRuntimeArtifact: agentRuntimeArtifact,
653
- networkConfiguration: agentcore.RuntimeNetworkConfiguration.usingPublicNetwork(),
654
- });
655
- ```
656
-
657
- #### VPC Network Mode
658
-
659
- For enhanced security and network isolation, you can deploy your runtime within a VPC:
660
-
661
- ```typescript fixture=default
662
- const repository = new ecr.Repository(this, "TestRepository", {
663
- repositoryName: "test-agent-runtime",
664
- });
665
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
666
-
667
- // Create or use an existing VPC
668
- const vpc = new ec2.Vpc(this, 'MyVpc', {
669
- maxAzs: 2,
670
- });
671
-
672
- // Configure runtime with VPC
673
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
674
- runtimeName: "myAgent",
675
- agentRuntimeArtifact: agentRuntimeArtifact,
676
- networkConfiguration: agentcore.RuntimeNetworkConfiguration.usingVpc(this, {
677
- vpc: vpc,
678
- vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
679
- // Optionally specify security groups, or one will be created automatically
680
- // securityGroups: [mySecurityGroup],
681
- }),
682
- });
683
-
684
- ```
685
-
686
- #### Managing Security Groups with VPC Configuration
687
-
688
- When using VPC mode, the Runtime implements `ec2.IConnectable`, allowing you to manage network access using the `connections` property:
689
-
690
- ```typescript fixture=default
691
- const vpc = new ec2.Vpc(this, 'MyVpc', {
692
- maxAzs: 2,
693
- });
694
-
695
- const repository = new ecr.Repository(this, "TestRepository", {
696
- repositoryName: "test-agent-runtime",
697
- });
698
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
699
-
700
- // Create runtime with VPC configuration
701
- const runtime = new agentcore.Runtime(this, "MyAgentRuntime", {
702
- runtimeName: "myAgent",
703
- agentRuntimeArtifact: agentRuntimeArtifact,
704
- networkConfiguration: agentcore.RuntimeNetworkConfiguration.usingVpc(this, {
705
- vpc: vpc,
706
- vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS },
707
- }),
708
- });
709
-
710
- // Now you can manage network access using the connections property
711
- // Allow inbound HTTPS traffic from a specific security group
712
- const webServerSecurityGroup = new ec2.SecurityGroup(this, 'WebServerSG', { vpc });
713
- runtime.connections.allowFrom(webServerSecurityGroup, ec2.Port.tcp(443), 'Allow HTTPS from web servers');
714
-
715
- // Allow outbound connections to a database
716
- const databaseSecurityGroup = new ec2.SecurityGroup(this, 'DatabaseSG', { vpc });
717
- runtime.connections.allowTo(databaseSecurityGroup, ec2.Port.tcp(5432), 'Allow PostgreSQL connection');
718
-
719
- // Allow outbound HTTPS to anywhere (for external API calls)
720
- runtime.connections.allowToAnyIpv4(ec2.Port.tcp(443), 'Allow HTTPS outbound');
721
- ```
722
-
723
- ### Runtime IAM Permissions
724
-
725
- The Runtime construct provides convenient methods for granting IAM permissions to principals that need to invoke the runtime or manage its execution role.
726
-
727
- ```typescript fixture=default
728
- const repository = new ecr.Repository(this, "TestRepository", {
729
- repositoryName: "test-agent-runtime",
730
- });
731
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
732
-
733
- // Create a runtime
734
- const runtime = new agentcore.Runtime(this, "MyRuntime", {
735
- runtimeName: "my_runtime",
736
- agentRuntimeArtifact: agentRuntimeArtifact,
737
- });
738
-
739
- // Create a Lambda function that needs to invoke the runtime
740
- const invokerFunction = new lambda.Function(this, "InvokerFunction", {
741
- runtime: lambda.Runtime.PYTHON_3_12,
742
- handler: "index.handler",
743
- code: lambda.Code.fromInline(`
744
- import boto3
745
- def handler(event, context):
746
- client = boto3.client('bedrock-agentcore')
747
- # Invoke the runtime...
748
- `),
749
- });
750
-
751
- // Grant permission to invoke the runtime directly
752
- runtime.grantInvokeRuntime(invokerFunction);
753
-
754
- // Grant permission to invoke the runtime on behalf of a user
755
- // (requires X-Amzn-Bedrock-AgentCore-Runtime-User-Id header)
756
- runtime.grantInvokeRuntimeForUser(invokerFunction);
757
-
758
- // Grant both invoke permissions (most common use case)
759
- runtime.grantInvoke(invokerFunction);
760
-
761
- // Grant specific custom permissions to the runtime's execution role
762
- runtime.grant(['bedrock:InvokeModel'], ['arn:aws:bedrock:*:*:*']);
763
-
764
- // Add a policy statement to the runtime's execution role
765
- runtime.addToRolePolicy(new iam.PolicyStatement({
766
- actions: ['s3:GetObject'],
767
- resources: ['arn:aws:s3:::my-bucket/*'],
768
- }));
769
- ```
770
-
771
- ### Other configuration
772
-
773
- #### Lifecycle configuration
774
-
775
- The LifecycleConfiguration input parameter to CreateAgentRuntime lets you manage the lifecycle of runtime sessions and resources in Amazon Bedrock AgentCore Runtime. This configuration helps optimize resource utilization by automatically cleaning up idle sessions and preventing long-running instances from consuming resources indefinitely.
776
-
777
- You can configure:
778
-
779
- - idleRuntimeSessionTimeout: Timeout in seconds for idle runtime sessions. When a session remains idle for this duration, it will trigger termination. Termination can last up to 15 seconds due to logging and other process completion. Default: 900 seconds (15 minutes)
780
- - maxLifetime: Maximum lifetime for the instance in seconds. Once reached, instances will initialize termination. Termination can last up to 15 seconds due to logging and other process completion. Default: 28800 seconds (8 hours)
781
-
782
- For additional information, please refer to the [documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-lifecycle-settings.html).
783
-
784
- ```typescript fixture=default
785
- const repository = new ecr.Repository(this, "TestRepository", {
786
- repositoryName: "test-agent-runtime",
787
- });
788
-
789
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
790
-
791
- new agentcore.Runtime(this, 'test-runtime', {
792
- runtimeName: 'test_runtime',
793
- agentRuntimeArtifact: agentRuntimeArtifact,
794
- lifecycleConfiguration: {
795
- idleRuntimeSessionTimeout: Duration.minutes(10),
796
- maxLifetime: Duration.hours(4),
797
- },
798
- });
799
- ```
800
-
801
- #### Request header configuration
802
-
803
- Custom headers let you pass contextual information from your application directly to your agent code without cluttering the main request payload. This includes authentication tokens like JWT (JSON Web Tokens, which contain user identity and authorization claims) through the Authorization header, allowing your agent to make decisions based on who is calling it. You can also pass custom metadata like user preferences, session identifiers, or trace context using headers prefixed with X-Amzn-Bedrock-AgentCore-Runtime-Custom-, giving your agent access to up to 20 pieces of runtime context that travel alongside each request. This information can be also used in downstream systems like AgentCore Memory that you can namespace based on those characteristics like user_id or aud in claims like line of business.
804
-
805
- For additional information, please refer to the [documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-header-allowlist.html).
806
-
807
- ```typescript fixture=default
808
- const repository = new ecr.Repository(this, "TestRepository", {
809
- repositoryName: "test-agent-runtime",
810
- });
811
-
812
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0");
813
-
814
- new agentcore.Runtime(this, 'test-runtime', {
815
- runtimeName: 'test_runtime',
816
- agentRuntimeArtifact: agentRuntimeArtifact,
817
- requestHeaderConfiguration: {
818
- allowlistedHeaders: ['X-Amzn-Bedrock-AgentCore-Runtime-Custom-H1'],
819
- },
820
- });
821
- ```
822
-
823
- #### Observability configuration
824
-
825
- The Runtime construct supports observability features including X-Ray tracing and logging to CloudWatch Logs, S3, or Kinesis Data Firehose. This allows you to monitor and debug your agent runtime invocations.
826
-
827
- You can configure:
828
-
829
- - tracingEnabled: Enable X-Ray tracing for the runtime
830
- - loggingConfigs: Send APPLICATION_LOGS (agent runtime invocations) and USAGE_LOGS (session-level resource consumption) to CloudWatch Logs, S3, or Kinesis Data Firehose
831
-
832
- For additional information, please refer to the [Set up logging and tracing for AgentCore](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/observability.html).
833
-
834
- ```typescript fixture=default
835
- const repository = new ecr.Repository(this, 'TestRepository', {
836
- repositoryName: 'test-agent-runtime',
837
- });
838
-
839
- const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, 'v1.0.0');
840
-
841
- // Create logging destinations
842
- const logGroup = new logs.LogGroup(this, 'RuntimeLogGroup');
843
- const logBucket = new s3.Bucket(this, 'RuntimeLogBucket');
844
- const firehoseStream = new firehose.DeliveryStream(this, 'RuntimeLogStream', {
845
- destination: new firehose.S3Bucket(logBucket),
846
- });
847
-
848
- new agentcore.Runtime(this, 'test-runtime', {
849
- runtimeName: 'test_runtime',
850
- agentRuntimeArtifact: agentRuntimeArtifact,
851
- tracingEnabled: true,
852
- loggingConfigs: [
853
- {
854
- logType: agentcore.LogType.APPLICATION_LOGS,
855
- destination: agentcore.LoggingDestination.cloudWatchLogs(logGroup),
856
- },
857
- {
858
- logType: agentcore.LogType.APPLICATION_LOGS,
859
- destination: agentcore.LoggingDestination.s3(logBucket),
860
- },
861
- {
862
- logType: agentcore.LogType.APPLICATION_LOGS,
863
- destination: agentcore.LoggingDestination.firehose(firehoseStream),
864
- },
865
- ],
866
- });
867
- ```
868
-
869
- ## Browser
870
-
871
- The Amazon Bedrock AgentCore Browser provides a secure, cloud-based browser that enables AI agents to interact with websites. It includes security features such as session isolation, built-in observability through live viewing, CloudTrail logging, and session replay capabilities.
872
-
873
- Additional information about the browser tool can be found in the [official documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/browser-tool.html)
874
-
875
- ### Browser Network modes
876
-
877
- The Browser construct supports the following network modes:
878
-
879
- 1. **Public Network Mode** (`BrowserNetworkMode.usingPublicNetwork()`) - Default
880
-
881
- - Allows internet access for web browsing and external API calls
882
- - Suitable for scenarios where agents need to interact with publicly available websites
883
- - Enables full web browsing capabilities
884
- - VPC mode is not supported with this option
885
-
886
- 2. **VPC (Virtual Private Cloud)** (`BrowserNetworkMode.usingVpc()`)
887
-
888
- - Select whether to run the browser in a virtual private cloud (VPC).
889
- - By configuring VPC connectivity, you enable secure access to private resources such as databases, internal APIs, and services within your VPC.
890
-
891
- While the VPC itself is mandatory, these are optional:
892
- - Subnets - if not provided, CDK will select appropriate subnets from the VPC
893
- - Security Groups - if not provided, CDK will create a default security group
894
- - Specific subnet selection criteria - you can let CDK choose automatically
895
-
896
- For more information on VPC connectivity for Amazon Bedrock AgentCore Browser, please refer to the [official documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/agentcore-vpc.html).
897
-
898
- ### Browser Properties
899
-
900
- | Name | Type | Required | Description |
901
- |------|------|----------|-------------|
902
- | `browserCustomName` | `string` | No | The name of the browser. Must start with a letter and can be up to 48 characters long. Pattern: `[a-zA-Z][a-zA-Z0-9_]{0,47}`. If not provided, a unique name will be auto-generated |
903
- | `description` | `string` | No | Optional description for the browser. Can have up to 200 characters |
904
- | `networkConfiguration` | `BrowserNetworkConfiguration` | No | Network configuration for browser. Defaults to PUBLIC network mode |
905
- | `recordingConfig` | `RecordingConfig` | No | Recording configuration for browser. Defaults to no recording |
906
- | `executionRole` | `iam.IRole` | No | The IAM role that provides permissions for the browser to access AWS services. A new role will be created if not provided |
907
- | `tags` | `{ [key: string]: string }` | No | Tags to apply to the browser resource |
908
- | `browserSigning` | BrowserSigning | No | Browser signing configuration. Defaults to DISABLED |
909
-
910
- ### Basic Browser Creation
911
-
912
- ```typescript fixture=default
913
- // Create a basic browser with public network access
914
- const browser = new agentcore.BrowserCustom(this, "MyBrowser", {
915
- browserCustomName: "my_browser",
916
- description: "A browser for web automation",
917
- });
918
- ```
919
-
920
- ### Browser with Tags
921
-
922
- ```typescript fixture=default
923
- // Create a browser with custom tags
924
- const browser = new agentcore.BrowserCustom(this, "MyBrowser", {
925
- browserCustomName: "my_browser",
926
- description: "A browser for web automation with tags",
927
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(),
928
- tags: {
929
- Environment: "Production",
930
- Team: "AI/ML",
931
- Project: "AgentCore",
932
- },
933
- });
934
- ```
935
-
936
- ### Browser with VPC
937
-
938
- ```typescript fixture=default
939
- const browser = new agentcore.BrowserCustom(this, 'BrowserVpcWithRecording', {
940
- browserCustomName: 'browser_recording',
941
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingVpc(this, {
942
- vpc: new ec2.Vpc(this, 'VPC', { restrictDefaultSecurityGroup: false }),
943
- }),
944
- });
945
- ```
946
-
947
- Browser exposes a [connections](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Connections.html) property. This property returns a connections object, which simplifies the process of defining and managing ingress and egress rules for security groups in your AWS CDK applications. Instead of directly manipulating security group rules, you interact with the Connections object of a construct, which then translates your connectivity requirements into the appropriate security group rules. For instance:
948
-
949
- ```typescript fixture=default
950
- const vpc = new ec2.Vpc(this, 'testVPC');
951
-
952
- const browser = new agentcore.BrowserCustom(this, 'test-browser', {
953
- browserCustomName: 'test_browser',
954
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingVpc(this, {
955
- vpc: vpc,
956
- }),
957
- });
958
-
959
- browser.connections.addSecurityGroup(new ec2.SecurityGroup(this, 'AdditionalGroup', { vpc }));
960
- ```
961
-
962
- So security groups can be added after the browser construct creation. You can use methods like allowFrom() and allowTo() to grant ingress access to/egress access from a specified peer over a given portRange. The Connections object automatically adds the necessary ingress or egress rules to the security group(s) associated with the calling construct.
963
-
964
- ### Browser with Recording Configuration
965
-
966
- ```typescript fixture=default
967
- // Create an S3 bucket for recordings
968
- const recordingBucket = new s3.Bucket(this, "RecordingBucket", {
969
- bucketName: "my-browser-recordings",
970
- removalPolicy: RemovalPolicy.DESTROY, // For demo purposes
971
- });
972
-
973
- // Create browser with recording enabled
974
- const browser = new agentcore.BrowserCustom(this, "MyBrowser", {
975
- browserCustomName: "my_browser",
976
- description: "Browser with recording enabled",
977
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(),
978
- recordingConfig: {
979
- enabled: true,
980
- s3Location: {
981
- bucketName: recordingBucket.bucketName,
982
- objectKey: "browser-recordings/",
983
- },
984
- },
985
- });
986
- ```
987
-
988
- ### Browser with Custom Execution Role
989
-
990
- ```typescript fixture=default
991
- // Create a custom execution role
992
- const executionRole = new iam.Role(this, "BrowserExecutionRole", {
993
- assumedBy: new iam.ServicePrincipal("bedrock-agentcore.amazonaws.com"),
994
- managedPolicies: [
995
- iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonBedrockAgentCoreBrowserExecutionRolePolicy"),
996
- ],
997
- });
998
-
999
- // Create browser with custom execution role
1000
- const browser = new agentcore.BrowserCustom(this, "MyBrowser", {
1001
- browserCustomName: "my_browser",
1002
- description: "Browser with custom execution role",
1003
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(),
1004
- executionRole: executionRole,
1005
- });
1006
- ```
1007
-
1008
- ### Browser with S3 Recording and Permissions
1009
-
1010
- ```typescript fixture=default
1011
- // Create an S3 bucket for recordings
1012
- const recordingBucket = new s3.Bucket(this, "RecordingBucket", {
1013
- bucketName: "my-browser-recordings",
1014
- removalPolicy: RemovalPolicy.DESTROY, // For demo purposes
1015
- });
1016
-
1017
- // Create browser with recording enabled
1018
- const browser = new agentcore.BrowserCustom(this, "MyBrowser", {
1019
- browserCustomName: "my_browser",
1020
- description: "Browser with recording enabled",
1021
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(),
1022
- recordingConfig: {
1023
- enabled: true,
1024
- s3Location: {
1025
- bucketName: recordingBucket.bucketName,
1026
- objectKey: "browser-recordings/",
1027
- },
1028
- },
1029
- });
1030
-
1031
- // The browser construct automatically grants S3 permissions to the execution role
1032
- // when recording is enabled, so no additional IAM configuration is needed
1033
- ```
1034
-
1035
- ### Browser with Browser signing
1036
-
1037
- AI agents need to browse the web on your behalf. When your agent visits a website to gather information, complete a form, or verify data, it encounters the same defenses designed to stop unwanted bots: CAPTCHAs, rate limits, and outright blocks.
1038
-
1039
- Amazon Bedrock AgentCore Browser supports Web Bot Auth. Web Bot Auth is a draft IETF protocol that gives agents verifiable cryptographic identities. When you enable Web Bot Auth in AgentCore Browser, the service issues cryptographic credentials that websites can verify. The agent presents these credentials with every request. The WAF may now additionally check the signature, confirm it matches a trusted directory, and allow the request through if verified bots are allowed by the domain owner and other WAF checks are clear.
1040
-
1041
- To enable the browser to sign requests using the Web Bot Auth protocol, create a browser tool with the browserSigning configuration:
1042
-
1043
- ```typescript fixture=default
1044
- const browser = new agentcore.BrowserCustom(this, 'test-browser', {
1045
- browserCustomName: 'test_browser',
1046
- browserSigning: agentcore.BrowserSigning.ENABLED
1047
- });
1048
- ```
1049
-
1050
- ### Browser IAM Permissions
1051
-
1052
- The Browser construct provides convenient methods for granting IAM permissions:
1053
-
1054
- ```typescript fixture=default
1055
- // Create a browser
1056
- const browser = new agentcore.BrowserCustom(this, "MyBrowser", {
1057
- browserCustomName: "my_browser",
1058
- description: "Browser for web automation",
1059
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(),
1060
- });
1061
-
1062
- // Create a role that needs access to the browser
1063
- const userRole = new iam.Role(this, "UserRole", {
1064
- assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
1065
- });
1066
-
1067
- // Grant read permissions (Get and List actions)
1068
- browser.grantRead(userRole);
1069
-
1070
- // Grant use permissions (Start, Update, Stop actions)
1071
- browser.grantUse(userRole);
1072
-
1073
- // Grant specific custom permissions
1074
- browser.grant(userRole, "bedrock-agentcore:GetBrowserSession");
1075
- ```
1076
-
1077
- ## Code Interpreter
1078
-
1079
- The Amazon Bedrock AgentCore Code Interpreter enables AI agents to write and execute code securely in sandbox environments, enhancing their accuracy and expanding their ability to solve complex end-to-end tasks. This is critical in Agentic AI applications where the agents may execute arbitrary code that can lead to data compromise or security risks. The AgentCore Code Interpreter tool provides secure code execution, which helps you avoid running into these issues.
1080
-
1081
- For more information about code interpreter, please refer to the [official documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/code-interpreter-tool.html)
1082
-
1083
- ### Code Interpreter Network Modes
1084
-
1085
- The Code Interpreter construct supports the following network modes:
1086
-
1087
- 1. **Public Network Mode** (`CodeInterpreterNetworkMode.usingPublicNetwork()`) - Default
1088
-
1089
- - Allows internet access for package installation and external API calls
1090
- - Suitable for development and testing environments
1091
- - Enables downloading Python packages from PyPI
1092
-
1093
- 2. **Sandbox Network Mode** (`CodeInterpreterNetworkMode.usingSandboxNetwork()`)
1094
- - Isolated network environment with no internet access
1095
- - Suitable for production environments with strict security requirements
1096
- - Only allows access to pre-installed packages and local resources
1097
-
1098
- 3. **VPC (Virtual Private Cloud)** (`CodeInterpreterNetworkMode.usingVpc()`)
1099
- - Select whether to run the browser in a virtual private cloud (VPC).
1100
- - By configuring VPC connectivity, you enable secure access to private resources such as databases, internal APIs, and services within your VPC.
1101
-
1102
- While the VPC itself is mandatory, these are optional:
1103
- - Subnets - if not provided, CDK will select appropriate subnets from the VPC
1104
- - Security Groups - if not provided, CDK will create a default security group
1105
- - Specific subnet selection criteria - you can let CDK choose automatically
1106
-
1107
- For more information on VPC connectivity for Amazon Bedrock AgentCore Browser, please refer to the [official documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/agentcore-vpc.html).
1108
-
1109
- ### Code Interpreter Properties
1110
-
1111
- | Name | Type | Required | Description |
1112
- |------|------|----------|-------------|
1113
- | `codeInterpreterCustomName` | `string` | No | The name of the code interpreter. Must start with a letter and can be up to 48 characters long. Pattern: `[a-zA-Z][a-zA-Z0-9_]{0,47}`. If not provided, a unique name will be auto-generated |
1114
- | `description` | `string` | No | Optional description for the code interpreter. Can have up to 200 characters |
1115
- | `executionRole` | `iam.IRole` | No | The IAM role that provides permissions for the code interpreter to access AWS services. A new role will be created if not provided |
1116
- | `networkConfiguration` | `CodeInterpreterNetworkConfiguration` | No | Network configuration for code interpreter. Defaults to PUBLIC network mode |
1117
- | `tags` | `{ [key: string]: string }` | No | Tags to apply to the code interpreter resource |
1118
-
1119
- ### Basic Code Interpreter Creation
1120
-
1121
- ```typescript fixture=default
1122
- // Create a basic code interpreter with public network access
1123
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1124
- codeInterpreterCustomName: "my_code_interpreter",
1125
- description: "A code interpreter for Python execution",
1126
- });
1127
- ```
1128
-
1129
- ### Code Interpreter with VPC
1130
-
1131
- ```typescript fixture=default
1132
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1133
- codeInterpreterCustomName: "my_sandbox_interpreter",
1134
- description: "Code interpreter with isolated network access",
1135
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingVpc(this, {
1136
- vpc: new ec2.Vpc(this, 'VPC', { restrictDefaultSecurityGroup: false }),
1137
- }),
1138
- });
1139
- ```
1140
-
1141
- Code Interpreter exposes a [connections](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.Connections.html) property. This property returns a connections object, which simplifies the process of defining and managing ingress and egress rules for security groups in your AWS CDK applications. Instead of directly manipulating security group rules, you interact with the Connections object of a construct, which then translates your connectivity requirements into the appropriate security group rules. For instance:
1142
-
1143
- ```typescript fixture=default
1144
- const vpc = new ec2.Vpc(this, 'testVPC');
1145
-
1146
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1147
- codeInterpreterCustomName: "my_sandbox_interpreter",
1148
- description: "Code interpreter with isolated network access",
1149
- networkConfiguration: agentcore.BrowserNetworkConfiguration.usingVpc(this, {
1150
- vpc: vpc,
1151
- }),
1152
- });
1153
-
1154
- codeInterpreter.connections.addSecurityGroup(new ec2.SecurityGroup(this, 'AdditionalGroup', { vpc }));
1155
- ```
1156
-
1157
- So security groups can be added after the browser construct creation. You can use methods like allowFrom() and allowTo() to grant ingress access to/egress access from a specified peer over a given portRange. The Connections object automatically adds the necessary ingress or egress rules to the security group(s) associated with the calling construct.
1158
-
1159
- ### Code Interpreter with Sandbox Network Mode
1160
-
1161
- ```typescript fixture=default
1162
- // Create code interpreter with sandbox network mode (isolated)
1163
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1164
- codeInterpreterCustomName: "my_sandbox_interpreter",
1165
- description: "Code interpreter with isolated network access",
1166
- networkConfiguration: agentcore.CodeInterpreterNetworkConfiguration.usingSandboxNetwork(),
1167
- });
1168
- ```
1169
-
1170
- ### Code Interpreter with Custom Execution Role
1171
-
1172
- ```typescript fixture=default
1173
- // Create a custom execution role
1174
- const executionRole = new iam.Role(this, "CodeInterpreterExecutionRole", {
1175
- assumedBy: new iam.ServicePrincipal("bedrock-agentcore.amazonaws.com"),
1176
- });
1177
-
1178
- // Create code interpreter with custom execution role
1179
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1180
- codeInterpreterCustomName: "my_code_interpreter",
1181
- description: "Code interpreter with custom execution role",
1182
- networkConfiguration: agentcore.CodeInterpreterNetworkConfiguration.usingPublicNetwork(),
1183
- executionRole: executionRole,
1184
- });
1185
- ```
1186
-
1187
- ### Code Interpreter IAM Permissions
1188
-
1189
- The Code Interpreter construct provides convenient methods for granting IAM permissions:
1190
-
1191
- ```typescript fixture=default
1192
- // Create a code interpreter
1193
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1194
- codeInterpreterCustomName: "my_code_interpreter",
1195
- description: "Code interpreter for Python execution",
1196
- networkConfiguration: agentcore.CodeInterpreterNetworkConfiguration.usingPublicNetwork(),
1197
- });
1198
-
1199
- // Create a role that needs access to the code interpreter
1200
- const userRole = new iam.Role(this, "UserRole", {
1201
- assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
1202
- });
1203
-
1204
- // Grant read permissions (Get and List actions)
1205
- codeInterpreter.grantRead(userRole);
1206
-
1207
- // Grant use permissions (Start, Invoke, Stop actions)
1208
- codeInterpreter.grantUse(userRole);
1209
-
1210
- // Grant specific custom permissions
1211
- codeInterpreter.grant(userRole, "bedrock-agentcore:GetCodeInterpreterSession");
1212
- ```
1213
-
1214
- ### Code interpreter with tags
1215
-
1216
- ```typescript fixture=default
1217
- // Create code interpreter with sandbox network mode (isolated)
1218
- const codeInterpreter = new agentcore.CodeInterpreterCustom(this, "MyCodeInterpreter", {
1219
- codeInterpreterCustomName: "my_sandbox_interpreter",
1220
- description: "Code interpreter with isolated network access",
1221
- networkConfiguration: agentcore.CodeInterpreterNetworkConfiguration.usingPublicNetwork(),
1222
- tags: {
1223
- Environment: "Production",
1224
- Team: "AI/ML",
1225
- Project: "AgentCore",
1226
- },
1227
- });
1228
- ```
1229
-
1230
- ## Gateway
1231
-
1232
- The Gateway construct provides a way to create Amazon Bedrock Agent Core Gateways, which serve as integration points between agents and external services.
1233
-
1234
- ### Gateway Properties
1235
-
1236
- | Name | Type | Required | Description |
1237
- |------|------|----------|-------------|
1238
- | `gatewayName` | `string` | No | The name of the gateway. Valid characters are a-z, A-Z, 0-9, _ (underscore) and - (hyphen). Maximum 100 characters. If not provided, a unique name will be auto-generated |
1239
- | `description` | `string` | No | Optional description for the gateway. Maximum 200 characters |
1240
- | `protocolConfiguration` | `IGatewayProtocolConfig` | No | The protocol configuration for the gateway. Defaults to MCP protocol |
1241
- | `authorizerConfiguration` | `IGatewayAuthorizerConfig` | No | The authorizer configuration for the gateway. Defaults to Cognito |
1242
- | `exceptionLevel` | `GatewayExceptionLevel` | No | The verbosity of exception messages. Use DEBUG mode to see granular exception messages |
1243
- | `kmsKey` | `kms.IKey` | No | The AWS KMS key used to encrypt data associated with the gateway |
1244
- | `role` | `iam.IRole` | No | The IAM role that provides permissions for the gateway to access AWS services. A new role will be created if not provided |
1245
- | `tags` | `{ [key: string]: string }` | No | Tags for the gateway. A list of key:value pairs of tags to apply to this Gateway resource |
1246
- | `policyEngineConfiguration` | `GatewayPolicyEngineConfig` | No | Associates a policy engine with this gateway. All agent requests are evaluated against the Cedar policies in the engine. The gateway role is automatically granted evaluate permissions. Default: no policy engine |
1247
-
1248
- ### Basic Gateway Creation
1249
-
1250
- The protocol configuration defaults to MCP and the inbound auth configuration uses Cognito (it is automatically created on your behalf).
1251
-
1252
- ```typescript fixture=default
1253
- // Create a basic gateway with default MCP protocol and Cognito authorizer
1254
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1255
- gatewayName: "my-gateway",
1256
- });
1257
- ```
1258
-
1259
- ### Protocol configuration
1260
-
1261
- Currently MCP is the only protocol available. To configure it, use the `protocol` property with `McpProtocolConfiguration`:
1262
-
1263
- - Instructions: Guidance for how to use the gateway with your tools
1264
- - Semantic search: Smart tool discovery that finds the right tools without typical limits. It improves accuracy by finding relevant tools based on context
1265
- - Supported versions: Which MCP protocol versions the gateway can use
1266
-
1267
- ```typescript fixture=default
1268
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1269
- gatewayName: "my-gateway",
1270
- protocolConfiguration: new agentcore.McpProtocolConfiguration({
1271
- instructions: "Use this gateway to connect to external MCP tools",
1272
- searchType: agentcore.McpGatewaySearchType.SEMANTIC,
1273
- supportedVersions: [agentcore.MCPProtocolVersion.MCP_2025_03_26],
1274
- }),
1275
- });
1276
- ```
1277
-
1278
- ### Inbound authorization
1279
-
1280
- Before you create your gateway, you must set up inbound authorization. Inbound authorization validates users who attempt to access targets through
1281
- your AgentCore gateway. By default, if not provided, the construct will create and configure Cognito as the default identity provider
1282
- (inbound Auth setup). AgentCore supports the following types of inbound authorization:
1283
-
1284
- **JSON Web Token (JWT)** – A secure and compact token used for authorization. After creating the JWT, you specify it as the authorization
1285
- configuration when you create the gateway. You can create a JWT with any of the identity providers at Provider setup and configuration.
1286
-
1287
- You can configure a custom authorization provider using the `authorizerConfiguration` property with `GatewayAuthorizer.usingCustomJwt()`.
1288
- You need to specify an OAuth discovery server and client IDs/audiences when you create the gateway. You can specify the following:
1289
-
1290
- - Discovery Url — String that must match the pattern ^.+/\.well-known/openid-configuration$ for OpenID Connect discovery URLs
1291
- - At least one of the below options depending on the chosen identity provider.
1292
- - Allowed audiences — List of allowed audiences for JWT tokens
1293
- - Allowed clients — List of allowed client identifiers
1294
- - Allowed scopes — List of allowed scopes for JWT tokens
1295
- - Custom claims — Optional custom claim validations (see Custom Claims Validation section below)
1296
-
1297
- ```typescript fixture=default
1298
-
1299
- // Optional: Create custom claims (CustomClaimOperator and GatewayCustomClaim from agentcore)
1300
- const customClaims = [
1301
- agentcore.GatewayCustomClaim.withStringValue('department', 'engineering'),
1302
- agentcore.GatewayCustomClaim.withStringArrayValue('roles', ['admin'], agentcore.CustomClaimOperator.CONTAINS),
1303
- agentcore.GatewayCustomClaim.withStringArrayValue('permissions', ['read', 'write'], agentcore.CustomClaimOperator.CONTAINS_ANY),
1304
- ];
1305
-
1306
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1307
- gatewayName: "my-gateway",
1308
- authorizerConfiguration: agentcore.GatewayAuthorizer.usingCustomJwt({
1309
- discoveryUrl: "https://auth.example.com/.well-known/openid-configuration",
1310
- allowedAudience: ["my-app"],
1311
- allowedClients: ["my-client-id"],
1312
- allowedScopes: ["read", "write"],
1313
- customClaims: customClaims, // Optional custom claims
1314
- }),
1315
- });
1316
- ```
1317
-
1318
- **IAM** – Authorizes through the credentials of the AWS IAM identity trying to access the gateway.
1319
-
1320
- ```typescript fixture=default
1321
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1322
- gatewayName: "my-gateway",
1323
- authorizerConfiguration: agentcore.GatewayAuthorizer.usingAwsIam(),
1324
- });
1325
-
1326
- // Grant access to a Lambda function's role
1327
- const lambdaRole = new iam.Role(this, "LambdaRole", {
1328
- assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
1329
- });
1330
-
1331
- // The Lambda needs permission to invoke the gateway
1332
- gateway.grantInvoke(lambdaRole);
1333
- ```
1334
-
1335
- **No Authorization** – Creates a gateway with no inbound authorization. This is useful for building public MCP servers,
1336
- or when you want to skip gateway-level authentication and enforce tool execution-level authentication using Gateway Interceptors.
1337
-
1338
- ```typescript fixture=default
1339
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1340
- gatewayName: "my-gateway",
1341
- authorizerConfiguration: agentcore.GatewayAuthorizer.withNoAuth(),
1342
- });
1343
- ```
1344
-
1345
- > **⚠️ Important:** Do not use No Authorization gateways for production workloads unless you have implemented all the security best practices. No Authorization gateways are most appropriate for testing and development purposes. See [Security Best Practices](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-inbound-auth.html#gateway-inbound-auth-none) for required compensating controls.
1346
-
1347
- For more information, see [No Authorization](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-inbound-auth.html#gateway-inbound-auth-none).
1348
-
1349
- **Cognito with M2M (Machine-to-Machine) Authentication (Default)** – When no authorizer is specified, the construct automatically creates a Cognito User Pool configured for OAuth 2.0 client credentials flow. This enables machine-to-machine authentication suitable for AI agents and service-to-service communication.
1350
-
1351
- For more information, see [Setting up Amazon Cognito for Gateway inbound authorization](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/identity-idp-cognito.html).
1352
-
1353
- ```typescript fixture=default
1354
- // Create a gateway with default Cognito M2M authorizer
1355
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1356
- gatewayName: "my-gateway",
1357
- });
1358
-
1359
- // Access the Cognito resources for authentication setup
1360
- const userPool = gateway.userPool;
1361
- const userPoolClient = gateway.userPoolClient;
1362
-
1363
- // Get the token endpoint URL and OAuth scopes for client credentials flow
1364
- const tokenEndpointUrl = gateway.tokenEndpointUrl;
1365
- const oauthScopes = gateway.oauthScopes;
1366
- // oauthScopes are in the format: ['{resourceServerId}/read', '{resourceServerId}/write']
1367
- ```
1368
-
1369
- **Using Cognito User Pool Explicitly with Custom Claims** – You can also use an existing Cognito User Pool with custom claims:
1370
-
1371
- ```typescript fixture=default
1372
- declare const userPool: cognito.UserPool;
1373
- declare const userPoolClient: cognito.UserPoolClient;
1374
-
1375
- // Optional: Create custom claims (CustomClaimOperator and GatewayCustomClaim from agentcore)
1376
- const customClaims = [
1377
- agentcore.GatewayCustomClaim.withStringValue('department', 'engineering'),
1378
- agentcore.GatewayCustomClaim.withStringArrayValue('roles', ['admin'], agentcore.CustomClaimOperator.CONTAINS),
1379
- agentcore.GatewayCustomClaim.withStringArrayValue('permissions', ['read', 'write'], agentcore.CustomClaimOperator.CONTAINS_ANY),
1380
- ];
1381
-
1382
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1383
- gatewayName: "my-gateway",
1384
- authorizerConfiguration: agentcore.GatewayAuthorizer.usingCognito({
1385
- userPool: userPool,
1386
- allowedClients: [userPoolClient],
1387
- allowedAudiences: ["audience1"],
1388
- allowedScopes: ["read", "write"],
1389
- customClaims: customClaims, // Optional custom claims
1390
- }),
1391
- });
1392
- ```
1393
-
1394
- To authenticate with the gateway, request an access token using the client credentials flow and use it to call Gateway endpoints. For more information about the token endpoint, see [The token issuer endpoint](https://docs.aws.amazon.com/cognito/latest/developerguide/token-endpoint.html).
1395
-
1396
- The following is an example of a token request using curl:
1397
-
1398
- ```bash
1399
- curl -X POST "${TOKEN_ENDPOINT_URL}" \
1400
- -H "Content-Type: application/x-www-form-urlencoded" \
1401
- -d "grant_type=client_credentials" \
1402
- -d "client_id=${USER_POOL_CLIENT_ID}" \
1403
- -d "client_secret=${CLIENT_SECRET}" \
1404
- -d "scope=${OAUTH_SCOPES}"
1405
- ```
1406
-
1407
- ### Gateway with KMS Encryption
1408
-
1409
- You can provide a KMS key, and configure the authorizer as well as the protocol configuration.
1410
-
1411
- ```typescript fixture=default
1412
- // Create a KMS key for encryption
1413
- const encryptionKey = new kms.Key(this, "GatewayEncryptionKey", {
1414
- enableKeyRotation: true,
1415
- description: "KMS key for gateway encryption",
1416
- });
1417
-
1418
- // Create gateway with KMS encryption
1419
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1420
- gatewayName: "my-encrypted-gateway",
1421
- description: "Gateway with KMS encryption",
1422
- protocolConfiguration: new agentcore.McpProtocolConfiguration({
1423
- instructions: "Use this gateway to connect to external MCP tools",
1424
- searchType: agentcore.McpGatewaySearchType.SEMANTIC,
1425
- supportedVersions: [agentcore.MCPProtocolVersion.MCP_2025_03_26],
1426
- }),
1427
- authorizerConfiguration: agentcore.GatewayAuthorizer.usingCustomJwt({
1428
- discoveryUrl: "https://auth.example.com/.well-known/openid-configuration",
1429
- allowedAudience: ["my-app"],
1430
- allowedClients: ["my-client-id"],
1431
- allowedScopes: ["read", "write"],
1432
- }),
1433
- kmsKey: encryptionKey,
1434
- exceptionLevel: agentcore.GatewayExceptionLevel.DEBUG,
1435
- });
1436
- ```
1437
-
1438
- ### Gateway with Custom Execution Role
1439
-
1440
- ```typescript fixture=default
1441
- // Create a custom execution role
1442
- const executionRole = new iam.Role(this, "GatewayExecutionRole", {
1443
- assumedBy: new iam.ServicePrincipal("bedrock-agentcore.amazonaws.com"),
1444
- managedPolicies: [
1445
- iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonBedrockAgentCoreGatewayExecutionRolePolicy"),
1446
- ],
1447
- });
1448
-
1449
- // Create gateway with custom execution role
1450
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1451
- gatewayName: "my-gateway",
1452
- description: "Gateway with custom execution role",
1453
- protocolConfiguration: new agentcore.McpProtocolConfiguration({
1454
- instructions: "Use this gateway to connect to external MCP tools",
1455
- searchType: agentcore.McpGatewaySearchType.SEMANTIC,
1456
- supportedVersions: [agentcore.MCPProtocolVersion.MCP_2025_03_26],
1457
- }),
1458
- authorizerConfiguration: agentcore.GatewayAuthorizer.usingCustomJwt({
1459
- discoveryUrl: "https://auth.example.com/.well-known/openid-configuration",
1460
- allowedAudience: ["my-app"],
1461
- allowedClients: ["my-client-id"],
1462
- allowedScopes: ["read", "write"],
1463
- }),
1464
- role: executionRole,
1465
- });
1466
- ```
1467
-
1468
- ### Gateway IAM Permissions
1469
-
1470
- The Gateway construct provides convenient methods for granting IAM permissions:
1471
-
1472
- ```typescript fixture=default
1473
- // Create a gateway
1474
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1475
- gatewayName: "my-gateway",
1476
- description: "Gateway for external service integration",
1477
- protocolConfiguration: new agentcore.McpProtocolConfiguration({
1478
- instructions: "Use this gateway to connect to external MCP tools",
1479
- searchType: agentcore.McpGatewaySearchType.SEMANTIC,
1480
- supportedVersions: [agentcore.MCPProtocolVersion.MCP_2025_03_26],
1481
- }),
1482
- authorizerConfiguration: agentcore.GatewayAuthorizer.usingCustomJwt({
1483
- discoveryUrl: "https://auth.example.com/.well-known/openid-configuration",
1484
- allowedAudience: ["my-app"],
1485
- allowedClients: ["my-client-id"],
1486
- allowedScopes: ["read", "write"],
1487
- }),
1488
- });
1489
-
1490
- // Create a role that needs access to the gateway
1491
- const userRole = new iam.Role(this, "UserRole", {
1492
- assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
1493
- });
1494
-
1495
- // Grant read permissions (Get and List actions)
1496
- gateway.grantRead(userRole);
1497
-
1498
- // Grant manage permissions (Create, Update, Delete actions)
1499
- gateway.grantManage(userRole);
1500
-
1501
- // Grant specific custom permissions
1502
- gateway.grant(userRole, "bedrock-agentcore:GetGateway");
1503
- ```
1504
-
1505
- ## Gateway Target
1506
-
1507
- After Creating gateways, you can add targets which define the tools that your gateway will host. Gateway supports multiple target
1508
- types including Lambda functions and API specifications (either OpenAPI schemas or Smithy models). Gateway allows you to attach multiple
1509
- targets to a Gateway and you can change the targets / tools attached to a gateway at any point. Each target can have its own
1510
- credential provider attached enabling you to securely access targets whether they need IAM, API Key, or OAuth credentials.
1511
-
1512
- ### Gateway Target Properties
1513
-
1514
- | Name | Type | Required | Description |
1515
- |------|------|----------|-------------|
1516
- | `gatewayTargetName` | `string` | No | The name of the gateway target. Valid characters are a-z, A-Z, 0-9, _ (underscore) and - (hyphen). If not provided, a unique name will be auto-generated |
1517
- | `description` | `string` | No | Optional description for the gateway target. Maximum 200 characters |
1518
- | `gateway` | `IGateway` | Yes | The gateway this target belongs to |
1519
- | `targetConfiguration` | `ITargetConfiguration` | Yes | The target configuration (Lambda, OpenAPI, Smithy, or API Gateway). **Note:** Users typically don't create this directly. When using convenience methods like `GatewayTarget.forLambda()`, `GatewayTarget.forOpenApi()`, `GatewayTarget.forSmithy()`, `GatewayTarget.forApiGateway()`, `GatewayTarget.forMcpServer()` or the gateway's `addLambdaTarget()`, `addOpenApiTarget()`, `addSmithyTarget()`, `addApiGatewayTarget()`, `addMcpServerTarget()` methods, this configuration is created internally for you. Only needed when using the GatewayTarget constructor directly for [advanced scenarios](#advanced-usage-direct-configuration-for-gateway-target). |
1520
- | `credentialProviderConfigurations` | `IGatewayCredentialProvider[]` | No | Credential providers for authentication. Defaults to `[GatewayCredentialProvider.fromIamRole()]`. Use `GatewayCredentialProvider.fromApiKeyIdentityArn()`, `GatewayCredentialProvider.fromOauthIdentityArn()`, or `GatewayCredentialProvider.fromIamRole()` |
1521
- | `validateOpenApiSchema` | `boolean` | No | (OpenAPI targets only) Whether to validate the OpenAPI schema at synthesis time. Defaults to `true`. Only applies to inline and local asset schemas. For more information refer here <https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-schema-openapi.html> |
1522
-
1523
- This approach gives you full control over the configuration but is typically not necessary for most use cases.
1524
-
1525
- ### Targets types
1526
-
1527
- You can create the following targets types:
1528
-
1529
- **Lambda Target**: Lambda targets allow you to connect your gateway to AWS Lambda functions that implement your tools. This is useful
1530
- when you want to execute custom code in response to tool invocations.
1531
-
1532
- - Supports GATEWAY_IAM_ROLE credential provider only
1533
- - Ideal for custom serverless function integration
1534
- - Need tool schema (tool schema is a blueprint that describes the functions your Lambda provides to AI agents).
1535
- The construct provide [3 ways to upload a tool schema to Lambda target](#tools-schema-for-lambda-target)
1536
- - When using the default IAM authentication (no `credentialProviderConfigurations` specified),
1537
- the construct automatocally grants the gateway role permission to invoke your Lambda function (`lambda:InvokeFunction`).
1538
-
1539
- **OpenAPI Schema Target** : OpenAPI widely used standard for describing RESTful APIs. Gateway supports OpenAPI 3.0
1540
- specifications for defining API targets. It connects to REST APIs using OpenAPI specifications
1541
-
1542
- - Supports OAUTH and API_KEY credential providers (Do not support IAM, you must provide `credentialProviderConfigurations`)
1543
- - Ideal for integrating with external REST services
1544
- - Need API schema. The construct provide [3 ways to upload a API schema to OpenAPI target](#api-schema-for-openapi-and-smithy-target)
1545
-
1546
- **Smithy Model Target** : Smithy is a language for defining services and software development kits (SDKs). Smithy models provide
1547
- a more structured approach to defining APIs compared to OpenAPI, and are particularly useful for connecting to AWS services.
1548
- AgentCore Gateway supports built-in AWS service models only. It connects to services using Smithy model definitions
1549
-
1550
- - Supports OAUTH and API_KEY credential providers
1551
- - Ideal for AWS service integrations
1552
- - Need API schema. The construct provide 3 ways to upload a API schema to Smity target
1553
- - When using the default IAM authentication (no `credentialProviderConfigurations` specified), The construct only
1554
- grants permission to read the Smithy schema file from S3. You MUST manually grant permissions for the gateway
1555
- role to invoke the actual Smithy API endpoints
1556
-
1557
- > Note: For Smithy model targets that access AWS services, your Gateway's execution role needs permissions to access those services.
1558
- For example, for a DynamoDB target, your execution role needs permissions to perform DynamoDB operations.
1559
- This is not managed by the construct due to the large number of options. Please refer to
1560
- [Smithy Model Permission](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-prerequisites-permissions.html) for example.
1561
-
1562
- **MCP Server Target**: Model Context Protocol (MCP) servers provide external tools, data access, and custom functions for AI agents.
1563
- MCP servers enable agents to interact with external systems and services through a standardized protocol. Gateway automatically
1564
- discovers and indexes available tools from MCP servers through synchronization.
1565
-
1566
- **Key Features:**
1567
-
1568
- - Requires explicit authentication configuration (OAuth2 recommended, empty array for NoAuth)
1569
- - Ideal for connecting to external MCP-compliant servers
1570
- - The endpoint must use HTTPS protocol
1571
- - Supported MCP protocol versions: 2025-06-18, 2025-03-26
1572
- - Automatic tool discovery through synchronization
1573
-
1574
- **Synchronization Behavior:**
1575
-
1576
- MCP Server targets require synchronization to discover and index available tools:
1577
-
1578
- - **Implicit Synchronization (Automatic)**: Tool discovery happens automatically during:
1579
- - Target creation (`CreateGatewayTarget`)
1580
- - Target updates (`UpdateGatewayTarget`)
1581
- - The Gateway calls the MCP server's `tools/list` endpoint and indexes tools without user intervention
1582
-
1583
- - **Explicit Synchronization (Manual)**: When the MCP server's tools change independently (new tools added, schemas modified, tools removed):
1584
- - The Gateway's tool catalog becomes stale
1585
- - Call the `SynchronizeGatewayTargets` API to refresh the catalog
1586
- - Use the `grantSync()` method to grant permissions to Lambda functions, CI/CD pipelines, or scheduled tasks that will trigger synchronization
1587
-
1588
- **Authentication & Permissions:**
1589
-
1590
- When using OAuth2, the Gateway service role automatically receives:
1591
-
1592
- - `bedrock-agentcore:GetWorkloadAccessToken`
1593
- - `bedrock-agentcore:GetResourceOauth2Token`
1594
- - `secretsmanager:GetSecretValue`
1595
- - KMS decrypt (if secrets are encrypted)
1596
-
1597
- For explicit synchronization, use `grantSync()` to grant `bedrock-agentcore:SynchronizeGatewayTargets` permission to your operator roles.
1598
-
1599
- > For more information, refer to the [MCP Server Target documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-target-MCPservers.html).
1600
-
1601
- ### Understanding Tool Naming
1602
-
1603
- When tools are exposed through gateway targets, AgentCore Gateway prefixes each tool name with the target name to ensure uniqueness across multiple targets. This is important to understand when building your application logic.
1604
-
1605
- **Naming Pattern:**
1606
-
1607
- **Example:**
1608
-
1609
- If your target is named `my-lambda-target` and provides a tool called `calculate_price`, agents will discover and invoke it as `my-lambda-target__calculate_price`.
1610
-
1611
- **Important Considerations:**
1612
-
1613
- - **For Lambda Targets**: Your Lambda handler must strip the target name prefix before processing the tool request. The full tool name (with prefix) is sent in the event.
1614
- - **For MCP Server Targets**: The MCP server receives tool calls with the prefixed name from the gateway.
1615
- - **For OpenAPI/Smithy Targets**: The gateway handles the prefix automatically when mapping to API operations based on the `operationId`.
1616
-
1617
- This naming convention ensures that:
1618
-
1619
- - Tools from different targets don't collide even if they have the same name
1620
- - Agents can access tools from multiple targets through a single gateway
1621
- - Tool names remain unique in the unified tool catalog
1622
-
1623
- For more details, see the [Gateway Tool Naming Documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-tool-naming.html).
1624
-
1625
- ### Tools schema For Lambda target
1626
-
1627
- The lambda target need tools schema to understand the fuunction lambda provides. You can upload the tool schema by following 3 ways:
1628
-
1629
- - From a local asset file
1630
-
1631
- ```typescript
1632
- const toolSchema = agentcore.ToolSchema.fromLocalAsset(
1633
- path.join(__dirname, "schemas", "my-tool-schema.json")
1634
- );
1635
- ```
1636
-
1637
- - From an existing S3 file:
1638
-
1639
- ```typescript
1640
-
1641
- const toolSchema = agentcore.ToolSchema.fromS3File(
1642
- s3.Bucket.fromBucketName(this, "SchemasBucket", "my-schemas-bucket"),
1643
- "tools/complex-tool-schema.json",
1644
- "123456789012"
1645
- );
1646
- ```
1647
-
1648
- - From Inline:
1649
-
1650
- ```typescript
1651
- const toolSchema = agentcore.ToolSchema.fromInline([{
1652
- name: "hello_world",
1653
- description: "A simple hello world tool",
1654
- inputSchema: {
1655
- type: agentcore.SchemaDefinitionType.OBJECT,
1656
- properties: {
1657
- name: {
1658
- type: agentcore.SchemaDefinitionType.STRING,
1659
- description: "The name to greet",
1660
- },
1661
- },
1662
- required: ["name"],
1663
- },
1664
- }]);
1665
-
1666
- ```
1667
-
1668
- ### Api schema For OpenAPI and Smithy target
1669
-
1670
- The OpenAPI and Smithy target need API Schema. The Gateway construct provide three ways to upload API schema for your target:
1671
-
1672
- - From a local asset file (requires binding to scope):
1673
-
1674
- ```typescript fixture=default
1675
- // When using ApiSchema.fromLocalAsset, you must bind the schema to a scope
1676
- const schema = agentcore.ApiSchema.fromLocalAsset(path.join(__dirname, "mySchema.yml"));
1677
-
1678
- schema.bind(this);
1679
- ```
1680
-
1681
- - From an inline schema:
1682
-
1683
- ```typescript fixture=default
1684
- const inlineSchema = agentcore.ApiSchema.fromInline(`
1685
- openapi: 3.0.3
1686
- info:
1687
- title: Library API
1688
- version: 1.0.0
1689
- paths:
1690
- /search:
1691
- get:
1692
- summary: Search for books
1693
- operationId: searchBooks
1694
- parameters:
1695
- - name: query
1696
- in: query
1697
- required: true
1698
- schema:
1699
- type: string
1700
- `);
1701
- ```
1702
-
1703
- - From an existing S3 file:
1704
-
1705
- ```typescript fixture=default
1706
- const bucket = s3.Bucket.fromBucketName(this, "ExistingBucket", "my-schema-bucket");
1707
- const s3Schema = agentcore.ApiSchema.fromS3File(bucket, "schemas/action-group.yaml");
1708
- ```
1709
-
1710
- ### Outbound auth
1711
-
1712
- Outbound authorization lets Amazon Bedrock AgentCore gateways securely access gateway targets on behalf of users authenticated
1713
- and authorized during Inbound Auth.
1714
-
1715
- AgentCore Gateway supports the following types of outbound authorization:
1716
-
1717
- **IAM-based outbound authorization** – The gateway uses its execution role to authenticate with AWS services. This is the default
1718
- and most common approach for Lambda targets and AWS service integrations.
1719
-
1720
- **2-legged OAuth (OAuth 2LO)** – Use OAuth 2.0 two-legged flow (2LO) for targets that require OAuth authentication.
1721
- The gateway authenticates on its own behalf, not on behalf of a user.
1722
-
1723
- **API key** – Use the AgentCore service/AWS console to generate an API key to authenticate access to the gateway target.
1724
-
1725
- **Note > You need to set up the outbound identity before you can create a gateway target.
1726
-
1727
- ### Basic Gateway Target Creation
1728
-
1729
- You can create targets in two ways: using the static factory methods on `GatewayTarget` or using the convenient `addTarget` methods on the gateway instance.
1730
-
1731
- #### Using addTarget methods (Recommended)
1732
-
1733
- This approach is recommended for most use cases, especially when creating targets alongside the gateway. It provides a cleaner, more fluent API by eliminating the need to explicitly pass the gateway reference.
1734
-
1735
- Below are the examples on how you can create Lambda, Smithy, OpenAPI, MCP Server, and API Gateway targets using `addTarget` methods.
1736
-
1737
- ```typescript fixture=default
1738
- // Create a gateway first
1739
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1740
- gatewayName: "my-gateway",
1741
- });
1742
-
1743
- const lambdaFunction = new lambda.Function(this, "MyFunction", {
1744
- runtime: lambda.Runtime.NODEJS_22_X,
1745
- handler: "index.handler",
1746
- code: lambda.Code.fromInline(`
1747
- exports.handler = async (event) => {
1748
- return {
1749
- statusCode: 200,
1750
- body: JSON.stringify({ message: 'Hello from Lambda!' })
1751
- };
1752
- };
1753
- `),
1754
- });
1755
-
1756
- const lambdaTarget = gateway.addLambdaTarget("MyLambdaTarget", {
1757
- gatewayTargetName: "my-lambda-target",
1758
- description: "Lambda function target",
1759
- lambdaFunction: lambdaFunction,
1760
- toolSchema: agentcore.ToolSchema.fromInline([
1761
- {
1762
- name: "hello_world",
1763
- description: "A simple hello world tool",
1764
- inputSchema: {
1765
- type: agentcore.SchemaDefinitionType.OBJECT,
1766
- properties: {
1767
- name: {
1768
- type: agentcore.SchemaDefinitionType.STRING,
1769
- description: "The name to greet",
1770
- },
1771
- },
1772
- required: ["name"],
1773
- },
1774
- },
1775
- ]),
1776
- });
1777
- ```
1778
-
1779
- - OpenAPI Target
1780
-
1781
- ``` typescript
1782
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1783
- gatewayName: "my-gateway",
1784
- });
1785
-
1786
- // These ARNs are returned when creating the API key credential provider via Console or API
1787
- const apiKeyProviderArn = "arn:aws:bedrock-agentcore:us-east-1:123456789012:token-vault/abc123/apikeycredentialprovider/my-apikey"
1788
- const apiKeySecretArn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-apikey-secret-abc123"
1789
-
1790
- const bucket = s3.Bucket.fromBucketName(this, "ExistingBucket", "my-schema-bucket");
1791
- const s3mySchema = agentcore.ApiSchema.fromS3File(bucket, "schemas/myschema.yaml");
1792
-
1793
- // Add an OpenAPI target directly to the gateway
1794
- const target = gateway.addOpenApiTarget("MyTarget", {
1795
- gatewayTargetName: "my-api-target",
1796
- description: "Target for external API integration",
1797
- apiSchema: s3mySchema,
1798
- credentialProviderConfigurations: [
1799
- agentcore.GatewayCredentialProvider.fromApiKeyIdentityArn({
1800
- providerArn: apiKeyProviderArn,
1801
- secretArn: apiKeySecretArn,
1802
- credentialLocation: agentcore.ApiKeyCredentialLocation.header({
1803
- credentialParameterName: "X-API-Key",
1804
- }),
1805
- }),
1806
- ],
1807
- });
1808
-
1809
- // This make sure your s3 bucket is available before target
1810
- target.node.addDependency(bucket);
1811
- ```
1812
-
1813
- - Smithy Target
1814
-
1815
- ```typescript fixture=default
1816
-
1817
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1818
- gatewayName: "my-gateway",
1819
- });
1820
-
1821
- const smithySchema = agentcore.ApiSchema.fromLocalAsset(
1822
- path.join(__dirname, "models", "smithy-model.json")
1823
- );
1824
- smithySchema.bind(this);
1825
-
1826
- const smithyTarget = gateway.addSmithyTarget("MySmithyTarget", {
1827
- gatewayTargetName: "my-smithy-target",
1828
- description: "Smithy model target",
1829
- smithyModel: smithySchema,
1830
-
1831
- });
1832
- ```
1833
-
1834
- - MCP Server Target
1835
-
1836
- ```typescript fixture=default
1837
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1838
- gatewayName: "my-gateway",
1839
- });
1840
-
1841
- // OAuth2 authentication (recommended)
1842
- // Note: Create the OAuth provider using AWS console or Identity L2 construct when available
1843
- const oauthProviderArn = "arn:aws:bedrock-agentcore:us-east-1:123456789012:token-vault/abc123/oauth2credentialprovider/my-oauth";
1844
- const oauthSecretArn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-oauth-secret-abc123";
1845
-
1846
- // Add an MCP server target directly to the gateway
1847
- const mcpTarget = gateway.addMcpServerTarget("MyMcpServer", {
1848
- gatewayTargetName: "my-mcp-server",
1849
- description: "External MCP server integration",
1850
- endpoint: "https://my-mcp-server.example.com",
1851
- credentialProviderConfigurations: [
1852
- agentcore.GatewayCredentialProvider.fromOauthIdentityArn({
1853
- providerArn: oauthProviderArn,
1854
- secretArn: oauthSecretArn,
1855
- scopes:['mcp-runtime-server/invoke']
1856
- }),
1857
- ],
1858
- });
1859
-
1860
- // Grant sync permission to a Lambda function that will trigger synchronization
1861
- const syncFunction = new lambda.Function(this, "SyncFunction", {
1862
- runtime: lambda.Runtime.PYTHON_3_12,
1863
- handler: "index.handler",
1864
- code: lambda.Code.fromInline(`
1865
- import boto3
1866
-
1867
- def handler(event, context):
1868
- client = boto3.client('bedrock-agentcore')
1869
- response = client.synchronize_gateway_targets(
1870
- gatewayIdentifier=event['gatewayId'],
1871
- targetIds=[event['targetId']]
1872
- )
1873
- return response
1874
- `),
1875
- });
1876
-
1877
- mcpTarget.grantSync(syncFunction);
1878
- ```
1879
-
1880
- - API Gateway Target
1881
-
1882
- ```typescript fixture=default
1883
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1884
- gatewayName: "my-gateway",
1885
- });
1886
-
1887
- const api = new apigateway.RestApi(this, 'MyApi', {
1888
- restApiName: 'my-api',
1889
- });
1890
-
1891
- // Uses IAM authorization for outbound auth by default
1892
- const apiGatewayTarget = gateway.addApiGatewayTarget("MyApiGatewayTarget", {
1893
- restApi: api,
1894
- apiGatewayToolConfiguration: {
1895
- toolFilters: [
1896
- {
1897
- filterPath: "/pets/*",
1898
- methods: [agentcore.ApiGatewayHttpMethod.GET],
1899
- },
1900
- ],
1901
- },
1902
- });
1903
- ```
1904
-
1905
- #### Using static factory methods
1906
-
1907
- Use static factory methods when working with imported gateways, creating targets in different constructs/stacks, or when you need more explicit control over the construct tree hierarchy.
1908
-
1909
- Create Gateway target using static convenience methods.
1910
-
1911
- - Lambda Target
1912
-
1913
- ```typescript fixture=default
1914
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1915
- gatewayName: "my-gateway",
1916
- });
1917
-
1918
- const lambdaFunction = new lambda.Function(this, "MyFunction", {
1919
- runtime: lambda.Runtime.NODEJS_22_X,
1920
- handler: "index.handler",
1921
- code: lambda.Code.fromInline(`
1922
- exports.handler = async (event) => {
1923
- return {
1924
- statusCode: 200,
1925
- body: JSON.stringify({ message: 'Hello from Lambda!' })
1926
- };
1927
- };
1928
- `),
1929
- });
1930
-
1931
- // Create a gateway target with Lambda and tool schema
1932
- const target = agentcore.GatewayTarget.forLambda(this, "MyLambdaTarget", {
1933
- gatewayTargetName: "my-lambda-target",
1934
- description: "Target for Lambda function integration",
1935
- gateway: gateway,
1936
- lambdaFunction: lambdaFunction,
1937
- toolSchema: agentcore.ToolSchema.fromLocalAsset(
1938
- path.join(__dirname, "schemas", "my-tool-schema.json")
1939
- ),
1940
- });
1941
- ```
1942
-
1943
- - OpenAPI Target
1944
-
1945
- ```typescript fixture=default
1946
-
1947
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1948
- gatewayName: "my-gateway",
1949
- });
1950
-
1951
- // outbound auth (Use AWS console to create it, Once Identity L2 construct is available you can use it to create identity)
1952
- const apiKeyIdentityArn = "arn:aws:bedrock-agentcore:us-east-1:123456789012:token-vault/abc123/apikeycredentialprovider/my-apikey"
1953
- const apiKeySecretArn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-apikey-secret-abc123"
1954
-
1955
- const opneapiSchema = agentcore.ApiSchema.fromLocalAsset(path.join(__dirname, "mySchema.yml"));
1956
- opneapiSchema.bind(this);
1957
-
1958
- // Create a gateway target with OpenAPI Schema
1959
- const target = agentcore.GatewayTarget.forOpenApi(this, "MyTarget", {
1960
- gatewayTargetName: "my-api-target",
1961
- description: "Target for external API integration",
1962
- gateway: gateway, // Note: you need to pass the gateway reference
1963
- apiSchema: opneapiSchema,
1964
- credentialProviderConfigurations: [
1965
- agentcore.GatewayCredentialProvider.fromApiKeyIdentityArn({
1966
- providerArn: apiKeyIdentityArn,
1967
- secretArn: apiKeySecretArn
1968
- }),
1969
- ],
1970
- });
1971
-
1972
- ```
1973
-
1974
- - Smithy Target
1975
-
1976
- ```typescript fixture=default
1977
-
1978
- const gateway = new agentcore.Gateway(this, "MyGateway", {
1979
- gatewayName: "my-gateway",
1980
- });
1981
-
1982
- const smithySchema = agentcore.ApiSchema.fromLocalAsset(
1983
- path.join(__dirname, "models", "smithy-model.json")
1984
- );
1985
- smithySchema.bind(this);
1986
-
1987
- // Create a gateway target with Smithy Model and OAuth
1988
- const target = agentcore.GatewayTarget.forSmithy(this, "MySmithyTarget", {
1989
- gatewayTargetName: "my-smithy-target",
1990
- description: "Target for Smithy model integration",
1991
- gateway: gateway,
1992
- smithyModel: smithySchema,
1993
- });
1994
-
1995
- ```
1996
-
1997
- - MCP Server Target
1998
-
1999
- ```typescript fixture=default
2000
- const gateway = new agentcore.Gateway(this, "MyGateway", {
2001
- gatewayName: "my-gateway",
2002
- });
2003
-
2004
- // OAuth2 authentication (recommended)
2005
- // Note: Create the OAuth provider using AWS console or Identity L2 construct when available
2006
- const oauthProviderArn = "arn:aws:bedrock-agentcore:us-east-1:123456789012:token-vault/abc123/oauth2credentialprovider/my-oauth";
2007
- const oauthSecretArn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:my-oauth-secret-abc123";
2008
-
2009
- // Create a gateway target with MCP Server
2010
- const mcpTarget = agentcore.GatewayTarget.forMcpServer(this, "MyMcpServer", {
2011
- gatewayTargetName: "my-mcp-server",
2012
- description: "External MCP server integration",
2013
- gateway: gateway,
2014
- endpoint: "https://my-mcp-server.example.com",
2015
- credentialProviderConfigurations: [
2016
- agentcore.GatewayCredentialProvider.fromOauthIdentityArn({
2017
- providerArn: oauthProviderArn,
2018
- secretArn: oauthSecretArn,
2019
- scopes:['mcp-runtime-server/invoke']
2020
- }),
2021
- ],
2022
- });
2023
- ```
2024
-
2025
- - API Gateway Target
2026
-
2027
- ```typescript fixture=default
2028
- const gateway = new agentcore.Gateway(this, "MyGateway", {
2029
- gatewayName: "my-gateway",
2030
- });
2031
-
2032
- const api = new apigateway.RestApi(this, 'MyApi', {
2033
- restApiName: 'my-api',
2034
- });
2035
-
2036
- // Create a gateway target using the static factory method
2037
- const apiGatewayTarget = agentcore.GatewayTarget.forApiGateway(this, "MyApiGatewayTarget", {
2038
- gatewayTargetName: "my-api-gateway-target",
2039
- description: "Target for API Gateway REST API integration",
2040
- gateway: gateway,
2041
- restApi: api,
2042
- apiGatewayToolConfiguration: {
2043
- toolFilters: [
2044
- {
2045
- filterPath: "/pets/*",
2046
- methods: [agentcore.ApiGatewayHttpMethod.GET, agentcore.ApiGatewayHttpMethod.POST],
2047
- },
2048
- ],
2049
- },
2050
- metadataConfiguration: {
2051
- allowedRequestHeaders: ["X-User-Id"],
2052
- allowedQueryParameters: ["limit"],
2053
- },
2054
- });
2055
- ```
2056
-
2057
- ### Advanced Usage: Direct Configuration for gateway target
2058
-
2059
- For advanced use cases where you need full control over the target configuration, you can create configurations manually using the static factory methods and use the GatewayTarget constructor directly.
2060
-
2061
- #### Configuration Factory Methods
2062
-
2063
- Each target type has a corresponding configuration class with a static `create()` method:
2064
-
2065
- - **Lambda**: `LambdaTargetConfiguration.create(lambdaFunction, toolSchema)`
2066
- - **OpenAPI**: `OpenApiTargetConfiguration.create(apiSchema, validateSchema?)`
2067
- - **Smithy**: `SmithyTargetConfiguration.create(smithyModel)`
2068
- - **API Gateway**: `ApiGatewayTargetConfiguration.create(props)`
2069
-
2070
- #### Example: Lambda Target with Custom Configuration
2071
-
2072
- ```typescript
2073
- const gateway = new agentcore.Gateway(this, "MyGateway", {
2074
- gatewayName: "my-gateway",
2075
- });
2076
-
2077
- const myLambdaFunction = new lambda.Function(this, "MyFunction", {
2078
- runtime: lambda.Runtime.NODEJS_22_X,
2079
- handler: "index.handler",
2080
- code: lambda.Code.fromInline(`
2081
- exports.handler = async (event) => ({ statusCode: 200 });
2082
- `),
2083
- });
2084
-
2085
- const myToolSchema = agentcore.ToolSchema.fromInline([{
2086
- name: "my_tool",
2087
- description: "My custom tool",
2088
- inputSchema: {
2089
- type: agentcore.SchemaDefinitionType.OBJECT,
2090
- properties: {},
2091
- },
2092
- }]);
2093
-
2094
- // Create a custom Lambda configuration
2095
- const customConfig = agentcore.LambdaTargetConfiguration.create(
2096
- myLambdaFunction,
2097
- myToolSchema
2098
- );
2099
-
2100
- // Use the GatewayTarget constructor directly
2101
- const target = new agentcore.GatewayTarget(this, "AdvancedTarget", {
2102
- gateway: gateway,
2103
- gatewayTargetName: "advanced-target",
2104
- targetConfiguration: customConfig, // Manually created configuration
2105
- credentialProviderConfigurations: [
2106
- agentcore.GatewayCredentialProvider.fromIamRole()
2107
- ]
2108
- });
2109
- ```
2110
-
2111
- This approach gives you full control over the configuration but is typically not necessary for most use cases. The convenience methods (`GatewayTarget.forLambda()`, `GatewayTarget.forOpenApi()`, `GatewayTarget.forSmithy()`, `GatewayTarget.forApiGateway()`) handle all of this internally.
2112
-
2113
- ### Gateway Interceptors
2114
-
2115
- Gateway interceptors allow you to run custom code during each gateway invocation to implement fine-grained access control, transform requests and responses, or implement custom authorization logic. A gateway can have at most one REQUEST interceptor and one RESPONSE interceptor.
2116
-
2117
- **Interceptor Types:**
2118
-
2119
- - **REQUEST interceptors**: Execute before the gateway calls the target. Useful for request validation, transformation, or custom authorization
2120
- - **RESPONSE interceptors**: Execute after the target responds but before the gateway sends the response back. Useful for response transformation, filtering, or adding custom headers
2121
-
2122
- **Security Best Practices:**
2123
-
2124
- 1. Keep `passRequestHeaders` disabled unless absolutely necessary (default: false)
2125
- 2. Implement idempotent Lambda functions (gateway may retry on failures)
2126
- 3. Restrict gateway execution role to specific Lambda functions
2127
- 4. Avoid logging sensitive information in your interceptor
2128
-
2129
- For more information, see the [Gateway Interceptors documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/gateway-interceptors.html).
2130
-
2131
- #### Adding Interceptors via Constructor
2132
-
2133
- ```typescript fixture=default
2134
- // Create Lambda functions for interceptors
2135
- const requestInterceptorFn = new lambda.Function(this, "RequestInterceptor", {
2136
- runtime: lambda.Runtime.PYTHON_3_12,
2137
- handler: "index.handler",
2138
- code: lambda.Code.fromInline(`
2139
- def handler(event, context):
2140
- # Validate and transform request
2141
- return {
2142
- "interceptorOutputVersion": "1.0",
2143
- "mcp": {
2144
- "transformedGatewayRequest": event["mcp"]["gatewayRequest"]
2145
- }
2146
- }
2147
- `),
2148
- });
2149
-
2150
- const responseInterceptorFn = new lambda.Function(this, "ResponseInterceptor", {
2151
- runtime: lambda.Runtime.PYTHON_3_12,
2152
- handler: "index.handler",
2153
- code: lambda.Code.fromInline(`
2154
- def handler(event, context):
2155
- # Filter or transform response
2156
- return {
2157
- "interceptorOutputVersion": "1.0",
2158
- "mcp": {
2159
- "transformedGatewayResponse": event["mcp"]["gatewayResponse"]
2160
- }
2161
- }
2162
- `),
2163
- });
2164
-
2165
- // Create gateway with interceptors
2166
- const gateway = new agentcore.Gateway(this, "MyGateway", {
2167
- gatewayName: "my-gateway",
2168
- interceptorConfigurations: [
2169
- agentcore.LambdaInterceptor.forRequest(requestInterceptorFn, {
2170
- passRequestHeaders: true // Only if you need to inspect headers
2171
- }),
2172
- agentcore.LambdaInterceptor.forResponse(responseInterceptorFn)
2173
- ]
2174
- });
2175
- ```
2176
-
2177
- **Automatic Permission Granting:**
2178
-
2179
- When you add a Lambda interceptor to a gateway (either via constructor or `addInterceptor()`), the gateway's IAM role automatically receives `lambda:InvokeFunction` permission on the Lambda function. This permission grant happens internally during the bind process - you do not need to manually configure these IAM permissions.
2180
-
2181
- #### Adding Interceptors Dynamically
2182
-
2183
- ```typescript fixture=default
2184
- // Create a gateway first
2185
- const gateway = new agentcore.Gateway(this, "MyGateway", {
2186
- gatewayName: "my-gateway",
2187
- });
2188
-
2189
- // Create Lambda functions for interceptors
2190
- const requestInterceptorFn = new lambda.Function(this, "RequestInterceptor", {
2191
- runtime: lambda.Runtime.PYTHON_3_12,
2192
- handler: "index.handler",
2193
- code: lambda.Code.fromInline(`
2194
- def handler(event, context):
2195
- # Custom request validation logic
2196
- return {
2197
- "interceptorOutputVersion": "1.0",
2198
- "mcp": {
2199
- "transformedGatewayRequest": event["mcp"]["gatewayRequest"]
2200
- }
2201
- }
2202
- `),
2203
- });
2204
-
2205
- const responseInterceptorFn = new lambda.Function(this, "ResponseInterceptor", {
2206
- runtime: lambda.Runtime.PYTHON_3_12,
2207
- handler: "index.handler",
2208
- code: lambda.Code.fromInline(`
2209
- def handler(event, context):
2210
- # Filter sensitive data from response
2211
- return {
2212
- "interceptorOutputVersion": "1.0",
2213
- "mcp": {
2214
- "transformedGatewayResponse": event["mcp"]["gatewayResponse"]
2215
- }
2216
- }
2217
- `),
2218
- });
2219
-
2220
- gateway.addInterceptor(
2221
- agentcore.LambdaInterceptor.forRequest(requestInterceptorFn, {
2222
- passRequestHeaders: false // Default, headers not passed for security
2223
- })
2224
- );
2225
-
2226
- gateway.addInterceptor(
2227
- agentcore.LambdaInterceptor.forResponse(responseInterceptorFn)
2228
- );
2229
- ```
2230
-
2231
- ### Gateway Target IAM Permissions
2232
-
2233
- The Gateway Target construct provides convenient methods for granting IAM permissions:
2234
-
2235
- ```typescript fixture=default
2236
- // Create a gateway and target
2237
- const gateway = new agentcore.Gateway(this, "MyGateway", {
2238
- gatewayName: "my-gateway",
2239
- });
2240
-
2241
- const smithySchema = agentcore.ApiSchema.fromLocalAsset(
2242
- path.join(__dirname, "models", "smithy-model.json")
2243
- );
2244
- smithySchema.bind(this);
2245
-
2246
- // Create a gateway target with Smithy Model and OAuth
2247
- const target = agentcore.GatewayTarget.forSmithy(this, "MySmithyTarget", {
2248
- gatewayTargetName: "my-smithy-target",
2249
- description: "Target for Smithy model integration",
2250
- gateway: gateway,
2251
- smithyModel: smithySchema,
2252
- });
2253
-
2254
- // Create a role that needs access to the gateway target
2255
- const userRole = new iam.Role(this, "UserRole", {
2256
- assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
2257
- });
2258
-
2259
- // Grant read permissions (Get and List actions)
2260
- target.grantRead(userRole);
2261
-
2262
- // Grant manage permissions (Create, Update, Delete actions)
2263
- target.grantManage(userRole);
2264
-
2265
- // Grant specific custom permissions
2266
- target.grant(userRole, "bedrock-agentcore:GetGatewayTarget");
2267
-
2268
-
2269
- // Grants permission to invoke this Gateway
2270
- gateway.grantInvoke(userRole);
2271
- ```
2272
-
2273
- ## Memory
2274
-
2275
- Memory is a critical component of intelligence. While Large Language Models (LLMs) have impressive capabilities, they lack persistent memory across conversations. Amazon Bedrock AgentCore Memory addresses this limitation by providing a managed service that enables AI agents to maintain context over time, remember important facts, and deliver consistent, personalized experiences.
2276
-
2277
- AgentCore Memory operates on two levels:
2278
-
2279
- - **Short-Term Memory**: Immediate conversation context and session-based information that provides continuity within a single interaction or closely related sessions.
2280
- - **Long-Term Memory**: Persistent information extracted and stored across multiple conversations, including facts, preferences, and summaries that enable personalized experiences over time.
2281
-
2282
- When you interact with the memory via the `CreateEvent` API, you store interactions in Short-Term Memory (STM) instantly. These interactions can include everything from user messages, assistant responses, to tool actions.
2283
-
2284
- To write to long-term memory, you need to configure extraction strategies which define how and where to store information from conversations for future use. These strategies are asynchronously processed from raw events after every few turns based on the strategy that was selected. You can't create long term memory records directly, as they are extracted asynchronously by AgentCore Memory.
2285
-
2286
- ### Memory Properties
2287
-
2288
- | Name | Type | Required | Description |
2289
- |------|------|----------|-------------|
2290
- | `memoryName` | `string` | No | The name of the memory. If not provided, a unique name will be auto-generated |
2291
- | `expirationDuration` | `Duration` | No | Short-term memory expiration in days (between 7 and 365). Default: 90 days |
2292
- | `description` | `string` | No | Optional description for the memory. Default: no description. |
2293
- | `kmsKey` | `IKey` | No | Custom KMS key to use for encryption. Default: Your data is encrypted with a key that AWS owns and manages for you |
2294
- | `memoryStrategies` | `MemoryStrategyBase[]` | No | Built-in extraction strategies to use for this memory. Default: No extraction strategies (short term memory only) |
2295
- | `executionRole` | `iam.IRole` | No | The IAM role that provides permissions for the memory to access AWS services. Default: A new role will be created. |
2296
- | `tags` | `{ [key: string]: string }` | No | Tags for memory. Default: no tags. |
2297
-
2298
- ### Basic Memory Creation
2299
-
2300
- Below you can find how to configure a simple short-term memory (STM) with no long-term memory extraction strategies.
2301
- Note how you set `expirationDuration`, which defines the time the events will be stored in the short-term memory before they expire.
2302
-
2303
- ```typescript fixture=default
2304
-
2305
- // Create a basic memory with default settings, no LTM strategies
2306
- const memory = new agentcore.Memory(this, "MyMemory", {
2307
- memoryName: "my_memory",
2308
- description: "A memory for storing user interactions for a period of 90 days",
2309
- expirationDuration: cdk.Duration.days(90),
2310
- });
2311
- ```
2312
-
2313
- Basic Memory with Custom KMS Encryption
2314
-
2315
- ```typescript fixture=default
2316
- // Create a custom KMS key for encryption
2317
- const encryptionKey = new kms.Key(this, "MemoryEncryptionKey", {
2318
- enableKeyRotation: true,
2319
- description: "KMS key for memory encryption",
2320
- });
2321
-
2322
- // Create memory with custom encryption
2323
- const memory = new agentcore.Memory(this, "MyMemory", {
2324
- memoryName: "my_encrypted_memory",
2325
- description: "Memory with custom KMS encryption",
2326
- expirationDuration: cdk.Duration.days(90),
2327
- kmsKey: encryptionKey,
2328
- });
2329
- ```
2330
-
2331
- ### LTM Memory Extraction Stategies
2332
-
2333
- If you need long-term memory for context recall across sessions, you can setup memory extraction strategies
2334
- to extract the relevant memory from the raw events.
2335
-
2336
- Amazon Bedrock AgentCore Memory has different memory strategies for extracting and organizing information:
2337
-
2338
- - **Summarization**: to summarize interactions to preserve critical context and key insights.
2339
- - **Semantic Memory**: to extract general factual knowledge, concepts and meanings from raw conversations using vector embeddings.
2340
- This enables similarity-based retrieval of relevant facts and context.
2341
- - **User Preferences**: to extract user behavior patterns from raw conversations.
2342
-
2343
- You can use built-in extraction strategies for quick setup, or create custom extraction strategies with specific models and prompt templates.
2344
-
2345
- ### Memory with Built-in Strategies
2346
-
2347
- The library provides four built-in LTM strategies. These are default strategies for organizing and extracting memory data,
2348
- each optimized for specific use cases.
2349
-
2350
- For example: An agent helps multiple users with cloud storage setup. From these conversations,
2351
- see how each strategy processes users expressing confusion about account connection:
2352
-
2353
- 1. **Summarization Strategy** (`MemoryStrategy.usingBuiltInSummarization()`)
2354
- This strategy compresses conversations into concise overviews, preserving essential context and key insights for quick recall.
2355
- Extracted memory example: Users confused by cloud setup during onboarding.
2356
-
2357
- - Extracts concise summaries to preserve critical context and key insights
2358
- - Namespace: `/strategies/{memoryStrategyId}/actors/{actorId}/sessions/{sessionId}`
2359
-
2360
- 2. **Semantic Memory Strategy** (`MemoryStrategy.usingBuiltInSemantic()`)
2361
- Distills general facts, concepts, and underlying meanings from raw conversational data, presenting the information in a context-independent format.
2362
- Extracted memory example: In-context learning = task-solving via examples, no training needed.
2363
-
2364
- - Extracts general factual knowledge, concepts and meanings from raw conversations
2365
- - Namespace: `/strategies/{memoryStrategyId}/actors/{actorId}`
2366
-
2367
- 3. **User Preference Strategy** (`MemoryStrategy.usingBuiltInUserPreference()`)
2368
- Captures individual preferences, interaction patterns, and personalized settings to enhance future experiences.
2369
- Extracted memory example: User needs clear guidance on cloud storage account connection during onboarding.
2370
-
2371
- - Extracts user behavior patterns from raw conversations
2372
- - Namespace: `/strategies/{memoryStrategyId}/actors/{actorId}`
2373
-
2374
- 4. **Episodic Memory Strategy** (`MemoryStrategy.usingBuiltInEpisodic()`)
2375
- Captures meaningful slices of user and system interactions, preserve them into compact records after summarizing.
2376
- Extracted memory example: User first asked about pricing on Monday, then requested feature comparison on Tuesday, finally made purchase decision on Wednesday.
2377
-
2378
- - Captures event sequences and temporal relationships
2379
- - Namespace: `/strategy/{memoryStrategyId}/actor/{actorId}/session/{sessionId}`
2380
- - Reflections: `/strategy/{memoryStrategyId}/actor/{actorId}`
2381
-
2382
- ```typescript fixture=default
2383
- // Create memory with built-in strategies
2384
- const memory = new agentcore.Memory(this, "MyMemory", {
2385
- memoryName: "my_memory",
2386
- description: "Memory with built-in strategies",
2387
- expirationDuration: cdk.Duration.days(90),
2388
- memoryStrategies: [
2389
- agentcore.MemoryStrategy.usingBuiltInSummarization(),
2390
- agentcore.MemoryStrategy.usingBuiltInSemantic(),
2391
- agentcore.MemoryStrategy.usingBuiltInUserPreference(),
2392
- agentcore.MemoryStrategy.usingBuiltInEpisodic(),
2393
- ],
2394
- });
2395
- ```
2396
-
2397
- The name generated for each built in memory strategy is as follows:
2398
-
2399
- - For Summarization: `summary_builtin_cdk001`
2400
- - For Semantic:`semantic_builtin_cdk001>`
2401
- - For User Preferences: `preference_builtin_cdk001`
2402
- - For Episodic : `episodic_builtin_cdkGen0001`
2403
-
2404
- ### Memory with custom Strategies
2405
-
2406
- With Long-Term Memory, organization is managed through Namespaces.
2407
-
2408
- An `actor` refers to entity such as end users or agent/user combinations. For example, in a coding support chatbot,
2409
- the actor is usually the developer asking questions. Using the actor ID helps the system know which user the memory belongs to,
2410
- keeping each user's data separate and organized.
2411
-
2412
- A `session` is usually a single conversation or interaction period between the user and the AI agent.
2413
- It groups all related messages and events that happen during that conversation.
2414
-
2415
- A `namespace` is used to logically group and organize long-term memories. It ensures data stays neat, separate, and secure.
2416
-
2417
- With AgentCore Memory, you need to add a namespace when you define a memory strategy. This namespace helps define where the long-term memory
2418
- will be logically grouped. Every time a new long-term memory is extracted using this memory strategy, it is saved under the namespace you set.
2419
- This means that all long-term memories are scoped to their specific namespace, keeping them organized and preventing any mix-ups with other
2420
- users or sessions. You should use a hierarchical format separated by forward slashes /. This helps keep memories organized clearly. As needed,
2421
- you can choose to use the below pre-defined variables within braces in the namespace based on your applications' organization needs:
2422
-
2423
- - `actorId` – Identifies who the long-term memory belongs to, such as a user
2424
- - `memoryStrategyId` – Shows which memory strategy is being used. This strategy identifier is auto-generated when you create a memory using CreateMemory operation.
2425
- - `sessionId` – Identifies which session or conversation the memory is from.
2426
-
2427
- For example, if you define the following namespace as the input to your strategy in CreateMemory operation:
2428
-
2429
- ```shell
2430
- /strategy/{memoryStrategyId}/actor/{actorId}/session/{sessionId}
2431
- ```
2432
-
2433
- After memory creation, this namespace might look like:
2434
-
2435
- ```shell
2436
- /strategy/summarization-93483043//actor/actor-9830m2w3/session/session-9330sds8
2437
- ```
2438
-
2439
- You can customise the namespace, i.e. where the memories are stored by using the following methods:
2440
-
2441
- 1. **Summarization Strategy** (`MemoryStrategy.usingSummarization(props)`)
2442
- 1. **Semantic Memory Strategy** (`MemoryStrategy.usingSemantic(props)`)
2443
- 1. **User Preference Strategy** (`MemoryStrategy.usingUserPreference(props)`)
2444
- 1. **Episodic Memory Strategy** (`MemoryStrategy.usingEpisodic(props)`)
2445
-
2446
- ```typescript fixture=default
2447
- // Create memory with custom strategies
2448
- const memory = new agentcore.Memory(this, "MyMemory", {
2449
- memoryName: "my_memory",
2450
- description: "Memory with custom strategies",
2451
- expirationDuration: cdk.Duration.days(90),
2452
- memoryStrategies: [
2453
- agentcore.MemoryStrategy.usingUserPreference({
2454
- name: "CustomerPreferences",
2455
- namespaces: ["support/customer/{actorId}/preferences"]
2456
- }),
2457
- agentcore.MemoryStrategy.usingSemantic({
2458
- name: "CustomerSupportSemantic",
2459
- namespaces: ["support/customer/{actorId}/semantic"]
2460
- }),
2461
- agentcore.MemoryStrategy.usingEpisodic({
2462
- name: "customerJourneyEpisodic",
2463
- namespaces: ["/journey/customer/{actorId}/episodes"],
2464
- reflectionConfiguration: {
2465
- namespaces: ["/journey/customer/{actorId}/reflections"]
2466
- }
2467
- }),
2468
- ],
2469
- });
2470
- ```
2471
-
2472
- Custom memory strategies let you tailor memory extraction and consolidation to your specific domain or use case.
2473
- You can override the prompts for extracting and consolidating semantic, summary, or user preferences.
2474
- You can also choose the model that you want to use for extraction and consolidation.
2475
-
2476
- The custom prompts you create are appended to a non-editable system prompt.
2477
-
2478
- Since a custom strategy requires you to invoke certain FMs, you need a role with appropriate permissions. For that, you can:
19
+ > The **Policy** submodule is the only submodule that remains in alpha. All other constructs have graduated to stable in `aws-cdk-lib/aws-bedrockagentcore` and we recommend migrating to the stable versions.
2479
20
 
2480
- - Let the L2 construct create a minimum permission role for you when use L2 Bedrock Foundation Models.
2481
- - Use a custom role with the overly permissive `AmazonBedrockAgentCoreMemoryBedrockModelInferenceExecutionRolePolicy` managed policy.
2482
- - Use a custom role with your own custom policies.
2483
-
2484
- #### Memory with Custom Execution Role
21
+ | **Language** | **Package** |
22
+ | :--------------------------------------------------------------------------------------------- | --------------------------------------- |
23
+ | ![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) TypeScript | `@aws-cdk/aws-bedrock-agentcore-alpha` |
2485
24
 
2486
- Keep in mind that memories that **do not** use custom strategies do not require a service role.
2487
- So even if you provide it, it will be ignored as it will never be used.
25
+ ## Migration to Stable
2488
26
 
2489
- ```typescript fixture=default
2490
- // Create a custom execution role
2491
- const executionRole = new iam.Role(this, "MemoryExecutionRole", {
2492
- assumedBy: new iam.ServicePrincipal("bedrock-agentcore.amazonaws.com"),
2493
- managedPolicies: [
2494
- iam.ManagedPolicy.fromAwsManagedPolicyName(
2495
- "AmazonBedrockAgentCoreMemoryBedrockModelInferenceExecutionRolePolicy"
2496
- ),
2497
- ],
2498
- });
27
+ All constructs except Policy have moved to `aws-cdk-lib/aws-bedrockagentcore`:
2499
28
 
2500
- // Create memory with custom execution role
2501
- const memory = new agentcore.Memory(this, "MyMemory", {
2502
- memoryName: "my_memory",
2503
- description: "Memory with custom execution role",
2504
- expirationDuration: cdk.Duration.days(90),
2505
- executionRole: executionRole,
2506
- });
29
+ ```ts nofixture
30
+ // Before
31
+ import * as agentcore from '@aws-cdk/aws-bedrock-agentcore-alpha';
2507
32
  ```
2508
33
 
2509
- In customConsolidation and customExtraction, the model property uses the [@aws-cdk/aws-bedrock-alph](https://www.npmjs.com/package/@aws-cdk/aws-bedrock-alpha) library which must be installed separately.
2510
-
2511
- ```typescript fixture=default
2512
- // Create a custom semantic memory strategy
2513
- const customSemanticStrategy = agentcore.MemoryStrategy.usingSemantic({
2514
- name: "customSemanticStrategy",
2515
- description: "Custom semantic memory strategy",
2516
- namespaces: ["/custom/strategies/{memoryStrategyId}/actors/{actorId}"],
2517
- customConsolidation: {
2518
- model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
2519
- appendToPrompt: "Custom consolidation prompt for semantic memory",
2520
- },
2521
- customExtraction: {
2522
- model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_5_SONNET_V1_0,
2523
- appendToPrompt: "Custom extraction prompt for semantic memory",
2524
- },
2525
- });
2526
-
2527
- // Create memory with custom strategy
2528
- const memory = new agentcore.Memory(this, "MyMemory", {
2529
- memoryName: "my-custom-memory",
2530
- description: "Memory with custom strategy",
2531
- expirationDuration: cdk.Duration.days(90),
2532
- memoryStrategies: [customSemanticStrategy],
2533
- });
34
+ ```ts nofixture
35
+ // After (for all non-Policy constructs)
36
+ import * as agentcore from 'aws-cdk-lib/aws-bedrockagentcore';
2534
37
  ```
2535
38
 
2536
- ### Memory with self-managed Strategies
2537
-
2538
- A self-managed strategy in Amazon Bedrock AgentCore Memory gives you complete control over your memory extraction and consolidation pipelines.
2539
- With a self-managed strategy, you can build custom memory processing workflows while leveraging Amazon Bedrock AgentCore for storage and retrieval.
2540
-
2541
- For additional information, you can refer to the [developer guide for self managed strategies](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/memory-self-managed-strategies.html).
2542
-
2543
- Create the required AWS resources including:
2544
-
2545
- - an S3 bucket in your account where Amazon Bedrock AgentCore will deliver batched event payloads.
2546
- - an SNS topic for job notifications. Use FIFO topics if processing order within sessions is important for your use case.
2547
-
2548
- The construct will apply the correct permissions to the memory execution role to access these resources.
2549
-
2550
- ```typescript fixture=default
2551
-
2552
- const bucket = new s3.Bucket(this, 'memoryBucket', {
2553
- bucketName: 'test-memory',
2554
- removalPolicy: cdk.RemovalPolicy.DESTROY,
2555
- autoDeleteObjects: true,
2556
- });
2557
-
2558
- const topic = new sns.Topic(this, 'topic');
2559
-
2560
- // Create a custom semantic memory strategy
2561
- const selfManagedStrategy = agentcore.MemoryStrategy.usingSelfManaged({
2562
- name: "selfManagedStrategy",
2563
- description: "self managed memory strategy",
2564
- historicalContextWindowSize: 5,
2565
- invocationConfiguration: {
2566
- topic: topic,
2567
- s3Location: {
2568
- bucketName: bucket.bucketName,
2569
- objectKey: 'memory/',
2570
- }
2571
- },
2572
- triggerConditions: {
2573
- messageBasedTrigger: 1,
2574
- timeBasedTrigger: cdk.Duration.seconds(10),
2575
- tokenBasedTrigger: 100
2576
- }
2577
- });
2578
-
2579
- // Create memory with custom strategy
2580
- const memory = new agentcore.Memory(this, "MyMemory", {
2581
- memoryName: "my-custom-memory",
2582
- description: "Memory with custom strategy",
2583
- expirationDuration: cdk.Duration.days(90),
2584
- memoryStrategies: [selfManagedStrategy],
2585
- });
2586
- ```
39
+ The following constructs are now in stable:
2587
40
 
2588
- ### Memory Strategy Methods
41
+ - **Runtime**: `Runtime`, `RuntimeEndpoint`, `AgentRuntimeArtifact`, `NetworkConfiguration`, `Observability`
42
+ - **Gateway**: `Gateway`, `GatewayTarget`, `GatewayAuthorizer`, `GatewayCredentialProvider`, `Interceptor`
43
+ - **Tools**: `BrowserCustom`, `CodeInterpreterCustom`
44
+ - **Memory**: `Memory`, `MemoryStrategy`
45
+ - **Evaluation**: `OnlineEvaluationConfig`, `Evaluator`, `EvaluatorSelector`
46
+ - **Identity**: `OAuth2CredentialProvider`, `ApiKeyCredentialProvider`, `WorkloadIdentity`
2589
47
 
2590
- You can add new memory strategies to the memory construct using the `addMemoryStrategy()` method, for instance:
48
+ ## What Remains in Alpha
2591
49
 
2592
- ```typescript fixture=default
2593
- // Create memory without initial strategies
2594
- const memory = new agentcore.Memory(this, "test-memory", {
2595
- memoryName: "test_memory_add_strategy",
2596
- description: "A test memory for testing addMemoryStrategy method",
2597
- expirationDuration: cdk.Duration.days(90),
2598
- });
50
+ The Policy submodule remains experimental:
2599
51
 
2600
- // Add strategies after instantiation
2601
- memory.addMemoryStrategy(agentcore.MemoryStrategy.usingBuiltInSummarization());
2602
- memory.addMemoryStrategy(agentcore.MemoryStrategy.usingBuiltInSemantic());
2603
- ```
52
+ - `PolicyEngine`
53
+ - `Policy`
54
+ - `PolicyStatement`
55
+ - `PolicyValidationMode`
56
+ - `PolicyEngineMode`
2604
57
 
2605
58
  ## Policy Engine
2606
59
 
@@ -2662,7 +115,7 @@ When associating a policy engine with a gateway, you can control the enforcement
2662
115
 
2663
116
  ```typescript fixture=default
2664
117
 
2665
- // Create a Policy engine
118
+ // Create a Policy engine
2666
119
  const policyEngine = new agentcore.PolicyEngine(this, "MyPolicyEngine", {
2667
120
  policyEngineName: "my_policy_engine",
2668
121
  description: "Policy engine for access control",
@@ -2745,12 +198,12 @@ declare const gateway: agentcore.Gateway;
2745
198
  // Action names follow pattern: "ToolName__operation"
2746
199
  policyEngine.addPolicy("SpecificToolPolicy", {
2747
200
  statement: agentcore.PolicyStatement.permit()
2748
- .forPrincipal('AgentCore::OAuthUser::your-client-id')
201
+ .forPrincipal('AgentCore::OAuthUser::your-client-id')
2749
202
  .onActions([
2750
203
  'AgentCore::Action::WeatherTool__get_forecast',
2751
204
  'AgentCore::Action::WeatherTool__get_current',
2752
205
  ])
2753
- .onResource('AgentCore::Gateway', gateway.gatewayArn),
206
+ .onResource('AgentCore::Gateway', gateway.gatewayArn),
2754
207
  description: "Allow specific weather tool operations",
2755
208
  validationMode: agentcore.PolicyValidationMode.FAIL_ON_ANY_FINDINGS,
2756
209
  });
@@ -2997,353 +450,47 @@ const lambdaRole = new iam.Role(this, "LambdaRole", {
2997
450
  assumedBy: new iam.ServicePrincipal("lambda.amazonaws.com"),
2998
451
  });
2999
452
 
3000
- // Grant read permissions
453
+ // Grant read permissions
3001
454
  policyEngine.grantRead(lambdaRole);
3002
455
 
3003
- // Grant evaluation permissions
456
+ // Grant evaluation permissions
3004
457
  policyEngine.grantEvaluate(lambdaRole);
3005
-
3006
- ```
3007
-
3008
- ## Online Evaluation
3009
-
3010
- The Online Evaluation construct enables continuous monitoring and assessment of your agent's performance using live traffic. It automatically samples agent traces from CloudWatch Logs or Agent Endpoints and applies built-in evaluators to assess quality metrics like helpfulness, correctness, and safety.
3011
-
3012
- ### Online Evaluation Properties
3013
-
3014
- | Name | Type | Required | Description |
3015
- |------|------|----------|-------------|
3016
- | `onlineEvaluationConfigName` | `string` | Yes | The name of the online evaluation configuration. Must start with a letter and can contain a-z, A-Z, 0-9, _ (underscore). Maximum 48 characters |
3017
- | `evaluators` | `EvaluatorReference[]` | Yes | The list of built-in evaluators to apply during evaluation. Minimum 1, maximum 10 |
3018
- | `dataSource` | `DataSourceConfig` | Yes | The data source configuration specifying where to read agent traces from |
3019
- | `executionRole` | `iam.IRole` | No | The IAM role for evaluation. If not provided, a role will be created automatically |
3020
- | `description` | `string` | No | Description of the evaluation configuration. Maximum 200 characters |
3021
- | `samplingPercentage` | `number` | No | Percentage of traces to sample (0.01-100). Default: 10 |
3022
- | `filters` | `FilterConfig[]` | No | Filters to determine which traces to evaluate. Use `FilterValue.string()`, `FilterValue.number()`, or `FilterValue.boolean()` for typed filter values. Maximum 5 |
3023
- | `sessionTimeout` | `Duration` | No | Duration of inactivity before a session is considered complete (1-1440 minutes). Default: `Duration.minutes(15)` |
3024
- | `tags` | `{ [key: string]: string }` | No | Tags for the evaluation configuration |
3025
-
3026
- ### Basic Online Evaluation Creation
3027
-
3028
- Create an online evaluation configuration with built-in evaluators:
3029
-
3030
- ```typescript fixture=default
3031
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'MyEvaluation', {
3032
- onlineEvaluationConfigName: 'my_evaluation',
3033
- evaluators: [
3034
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3035
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.CORRECTNESS),
3036
- ],
3037
- dataSource: agentcore.DataSourceConfig.fromCloudWatchLogs({
3038
- logGroupNames: ['/aws/bedrock-agentcore/my-agent'],
3039
- serviceNames: ['my-agent.default'],
3040
- }),
3041
- });
3042
- ```
3043
-
3044
- ### Built-in Evaluators
3045
-
3046
- Amazon Bedrock AgentCore provides 13 built-in evaluators that assess different aspects of agent performance:
3047
-
3048
- **Session-Level Evaluators:**
3049
-
3050
- - `GOAL_SUCCESS_RATE` - Evaluates whether the conversation successfully meets the user's goals
3051
-
3052
- **Trace-Level Evaluators:**
3053
-
3054
- - `HELPFULNESS` - How useful and valuable the agent's response is
3055
- - `CORRECTNESS` - Whether the information is factually accurate
3056
- - `FAITHFULNESS` - Whether the response is faithful to the provided context
3057
- - `HARMFULNESS` - Whether the response contains harmful content
3058
- - `STEREOTYPING` - Detects content that makes generalizations about individuals or groups
3059
- - `REFUSAL` - Whether the agent appropriately refuses harmful requests
3060
- - `COHERENCE` - Whether the response is logically coherent
3061
- - `RESPONSE_RELEVANCE` - Whether the response appropriately addresses the user's query
3062
- - `CONCISENESS` - Whether the response is appropriately concise
3063
- - `INSTRUCTION_FOLLOWING` - How well the agent follows system instructions
3064
-
3065
- **Tool Call-Level Evaluators:**
3066
-
3067
- - `TOOL_SELECTION_ACCURACY` - Whether the agent selected the appropriate tool
3068
- - `TOOL_PARAMETER_ACCURACY` - How accurately the agent extracts parameters from user queries
3069
-
3070
- ```typescript fixture=default
3071
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'ComprehensiveEval', {
3072
- onlineEvaluationConfigName: 'comprehensive_evaluation',
3073
- evaluators: [
3074
- // Session level
3075
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.GOAL_SUCCESS_RATE),
3076
- // Trace level - quality
3077
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3078
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.CORRECTNESS),
3079
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.COHERENCE),
3080
- // Trace level - safety
3081
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HARMFULNESS),
3082
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.STEREOTYPING),
3083
- // Tool call level
3084
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.TOOL_SELECTION_ACCURACY),
3085
- ],
3086
- dataSource: agentcore.DataSourceConfig.fromCloudWatchLogs({
3087
- logGroupNames: ['/aws/bedrock-agentcore/my-agent'],
3088
- serviceNames: ['my-agent.default'],
3089
- }),
3090
- });
3091
- ```
3092
-
3093
- ### Custom Evaluators
3094
-
3095
- Custom evaluators let you define evaluation logic tailored to your specific use cases. You can create custom evaluators using two strategies:
3096
-
3097
- - **LLM-as-a-Judge**: Uses a foundation model with custom instructions and a rating scale to assess agent performance.
3098
- - **Code-based**: Uses a Lambda function for custom evaluation logic.
3099
-
3100
- | Property | Type | Required | Description |
3101
- |---|---|---|---|
3102
- | `evaluatorName` | `string` | Yes | Name of the evaluator. Must start with a letter, a-z, A-Z, 0-9, _ only. Maximum 48 characters |
3103
- | `evaluatorConfig` | `EvaluatorConfig` | Yes | Configuration defining how the evaluator assesses performance |
3104
- | `level` | `EvaluationLevel` | Yes | The level at which the evaluator operates: `TOOL_CALL`, `TRACE`, or `SESSION` |
3105
- | `description` | `string` | No | Description of the evaluator. Maximum 200 characters |
3106
-
3107
- #### LLM-as-a-Judge Evaluator
3108
-
3109
- Create a custom evaluator that uses a foundation model to assess agent performance:
3110
-
3111
- ```typescript fixture=default
3112
- // LLM-as-a-Judge with categorical rating scale
3113
- const categoricalEvaluator = new agentcore.Evaluator(this, 'CategoricalEvaluator', {
3114
- evaluatorName: 'domain_accuracy_evaluator',
3115
- level: agentcore.EvaluationLevel.SESSION,
3116
- description: 'Evaluates domain-specific accuracy of agent responses',
3117
- evaluatorConfig: agentcore.EvaluatorConfig.llmAsAJudge({
3118
- instructions: 'Evaluate whether the agent response is accurate within the healthcare domain.',
3119
- modelId: 'us.anthropic.claude-sonnet-4-6',
3120
- ratingScale: agentcore.EvaluatorRatingScale.categorical([
3121
- { label: 'Accurate', definition: 'The response contains factually correct healthcare information.' },
3122
- { label: 'Inaccurate', definition: 'The response contains incorrect or misleading healthcare information.' },
3123
- ]),
3124
- }),
3125
- });
3126
-
3127
- // LLM-as-a-Judge with numerical rating scale and inference config
3128
- const numericalEvaluator = new agentcore.Evaluator(this, 'NumericalEvaluator', {
3129
- evaluatorName: 'response_quality_evaluator',
3130
- level: agentcore.EvaluationLevel.TRACE,
3131
- evaluatorConfig: agentcore.EvaluatorConfig.llmAsAJudge({
3132
- instructions: 'Rate the overall quality of the agent response on a scale of 1 to 5.',
3133
- modelId: 'us.anthropic.claude-sonnet-4-6',
3134
- ratingScale: agentcore.EvaluatorRatingScale.numerical([
3135
- { label: 'Poor', definition: 'Inadequate response.', value: 1 },
3136
- { label: 'Below Average', definition: 'Partially addresses the query.', value: 2 },
3137
- { label: 'Average', definition: 'Adequately addresses the query.', value: 3 },
3138
- { label: 'Good', definition: 'Well-structured and accurate response.', value: 4 },
3139
- { label: 'Excellent', definition: 'Outstanding response exceeding expectations.', value: 5 },
3140
- ]),
3141
- inferenceConfig: {
3142
- maxTokens: 1024,
3143
- temperature: 0.1,
3144
- },
3145
- }),
3146
- });
3147
- ```
3148
-
3149
- The `modelId` accepts standard Bedrock model IDs and cross-region inference profile IDs with region prefixes (e.g., `us.`, `eu.`, `global.`).
3150
-
3151
- > **Instructions placeholders:** Instructions must contain placeholders appropriate for the evaluation level (e.g., `{context}`, `{available_tools}` for SESSION level). Evaluators using reference-input placeholders (e.g., `{expected_tool_trajectory}`, `{assertions}`) are only compatible with on-demand evaluation, not online evaluation. See the [custom evaluators documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/custom-evaluators.html) for allowed placeholders per level.
3152
-
3153
- #### Code-Based Evaluator
3154
-
3155
- Create a custom evaluator that uses a Lambda function for evaluation logic:
3156
-
3157
- ```typescript fixture=default
3158
- declare const evalFunction: lambda.IFunction;
3159
-
3160
- const codeEvaluator = new agentcore.Evaluator(this, 'CodeEvaluator', {
3161
- evaluatorName: 'custom_code_evaluator',
3162
- level: agentcore.EvaluationLevel.TOOL_CALL,
3163
- description: 'Evaluates tool call accuracy using custom logic',
3164
- evaluatorConfig: agentcore.EvaluatorConfig.codeBased({
3165
- lambdaFunction: evalFunction,
3166
- timeout: cdk.Duration.seconds(30),
3167
- }),
3168
- });
3169
- ```
3170
-
3171
- For code-based evaluators, the construct automatically grants the `bedrock-agentcore.amazonaws.com` service principal permission to invoke the Lambda function, scoped to the specific evaluator resource with `aws:SourceAccount` and `aws:SourceArn` conditions for confused deputy prevention.
3172
-
3173
- #### Using Custom Evaluators with Online Evaluation
3174
-
3175
- Custom evaluators are used in `OnlineEvaluationConfig` via `EvaluatorReference.custom()`, alongside built-in evaluators:
3176
-
3177
- ```typescript fixture=default
3178
- declare const customEvaluator: agentcore.Evaluator;
3179
-
3180
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'MixedEvaluation', {
3181
- onlineEvaluationConfigName: 'mixed_evaluation',
3182
- evaluators: [
3183
- // Built-in evaluators
3184
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3185
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.CORRECTNESS),
3186
- // Custom evaluator
3187
- agentcore.EvaluatorReference.custom(customEvaluator),
3188
- ],
3189
- dataSource: agentcore.DataSourceConfig.fromCloudWatchLogs({
3190
- logGroupNames: ['/aws/bedrock-agentcore/my-agent'],
3191
- serviceNames: ['my-agent.default'],
3192
- }),
3193
- });
3194
- ```
3195
-
3196
- ### Data Source Configuration
3197
-
3198
- Online evaluation supports two types of data sources:
3199
-
3200
- **AgentCore Runtime Data Source (Recommended):**
3201
-
3202
- For runtimes created within your CDK app, use `fromAgentRuntimeEndpoint()` which automatically derives the CloudWatch log group and service name:
3203
-
3204
- ```typescript fixture=default
3205
- const repository = new ecr.Repository(this, 'TestRepository', {
3206
- repositoryName: 'test-agent-runtime',
3207
- });
3208
-
3209
- const runtime = new agentcore.Runtime(this, 'MyRuntime', {
3210
- runtimeName: 'my_agent',
3211
- agentRuntimeArtifact: agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, 'v1.0.0'),
3212
- });
3213
-
3214
- // Using default endpoint (simplest)
3215
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'RuntimeEval', {
3216
- onlineEvaluationConfigName: 'runtime_evaluation',
3217
- evaluators: [
3218
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3219
- ],
3220
- dataSource: agentcore.DataSourceConfig.fromAgentRuntimeEndpoint(runtime),
3221
- });
3222
458
  ```
3223
459
 
3224
- You can also specify a specific endpoint:
3225
-
3226
- ```typescript fixture=default
3227
- declare const runtime: agentcore.Runtime;
3228
-
3229
- // Using a specific endpoint construct
3230
- const prodEndpoint = runtime.addEndpoint('PROD');
3231
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'ProdEval', {
3232
- onlineEvaluationConfigName: 'prod_evaluation',
3233
- evaluators: [
3234
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.CORRECTNESS),
3235
- ],
3236
- dataSource: agentcore.DataSourceConfig.fromAgentRuntimeEndpoint(runtime, prodEndpoint),
3237
- });
460
+ ## Using Policy with Stable Gateway
3238
461
 
3239
- // Or using endpoint name as string
3240
- const stagingEval = new agentcore.OnlineEvaluationConfig(this, 'StagingEval', {
3241
- onlineEvaluationConfigName: 'staging_evaluation',
3242
- evaluators: [
3243
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.CORRECTNESS),
3244
- ],
3245
- dataSource: agentcore.DataSourceConfig.fromAgentRuntimeEndpointName(runtime, 'STAGING'),
3246
- });
3247
- ```
462
+ Since Gateway is now in `aws-cdk-lib/aws-bedrockagentcore` but Policy remains in alpha, use the L1 escape hatch to associate a policy engine with a stable gateway:
3248
463
 
3249
- **CloudWatch Logs Data Source:**
464
+ > Proper L2 integration will be added in a future update.
3250
465
 
3251
- For external agents or when you need to specify log groups directly:
466
+ ```ts fixture=policy
467
+ import * as agentcore from 'aws-cdk-lib/aws-bedrockagentcore';
468
+ import * as agentcoreAlpha from '@aws-cdk/aws-bedrock-agentcore-alpha';
3252
469
 
3253
- ```typescript fixture=default
3254
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'CloudWatchEval', {
3255
- onlineEvaluationConfigName: 'cloudwatch_evaluation',
3256
- evaluators: [
3257
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3258
- ],
3259
- dataSource: agentcore.DataSourceConfig.fromCloudWatchLogs({
3260
- logGroupNames: [
3261
- '/aws/bedrock-agentcore/agent1',
3262
- '/aws/bedrock-agentcore/agent2',
3263
- ],
3264
- serviceNames: ['agent1.default'],
3265
- }),
470
+ // Create policy engine (alpha)
471
+ const policyEngine = new agentcoreAlpha.PolicyEngine(this, 'Engine', {
472
+ policyEngineName: 'my_engine',
3266
473
  });
3267
- ```
3268
-
3269
- ### Sampling and Filtering
3270
474
 
3271
- Configure sampling percentage and filters to control which traces are evaluated:
3272
-
3273
- ```typescript fixture=default
3274
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'FilteredEval', {
3275
- onlineEvaluationConfigName: 'filtered_evaluation',
3276
- evaluators: [
3277
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3278
- ],
3279
- dataSource: agentcore.DataSourceConfig.fromCloudWatchLogs({
3280
- logGroupNames: ['/aws/bedrock-agentcore/my-agent'],
3281
- serviceNames: ['my-agent.default'],
3282
- }),
3283
- // Sample 25% of traces
3284
- samplingPercentage: 25,
3285
- // Only evaluate traces matching these filters
3286
- filters: [
3287
- {
3288
- key: 'user.region',
3289
- operator: agentcore.FilterOperator.EQUAL,
3290
- value: agentcore.FilterValue.string('us-east-1'),
3291
- },
3292
- {
3293
- key: 'session.duration',
3294
- operator: agentcore.FilterOperator.GREATER_THAN,
3295
- value: agentcore.FilterValue.number(60),
3296
- },
3297
- ],
3298
- // Consider sessions complete after 30 minutes of inactivity
3299
- sessionTimeout: cdk.Duration.minutes(30),
475
+ // Create gateway (stable)
476
+ const gateway = new agentcore.Gateway(this, 'Gateway', {
477
+ gatewayName: 'my-gateway',
3300
478
  });
3301
- ```
3302
-
3303
- ### Online Evaluation with Custom Execution Role
3304
479
 
3305
- Provide a custom IAM role for the evaluation execution:
3306
-
3307
- ```typescript fixture=default
3308
- const executionRole = new iam.Role(this, 'EvaluationRole', {
3309
- assumedBy: new iam.ServicePrincipal('bedrock-agentcore.amazonaws.com'),
3310
- description: 'Custom role for online evaluation',
3311
- });
480
+ // Wire policy engine to gateway via the L1 construct
481
+ const cfnGateway = gateway.node.defaultChild as agentcore.CfnGateway;
482
+ cfnGateway.policyEngineConfiguration = {
483
+ arn: policyEngine.policyEngineArn,
484
+ mode: agentcoreAlpha.PolicyEngineMode.ENFORCE.value,
485
+ };
3312
486
 
3313
- // Add required permissions
3314
- executionRole.addToPolicy(new iam.PolicyStatement({
3315
- actions: [
3316
- 'logs:DescribeLogGroups',
3317
- 'logs:GetQueryResults',
3318
- 'logs:StartQuery',
3319
- ],
3320
- resources: ['arn:aws:logs:*:*:log-group:/aws/bedrock-agentcore/*'],
487
+ // Grant evaluate permissions to the gateway role
488
+ gateway.role.addToPrincipalPolicy(new iam.PolicyStatement({
489
+ actions: ['bedrock-agentcore:GetPolicyEngine'],
490
+ resources: [policyEngine.policyEngineArn],
491
+ }));
492
+ gateway.role.addToPrincipalPolicy(new iam.PolicyStatement({
493
+ actions: ['bedrock-agentcore:AuthorizeAction', 'bedrock-agentcore:PartiallyAuthorizeActions'],
494
+ resources: [policyEngine.policyEngineArn, gateway.gatewayArn],
3321
495
  }));
3322
-
3323
- const evaluation = new agentcore.OnlineEvaluationConfig(this, 'CustomRoleEval', {
3324
- onlineEvaluationConfigName: 'custom_role_evaluation',
3325
- evaluators: [
3326
- agentcore.EvaluatorReference.builtin(agentcore.BuiltinEvaluator.HELPFULNESS),
3327
- ],
3328
- dataSource: agentcore.DataSourceConfig.fromCloudWatchLogs({
3329
- logGroupNames: ['/aws/bedrock-agentcore/my-agent'],
3330
- serviceNames: ['my-agent.default'],
3331
- }),
3332
- executionRole: executionRole,
3333
- });
3334
- ```
3335
-
3336
- ### Online Evaluation IAM Permissions
3337
-
3338
- Grant IAM permissions to manage or read evaluation configurations:
3339
-
3340
- ```typescript fixture=default
3341
- declare const evaluation: agentcore.OnlineEvaluationConfig;
3342
- declare const role: iam.IRole;
3343
-
3344
- // Grant specific permissions
3345
- evaluation.grant(role,
3346
- 'bedrock-agentcore:GetOnlineEvaluationConfig',
3347
- 'bedrock-agentcore:UpdateOnlineEvaluationConfig',
3348
- );
3349
496
  ```