UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

1,033 lines (813 loc) 139 kB
# Amazon Bedrock AgentCore Construct Library | **Language** | **Package** | | :--------------------------------------------------------------------------------------------- | --------------------------------------- | | ![Typescript Logo](https://docs.aws.amazon.com/cdk/api/latest/img/typescript32.png) TypeScript | `aws-cdk-lib/aws-bedrockagentcore` | [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. 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. > **Note:** Users need to ensure their CDK deployment role has the `iam:CreateServiceLinkedRole` permission for AgentCore service-linked roles. ## Table of contents - [Amazon Bedrock AgentCore Construct Library](#amazon-bedrock-agentcore-construct-library) - [Table of contents](#table-of-contents) - [AgentCore Runtime](#agentcore-runtime) - [Runtime Endpoints](#runtime-endpoints) - [AgentCore Runtime Properties](#agentcore-runtime-properties) - [Runtime Endpoint Properties](#runtime-endpoint-properties) - [Creating a Runtime](#creating-a-runtime) - [Option 1: Use an existing image in ECR](#option-1-use-an-existing-image-in-ecr) - [Option 2: Use a local asset](#option-2-use-a-local-asset) - [Option 3: Use direct code deployment](#option-3-use-direct-code-deployment) - [Option 4: Use an ECR container image URI](#option-4-use-an-ecr-container-image-uri) - [Granting Permissions to Invoke Bedrock Models or Inference Profiles](#granting-permissions-to-invoke-bedrock-models-or-inference-profiles) - [Runtime Versioning](#runtime-versioning) - [Managing Endpoints and Versions](#managing-endpoints-and-versions) - [Step 1: Initial Deployment](#step-1-initial-deployment) - [Step 2: Creating Custom Endpoints](#step-2-creating-custom-endpoints) - [Step 3: Runtime Update Deployment](#step-3-runtime-update-deployment) - [Step 4: Testing with Staging Endpoints](#step-4-testing-with-staging-endpoints) - [Step 5: Promoting to Production](#step-5-promoting-to-production) - [Creating Standalone Runtime Endpoints](#creating-standalone-runtime-endpoints) - [Example: Creating an endpoint for an existing runtime](#example-creating-an-endpoint-for-an-existing-runtime) - [Runtime Authentication Configuration](#runtime-authentication-configuration) - [IAM Authentication (Default)](#iam-authentication-default) - [Cognito Authentication](#cognito-authentication) - [JWT Authentication](#jwt-authentication) - [OAuth Authentication](#oauth-authentication) - [Using a Custom IAM Role](#using-a-custom-iam-role) - [Runtime Network Configuration](#runtime-network-configuration) - [Public Network Mode (Default)](#public-network-mode-default) - [VPC Network Mode](#vpc-network-mode) - [Managing Security Groups with VPC Configuration](#managing-security-groups-with-vpc-configuration) - [Runtime IAM Permissions](#runtime-iam-permissions) - [Other configuration](#other-configuration) - [Lifecycle configuration](#lifecycle-configuration) - [Request header configuration](#request-header-configuration) - [Browser](#browser) - [Browser Network modes](#browser-network-modes) - [Browser Properties](#browser-properties) - [Basic Browser Creation](#basic-browser-creation) - [Browser with Tags](#browser-with-tags) - [Browser with VPC](#browser-with-vpc) - [Browser with Recording Configuration](#browser-with-recording-configuration) - [Browser with Custom Execution Role](#browser-with-custom-execution-role) - [Browser with S3 Recording and Permissions](#browser-with-s3-recording-and-permissions) - [Browser with Browser signing](#browser-with-browser-signing) - [Browser IAM Permissions](#browser-iam-permissions) - [Code Interpreter](#code-interpreter) - [Code Interpreter Network Modes](#code-interpreter-network-modes) - [Code Interpreter Properties](#code-interpreter-properties) - [Basic Code Interpreter Creation](#basic-code-interpreter-creation) - [Code Interpreter with VPC](#code-interpreter-with-vpc) - [Code Interpreter with Sandbox Network Mode](#code-interpreter-with-sandbox-network-mode) - [Code Interpreter with Custom Execution Role](#code-interpreter-with-custom-execution-role) - [Code Interpreter IAM Permissions](#code-interpreter-iam-permissions) - [Code interpreter with tags](#code-interpreter-with-tags) - [Gateway](#gateway) - [Gateway Properties](#gateway-properties) - [Basic Gateway Creation](#basic-gateway-creation) - [Protocol configuration](#protocol-configuration) - [Inbound authorization](#inbound-authorization) - [Gateway with KMS Encryption](#gateway-with-kms-encryption) - [Gateway with Custom Execution Role](#gateway-with-custom-execution-role) - [Gateway IAM Permissions](#gateway-iam-permissions) - [Gateway Target](#gateway-target) - [Gateway Target Properties](#gateway-target-properties) - [Targets types](#targets-types) - [Understanding Tool Naming](#understanding-tool-naming) - [Tools schema For Lambda target](#tools-schema-for-lambda-target) - [Api schema For OpenAPI and Smithy target](#api-schema-for-openapi-and-smithy-target) - [Outbound auth](#outbound-auth) - [Token Vault credential providers](#token-vault-credential-providers) - [Workload identities](#workload-identities) - [Basic Gateway Target Creation](#basic-gateway-target-creation) - [Using addTarget methods (Recommended)](#using-addtarget-methods-recommended) - [Using static factory methods](#using-static-factory-methods) - [Advanced Usage: Direct Configuration for gateway target](#advanced-usage-direct-configuration-for-gateway-target) - [Configuration Factory Methods](#configuration-factory-methods) - [Example: Lambda Target with Custom Configuration](#example-lambda-target-with-custom-configuration) - [Gateway Target IAM Permissions](#gateway-target-iam-permissions) - [Memory](#memory) - [Memory Properties](#memory-properties) - [Basic Memory Creation](#basic-memory-creation) - [LTM Memory Extraction Stategies](#ltm-memory-extraction-stategies) - [Memory with Built-in Strategies](#memory-with-built-in-strategies) - [Memory with custom Strategies](#memory-with-custom-strategies) - [Memory with Custom Execution Role](#memory-with-custom-execution-role) - [Memory with self-managed Strategies](#memory-with-self-managed-strategies) - [Memory Strategy Methods](#memory-strategy-methods) - [Online Evaluation](#online-evaluation) - [Online Evaluation Properties](#online-evaluation-properties) - [Basic Online Evaluation Creation](#basic-online-evaluation-creation) - [Built-in Evaluators](#built-in-evaluators) - [Custom Evaluators](#custom-evaluators) - [LLM-as-a-Judge Evaluator](#llm-as-a-judge-evaluator) - [Code-Based Evaluator](#code-based-evaluator) - [Using Custom Evaluators with Online Evaluation](#using-custom-evaluators-with-online-evaluation) - [Data Source Configuration](#data-source-configuration) - [Sampling and Filtering](#sampling-and-filtering) - [Online Evaluation with Custom Execution Role](#online-evaluation-with-custom-execution-role) - [Online Evaluation IAM Permissions](#online-evaluation-iam-permissions) ## AgentCore Runtime The AgentCore Runtime construct enables you to deploy containerized agents on Amazon Bedrock AgentCore. This L2 construct simplifies runtime creation just pass your ECR repository name and the construct handles all the configuration with sensible defaults. ### Runtime Endpoints Endpoints provide a stable way to invoke specific versions of your agent runtime, enabling controlled deployments across different environments. When you create an agent runtime, Amazon Bedrock AgentCore automatically creates a "DEFAULT" endpoint which always points to the latest version of runtime. You can create additional endpoints in two ways: 1. **Using Runtime.addEndpoint()** - Convenient method when creating endpoints alongside the runtime. 2. **Using RuntimeEndpoint** - Flexible approach for existing runtimes. For example, you might keep a "production" endpoint on a stable version while testing newer versions through a "staging" endpoint. This separation allows you to test changes thoroughly before promoting them to production by simply updating the endpoint to point to the newer version. ### AgentCore Runtime Properties | Name | Type | Required | Description | |------|------|----------|-------------| | `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 | | `agentRuntimeArtifact` | `AgentRuntimeArtifact` | Yes | The artifact configuration for the agent runtime containing the container configuration with ECR URI | | `executionRole` | `iam.IRole` | No | The IAM role that provides permissions for the agent runtime. If not provided, a role will be created automatically | | `networkConfiguration` | `NetworkConfiguration` | No | Network configuration for the agent runtime. Defaults to `RuntimeNetworkConfiguration.usingPublicNetwork()` | | `description` | `string` | No | Optional description for the agent runtime | | `protocolConfiguration` | `ProtocolType` | No | Protocol configuration for the agent runtime. Defaults to `ProtocolType.HTTP` | | `authorizerConfiguration` | `RuntimeAuthorizerConfiguration` | No | Authorizer configuration for the agent runtime. Use `RuntimeAuthorizerConfiguration` static methods to create configurations for IAM, Cognito, JWT, or OAuth authentication | | `environmentVariables` | `{ [key: string]: string }` | No | Environment variables for the agent runtime. Maximum 50 environment variables | | `tags` | `{ [key: string]: string }` | No | Tags for the agent runtime. A list of key:value pairs of tags to apply to this Runtime resource | | `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 | | `requestHeaderConfiguration` | RequestHeaderConfiguration | No | Configuration for HTTP request headers that will be passed through to the runtime. Defaults to no configuration | ### Runtime Endpoint Properties | Name | Type | Required | Description | |------|------|----------|-------------| | `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 | | `agentRuntimeId` | `string` | Yes | The Agent Runtime ID for this endpoint | | `agentRuntimeVersion` | `string` | Yes | The Agent Runtime version for this endpoint. Must be between 1 and 5 characters long.| | `description` | `string` | No | Optional description for the runtime endpoint | | `tags` | `{ [key: string]: string }` | No | Tags for the runtime endpoint | ### Creating a Runtime #### Option 1: Use an existing image in ECR Reference an image available within ECR. ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); // The runtime by default create ECR permission only for the repository available in the account the stack is being deployed const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // Create runtime using the built image const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact }); ``` #### Option 2: Use a local asset Reference a local directory containing a Dockerfile. Images are built from a local Docker context directory (with a Dockerfile), uploaded to Amazon Elastic Container Registry (ECR) by the CDK toolkit,and can be naturally referenced in your CDK app. ```typescript fixture=default const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromAsset( path.join(__dirname, "path to agent dockerfile directory") ); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, }); ``` #### Option 3: Use direct code deployment 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. 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. 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. For more information, please refer to the [documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-get-started-code-deploy.html). ```typescript fixture=default // S3 bucket containing the agent core const codeBucket = new s3.Bucket(this, "AgentCode", { bucketName: "my-code-bucket", removalPolicy: RemovalPolicy.DESTROY, // For demo purposes }); // the bucket above needs to contain the agent code const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromS3( { bucketName: codeBucket.bucketName, objectKey: 'deployment_package.zip', }, agentcore.AgentCoreRuntime.PYTHON_3_12, ['opentelemetry-instrument', 'main.py'] ); const runtimeInstance = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, }); ``` Alternatively, you can use local code assets that will be automatically packaged and uploaded to a CDK-managed S3 bucket: ```typescript fixture=default const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromCodeAsset({ path: path.join(__dirname, 'path/to/agent/code'), runtime: agentcore.AgentCoreRuntime.PYTHON_3_12, entrypoint: ['opentelemetry-instrument', 'main.py'], }); const runtimeInstance = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, }); ``` #### Option 4: Use an ECR container image URI 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. ```typescript fixture=default // Direct URI reference const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromImageUri( "123456789012.dkr.ecr.us-east-1.amazonaws.com/my-agent:v1.0.0" ); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, }); ``` You can also use CloudFormation parameters or references: ```typescript fixture=default // Using a CloudFormation parameter const imageUriParam = new cdk.CfnParameter(this, "ImageUri", { type: "String", description: "Container image URI for the agent runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromImageUri( imageUriParam.valueAsString ); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, }); ``` ### Granting Permissions to Invoke Bedrock Models or Inference Profiles To grant the runtime permissions to invoke Bedrock models or inference profiles: ```typescript fixture=default // Note: This example uses aws-cdk-lib/aws-bedrock for foundation model references declare const runtime: agentcore.Runtime; // Define the Bedrock Foundation Model const model = bedrock.FoundationModel.fromFoundationModelId(this, 'Model', bedrock.FoundationModelIdentifier.ANTHROPIC_CLAUDE_3_7_SONNET_20250219_V1_0, ); // Grant the runtime permissions to invoke the model runtime.role.addToPrincipalPolicy(new iam.PolicyStatement({ actions: ['bedrock:InvokeModel'], resources: [model.modelArn], })); ``` ### Runtime Versioning Amazon Bedrock AgentCore automatically manages runtime versioning to ensure safe deployments and rollback capabilities. When you create an agent runtime, AgentCore automatically creates version 1 (V1). Each subsequent update to the runtime configuration (such as updating the container image, modifying network settings, or changing protocol configurations) creates a new immutable version. These versions contain complete, self-contained configurations that can be referenced by endpoints, allowing you to maintain different versions for different environments or gradually roll out updates. #### Managing Endpoints and Versions Amazon Bedrock AgentCore automatically manages runtime versioning to provide safe deployments and rollback capabilities. You can follow the steps below to understand how to use versioning with runtime for controlled deployments across different environments. ##### Step 1: Initial Deployment When you first create an agent runtime, AgentCore automatically creates Version 1 of your runtime. At this point, a DEFAULT endpoint is automatically created that points to Version 1. This DEFAULT endpoint serves as the main access point for your runtime. ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"), }); ``` ##### Step 2: Creating Custom Endpoints After the initial deployment, you can create additional endpoints for different environments. For example, you might create a "production" endpoint that explicitly points to Version 1. This allows you to maintain stable access points for specific environments while keeping the flexibility to test newer versions elsewhere. ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"), }); const prodEndpoint = runtime.addEndpoint("production", { version: "1", description: "Stable production endpoint - pinned to v1" }); ``` ##### Step 3: Runtime Update Deployment When you update the runtime configuration (such as updating the container image, modifying network settings, or changing protocol configurations), AgentCore automatically creates a new version (Version 2). Upon this update: - Version 2 is created automatically with the new configuration - The DEFAULT endpoint automatically updates to point to Version 2 - Any explicitly pinned endpoints (like the production endpoint) remain on their specified versions ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifactNew = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v2.0.0"); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifactNew, }); ``` ##### Step 4: Testing with Staging Endpoints Once Version 2 exists, you can create a staging endpoint that points to the new version. This staging endpoint allows you to test the new version in a controlled environment before promoting it to production. This separation ensures that production traffic continues to use the stable version while you validate the new version. ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifactNew = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v2.0.0"); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifactNew, }); const stagingEndpoint = runtime.addEndpoint("staging", { version: "2", description: "Staging environment for testing new version" }); ``` ##### Step 5: Promoting to Production After thoroughly testing the new version through the staging endpoint, you can update the production endpoint to point to Version 2. This controlled promotion process ensures that you can validate changes before they affect production traffic. ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifactNew = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v2.0.0"); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifactNew, }); const prodEndpoint = runtime.addEndpoint("production", { version: "2", // New version added here description: "Stable production endpoint" }); ``` ### Creating Standalone Runtime Endpoints RuntimeEndpoint can also be created as a standalone resource. #### Example: Creating an endpoint for an existing runtime ```typescript fixture=default // Reference an existing runtime by its ID const existingRuntimeId = "abc123-runtime-id"; // The ID of an existing runtime // Create a standalone endpoint const endpoint = new agentcore.RuntimeEndpoint(this, "MyEndpoint", { endpointName: "production", agentRuntimeId: existingRuntimeId, agentRuntimeVersion: "1", // Specify which version to use description: "Production endpoint for existing runtime" }); ``` ### Runtime Authentication Configuration 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. #### IAM Authentication (Default) IAM authentication is the default mode, when no authorizerConfiguration is set then the underlying service use IAM. #### Cognito Authentication To configure AWS Cognito User Pool authentication: ```typescript fixture=default declare const userPool: cognito.UserPool; declare const userPoolClient: cognito.UserPoolClient; declare const anotherUserPoolClient: cognito.UserPoolClient; const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // Optional: Create custom claims for additional validation const customClaims = [ agentcore.RuntimeCustomClaim.withStringValue('department', 'engineering'), agentcore.RuntimeCustomClaim.withStringArrayValue('roles', ['admin'], agentcore.CustomClaimOperator.CONTAINS), agentcore.RuntimeCustomClaim.withStringArrayValue('permissions', ['read', 'write'], agentcore.CustomClaimOperator.CONTAINS_ANY), ]; const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingCognito( userPool, // User Pool (required) [userPoolClient, anotherUserPoolClient], // User Pool Clients ["audience1"], // Allowed Audiences (optional) ["read", "write"], // Allowed Scopes (optional) customClaims, // Custom claims (optional) - see Custom Claims Validation section ), }); ``` You can configure: - User Pool: The Cognito User Pool that issues JWT tokens - User Pool Clients: One or more Cognito User Pool App Clients that are allowed to access the runtime - 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 - Allowed scopes: Allow access only if the token contains at least one of the required scopes configured here - Custom claims: A set of rules to match specific claims in the incoming token against predefined values for validating JWT tokens #### JWT Authentication To configure custom JWT authentication with your own OpenID Connect (OIDC) provider: ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingJWT( "https://example.com/.well-known/openid-configuration", // Discovery URL (required) ["client1", "client2"], // Allowed Client IDs (optional) ["audience1"], // Allowed Audiences (optional) ["read", "write"], // Allowed Scopes (optional) // Custom claims (optional) - see Custom Claims Validation section below ), }); ``` You can configure: - 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. - 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. - 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. - Allowed scopes: Allow access only if the token contains at least one of the required scopes configured here. - Custom claims: A set of rules to match specific claims in the incoming token against predefined values for validating JWT tokens. **Note**: The discovery URL must end with `/.well-known/openid-configuration`. ##### Custom Claims Validation 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: ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // String claim - validates that the claim exactly equals the specified value // Uses EQUALS operator automatically const departmentClaim = agentcore.RuntimeCustomClaim.withStringValue('department', 'engineering'); // String array claim with CONTAINS operator (default) // Validates that the claim array contains a specific string value // IMPORTANT: CONTAINS requires exactly one value in the array parameter const rolesClaim = agentcore.RuntimeCustomClaim.withStringArrayValue('roles', ['admin']); // String array claim with CONTAINS_ANY operator // Validates that the claim array contains at least one of the specified values // Use this when you want to check for multiple possible values const permissionsClaim = agentcore.RuntimeCustomClaim.withStringArrayValue( 'permissions', ['read', 'write'], agentcore.CustomClaimOperator.CONTAINS_ANY ); // Use custom claims in authorizer configuration const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingJWT( "https://example.com/.well-known/openid-configuration", ["client1", "client2"], ["audience1"], ["read", "write"], [departmentClaim, rolesClaim, permissionsClaim] // Custom claims ), }); ``` **Custom Claim Rules**: - **String claims**: Must use the `EQUALS` operator (automatically set). The claim value must exactly match the specified string. - **String array claims**: Can use `CONTAINS` (default) or `CONTAINS_ANY` operators: - **`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'`. - **`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'`. **Example Use Cases**: - Use `CONTAINS` when you need to verify a user has a specific role: `RuntimeCustomClaim.withStringArrayValue('roles', ['admin'])` - Use `CONTAINS_ANY` when you need to verify a user has any of several permissions: `RuntimeCustomClaim.withStringArrayValue('permissions', ['read', 'write'], CustomClaimOperator.CONTAINS_ANY)` #### OAuth Authentication To configure OAuth 2.0 authentication: ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, authorizerConfiguration: agentcore.RuntimeAuthorizerConfiguration.usingOAuth( "https://github.com/.well-known/openid-configuration", // Discovery URL (required) "oauth_client_123", // OAuth Client ID (required) ["audience1"], // Allowed Audiences (optional) ["openid", "profile"], // Allowed Scopes (optional) // Custom claims (optional) - see Custom Claims Validation section ), }); ``` #### Using a Custom IAM Role Instead of using the auto-created execution role, you can provide your own IAM role with specific permissions: 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. ### Runtime Network Configuration The AgentCore Runtime supports two network modes for deployment: #### Public Network Mode (Default) By default, runtimes are deployed in PUBLIC network mode, which provides internet access suitable for less sensitive or open-use scenarios: ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // Explicitly using public network (this is the default) const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, networkConfiguration: agentcore.RuntimeNetworkConfiguration.usingPublicNetwork(), }); ``` #### VPC Network Mode For enhanced security and network isolation, you can deploy your runtime within a VPC: ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // Create or use an existing VPC const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2, }); // Configure runtime with VPC const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, networkConfiguration: agentcore.RuntimeNetworkConfiguration.usingVpc(this, { vpc: vpc, vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }, // Optionally specify security groups, or one will be created automatically // securityGroups: [mySecurityGroup], }), }); ``` #### Managing Security Groups with VPC Configuration When using VPC mode, the Runtime implements `ec2.IConnectable`, allowing you to manage network access using the `connections` property: ```typescript fixture=default const vpc = new ec2.Vpc(this, 'MyVpc', { maxAzs: 2, }); const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // Create runtime with VPC configuration const runtime = new agentcore.Runtime(this, "MyAgentRuntime", { runtimeName: "myAgent", agentRuntimeArtifact: agentRuntimeArtifact, networkConfiguration: agentcore.RuntimeNetworkConfiguration.usingVpc(this, { vpc: vpc, vpcSubnets: { subnetType: ec2.SubnetType.PRIVATE_WITH_EGRESS }, }), }); // Now you can manage network access using the connections property // Allow inbound HTTPS traffic from a specific security group const webServerSecurityGroup = new ec2.SecurityGroup(this, 'WebServerSG', { vpc }); runtime.connections.allowFrom(webServerSecurityGroup, ec2.Port.tcp(443), 'Allow HTTPS from web servers'); // Allow outbound connections to a database const databaseSecurityGroup = new ec2.SecurityGroup(this, 'DatabaseSG', { vpc }); runtime.connections.allowTo(databaseSecurityGroup, ec2.Port.tcp(5432), 'Allow PostgreSQL connection'); // Allow outbound HTTPS to anywhere (for external API calls) runtime.connections.allowToAnyIpv4(ec2.Port.tcp(443), 'Allow HTTPS outbound'); ``` ### Runtime IAM Permissions The Runtime construct provides convenient methods for granting IAM permissions to principals that need to invoke the runtime or manage its execution role. ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); // Create a runtime const runtime = new agentcore.Runtime(this, "MyRuntime", { runtimeName: "my_runtime", agentRuntimeArtifact: agentRuntimeArtifact, }); // Create a Lambda function that needs to invoke the runtime const invokerFunction = new lambda.Function(this, "InvokerFunction", { runtime: lambda.Runtime.PYTHON_3_12, handler: "index.handler", code: lambda.Code.fromInline(` import boto3 def handler(event, context): client = boto3.client('bedrock-agentcore') # Invoke the runtime... `), }); // Grant permission to invoke the runtime directly runtime.grantInvokeRuntime(invokerFunction); // Grant permission to invoke the runtime on behalf of a user // (requires X-Amzn-Bedrock-AgentCore-Runtime-User-Id header) runtime.grantInvokeRuntimeForUser(invokerFunction); // Grant both invoke permissions (most common use case) runtime.grantInvoke(invokerFunction); // Grant specific custom permissions to the runtime's execution role runtime.grant(['bedrock:InvokeModel'], ['arn:aws:bedrock:*:*:*']); // Add a policy statement to the runtime's execution role runtime.addToRolePolicy(new iam.PolicyStatement({ actions: ['s3:GetObject'], resources: ['arn:aws:s3:::my-bucket/*'], })); ``` ### Other configuration #### Lifecycle configuration 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. You can configure: - 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) - 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) For additional information, please refer to the [documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-lifecycle-settings.html). ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); new agentcore.Runtime(this, 'test-runtime', { runtimeName: 'test_runtime', agentRuntimeArtifact: agentRuntimeArtifact, lifecycleConfiguration: { idleRuntimeSessionTimeout: Duration.minutes(10), maxLifetime: Duration.hours(4), }, }); ``` #### Request header configuration 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. For additional information, please refer to the [documentation](https://docs.aws.amazon.com/bedrock-agentcore/latest/devguide/runtime-header-allowlist.html). ```typescript fixture=default const repository = new ecr.Repository(this, "TestRepository", { repositoryName: "test-agent-runtime", }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, "v1.0.0"); new agentcore.Runtime(this, 'test-runtime', { runtimeName: 'test_runtime', agentRuntimeArtifact: agentRuntimeArtifact, requestHeaderConfiguration: { allowlistedHeaders: ['X-Amzn-Bedrock-AgentCore-Runtime-Custom-H1'], }, }); ``` #### Observability configuration 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. You can configure: - tracingEnabled: Enable X-Ray tracing for the runtime - loggingConfigs: Send APPLICATION_LOGS (agent runtime invocations) and USAGE_LOGS (session-level resource consumption) to CloudWatch Logs, S3, or Kinesis Data Firehose 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). ```typescript fixture=default const repository = new ecr.Repository(this, 'TestRepository', { repositoryName: 'test-agent-runtime', }); const agentRuntimeArtifact = agentcore.AgentRuntimeArtifact.fromEcrRepository(repository, 'v1.0.0'); // Create logging destinations const logGroup = new logs.LogGroup(this, 'RuntimeLogGroup'); const logBucket = new s3.Bucket(this, 'RuntimeLogBucket'); const firehoseStream = new firehose.DeliveryStream(this, 'RuntimeLogStream', { destination: new firehose.S3Bucket(logBucket), }); new agentcore.Runtime(this, 'test-runtime', { runtimeName: 'test_runtime', agentRuntimeArtifact: agentRuntimeArtifact, tracingEnabled: true, loggingConfigs: [ { logType: agentcore.LogType.APPLICATION_LOGS, destination: agentcore.LoggingDestination.cloudWatchLogs(logGroup), }, { logType: agentcore.LogType.APPLICATION_LOGS, destination: agentcore.LoggingDestination.s3(logBucket), }, { logType: agentcore.LogType.APPLICATION_LOGS, destination: agentcore.LoggingDestination.firehose(firehoseStream), }, ], }); ``` ## Browser 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. 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) ### Browser Network modes The Browser construct supports the following network modes: 1. **Public Network Mode** (`BrowserNetworkMode.usingPublicNetwork()`) - Default - Allows internet access for web browsing and external API calls - Suitable for scenarios where agents need to interact with publicly available websites - Enables full web browsing capabilities - VPC mode is not supported with this option 2. **VPC (Virtual Private Cloud)** (`BrowserNetworkMode.usingVpc()`) - Select whether to run the browser in a virtual private cloud (VPC). - By configuring VPC connectivity, you enable secure access to private resources such as databases, internal APIs, and services within your VPC. While the VPC itself is mandatory, these are optional: - Subnets - if not provided, CDK will select appropriate subnets from the VPC - Security Groups - if not provided, CDK will create a default security group - Specific subnet selection criteria - you can let CDK choose automatically 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). ### Browser Properties | Name | Type | Required | Description | |------|------|----------|-------------| | `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 | | `description` | `string` | No | Optional description for the browser. Can have up to 200 characters | | `networkConfiguration` | `BrowserNetworkConfiguration` | No | Network configuration for browser. Defaults to PUBLIC network mode | | `recordingConfig` | `RecordingConfig` | No | Recording configuration for browser. Defaults to no recording | | `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 | | `tags` | `{ [key: string]: string }` | No | Tags to apply to the browser resource | | `browserSigning` | BrowserSigning | No | Browser signing configuration. Defaults to DISABLED | ### Basic Browser Creation ```typescript fixture=default // Create a basic browser with public network access const browser = new agentcore.BrowserCustom(this, "MyBrowser", { browserCustomName: "my_browser", description: "A browser for web automation", }); ``` ### Browser with Tags ```typescript fixture=default // Create a browser with custom tags const browser = new agentcore.BrowserCustom(this, "MyBrowser", { browserCustomName: "my_browser", description: "A browser for web automation with tags", networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(), tags: { Environment: "Production", Team: "AI/ML", Project: "AgentCore", }, }); ``` ### Browser with VPC ```typescript fixture=default const browser = new agentcore.BrowserCustom(this, 'BrowserVpcWithRecording', { browserCustomName: 'browser_recording', networkConfiguration: agentcore.BrowserNetworkConfiguration.usingVpc(this, { vpc: new ec2.Vpc(this, 'VPC', { restrictDefaultSecurityGroup: false }), }), }); ``` 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: ```typescript fixture=default const vpc = new ec2.Vpc(this, 'testVPC'); const browser = new agentcore.BrowserCustom(this, 'test-browser', { browserCustomName: 'test_browser', networkConfiguration: agentcore.BrowserNetworkConfiguration.usingVpc(this, { vpc: vpc, }), }); browser.connections.addSecurityGroup(new ec2.SecurityGroup(this, 'AdditionalGroup', { vpc })); ``` 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. ### Browser with Recording Configuration ```typescript fixture=default // Create an S3 bucket for recordings const recordingBucket = new s3.Bucket(this, "RecordingBucket", { bucketName: "my-browser-recordings", removalPolicy: RemovalPolicy.DESTROY, // For demo purposes }); // Create browser with recording enabled const browser = new agentcore.BrowserCustom(this, "MyBrowser", { browserCustomName: "my_browser", description: "Browser with recording enabled", networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(), recordingConfig: { enabled: true, s3Location: { bucketName: recordingBucket.bucketName, objectKey: "browser-recordings/", }, }, }); ``` ### Browser with Custom Execution Role ```typescript fixture=default // Create a custom execution role const executionRole = new iam.Role(this, "BrowserExecutionRole", { assumedBy: new iam.ServicePrincipal("bedrock-agentcore.amazonaws.com"), managedPolicies: [ iam.ManagedPolicy.fromAwsManagedPolicyName("AmazonBedrockAgentCoreBrowserExecutionRolePolicy"), ], }); // Create browser with custom execution role const browser = new agentcore.BrowserCustom(this, "MyBrowser", { browserCustomName: "my_browser", description: "Browser with custom execution role", networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(), executionRole: executionRole, }); ``` ### Browser with S3 Recording and Permissions ```typescript fixture=default // Create an S3 bucket for recordings const recordingBucket = new s3.Bucket(this, "RecordingBucket", { bucketName: "my-browser-recordings", removalPolicy: RemovalPolicy.DESTROY, // For demo purposes }); // Create browser with recording enabled const browser = new agentcore.BrowserCustom(this, "MyBrowser", { browserCustomName: "my_browser", description: "Browser with recording enabled", networkConfiguration: agentcore.BrowserNetworkConfiguration.usingPublicNetwork(), recordingConfig: { enabled: true, s3Location: { bucketName: recordingBucket.bucketName, objectKey: "browser-recordings/", }, }, }); // The browser construct automatically grants S3 permissions to the execution role // when recording is enabled, so no additional IAM configuration is needed ``` ### Browser with Browser signing 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. 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. To enable the browser to sign requests using the Web Bot Auth protocol, create a browser tool with the browserSigning configuration: ```typescript fixture=default const browser = new agentcore.BrowserCustom(this, 'test-browser', { browserCustomName: 'test_browser', browserSigning: agentcore.BrowserSigning.ENABLED }); ``` ### Browser IAM Permissions The Browser construct provides convenient methods for granting IAM permissions: ```typescript fixture=default // Create a browser const browser = new agentcore.BrowserCustom(this, "MyBrowser", { browserCustomName: "my_browser", description: "Browser for web automation", networkC