Skip to content

AgentCore Harness

Generate an Amazon Bedrock AgentCore Harness project. A Harness is a managed agent loop powered by Strands Agents: it owns deployment defaults for the model, system prompt, tools, memory, skills, environments, truncation, authorization, and execution limits, while the service accepts per-invocation overrides for supported fields. Reusing the same Runtime Session ID continues the same Harness session.

Terminal window
pnpm nx g @aws/nx-plugin:agentcore-harness
You can also perform a dry-run to see what files would be changed
Terminal window
pnpm nx g @aws/nx-plugin:agentcore-harness --dry-run
ParameterTypeDefaultDescription
name Requiredstring-The name of your AgentCore Harness project. Must contain at least one non-whitespace character which can be normalized into a kebab-case project name (eg. my-harness).
directory string-Parent directory where the harness project is placed. Defaults to packages. Must be a relative path which does not contain parent directory (..) segments.
subDirectory string-The sub directory the project is placed in. Defaults to the kebab-case harness name. Must be a relative path which does not contain parent directory (..) segments.
infra agentcore | noneagentcoreThe type of infrastructure to generate for hosting your harness. Defaults to agentcore. Select none for no hosting.
iac inherit | cdk | terraforminheritThe preferred IaC provider for generated harness infrastructure. Defaults to inherit, which uses the provider configured for your workspace.
preferInstallDependencies boolean-Whether to prefer installing dependencies after the generator runs. Defaults to true. Set to false to defer installing when batching multiple generators (an install still runs if needed so subsequent generators can compute the Nx project graph); install once at the end.

The generator creates a standalone project at packages/<name>/. Because AWS runs the agent loop for you, the project holds only the prompt that shapes it and a script for talking to it:

  • Directorypackages/<name>/
    • src/PROMPT.md The Harness system prompt
    • scripts/chat.ts Multi-turn chat client for the deployed Harness
    • project.json Adds the chat target
    • README.md Chat and customization instructions

Infrastructure is generated when infra is agentcore (the default). With infra: none no infrastructure is generated — set HARNESS_ARN to invoke a Harness managed elsewhere, and re-run the generator with infra: agentcore later to add infrastructure; existing project files (including your edits) are preserved.

Since this generator vends infrastructure as code based on your chosen iac, it will create a project in packages/common which includes the relevant CDK constructs or Terraform modules.

The common infrastructure as code project is structured as follows:

  • Directorypackages/common/constructs
    • Directorysrc
      • Directoryapp/ Constructs for infrastructure specific to a project/generator
      • Directorycore/ Generic constructs which are reused by constructs in app
      • index.ts Entry point exporting constructs from app
    • project.json Project build targets and configuration
  • Directorypackages/common/constructs/src/app/harnesses/<name>/
    • <name>.ts CDK construct containing the Harness and execution role

The generated infrastructure manages the Harness through the native resource (CDK aws_bedrockagentcore.CfnHarness, Terraform aws_bedrockagentcore_harness) with your generated defaults, creates an IAM execution role with the baseline permissions described below, and uses IAM inbound authorization (no custom JWT authorizer is configured by default).

The Harness ARN is registered at agentcore.harnesses.<ClassName> in Runtime Configuration, preserving any existing entries.

The generator creates CDK or Terraform infrastructure as code based on your selected iac provider. You can use this to deploy your Harness through your usual infrastructure workflow.

The CDK construct for deploying your Harness lives in the common/constructs folder. Instantiate it from a CDK application:

packages/infra/src/stacks/application-stack.ts
import { MyHarness } from '@my-scope/common-constructs';
import { Stack, type StackProps } from 'aws-cdk-lib';
import type { Construct } from 'constructs';
export class ApplicationStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
new MyHarness(this, 'MyHarness');
}
}

The construct’s props interface (MyHarnessProps) extends Partial<Omit<CfnHarnessProps, 'executionRoleArn' | 'allowedTools'>>, so any native Harness property can be supplied and takes precedence over the generated defaults:

const harness = new MyHarness(this, 'MyHarness', {
maxIterations: 20,
timeoutSeconds: 600,
});

The construct also accepts:

  • allowedTools — the tools the Harness may use. The Harness deploys with none unless you supply them, see Configuring tools.
  • executionRole — an existing IAM role to use instead of the generated role. A supplied role is used as-is: the baseline permissions are not added to it, and its ARN always feeds the Harness (the raw executionRoleArn string cannot be overridden).
  • modelResourceArns — the Bedrock model and inference-profile ARNs the generated execution role may invoke, replacing the default list.
  • vpc, vpcSubnets and securityGroups — run the Harness in a VPC so it can reach private resources, see Running in a VPC.

Its public members are harness (the CfnHarness), executionRole, grantPrincipal, the harnessArn getter, connections (in a VPC), addToRolePolicy(statement) for execution role extensions, and grantInvokeAccess(grantee) for authorizing callers.

Deploy the stack with your infrastructure project as usual — see the CDK infrastructure guide.

You can grant a caller permissions to invoke the harness as follows:

const harness = new MyHarness(this, 'MyHarness');
harness.grantInvokeAccess(caller);

A caller needs both bedrock-agentcore:InvokeHarness and bedrock-agentcore:InvokeAgentRuntime on the Harness ARN, which is exactly what grantInvokeAccess grants.

The generated chat target runs scripts/chat.ts, dropping you into an interactive terminal chat with your deployed Harness:

Terminal window
pnpm nx run <project>:chat

Every turn of a run shares one session, so the Harness keeps the conversation context until you exit. Credentials come from the standard AWS SDK credential provider chain, and the AWS Region is derived from the Harness ARN.

The Harness ARN is resolved in this order:

  1. HARNESS_ARN (non-empty): used directly, without reading Runtime Configuration:

    Terminal window
    HARNESS_ARN=<harness-arn> pnpm nx run <project>:chat
  2. RUNTIME_CONFIG_APP_ID: resolves the ARN from the agentcore.harnesses.<ClassName> entry published by the deployed infrastructure:

    Terminal window
    RUNTIME_CONFIG_APP_ID=<application-id> pnpm nx run <project>:chat

With neither set, the script fails with an error naming both options.

src/PROMPT.md is the Harness system prompt: edit it and redeploy to change how the Harness behaves. Everything else is configured where you instantiate the infrastructure, or by editing the generated construct or module directly. Re-running the generator never overwrites existing files (it only adds missing files and merges project metadata), so your edits to the prompt and to the generated infrastructure are preserved.

Every native Harness property of the pinned aws-cdk-lib/aws-bedrockagentcore module is available through the construct’s props — alternate model providers, tool definitions, memory, skills, environment configuration, truncation, custom JWT authorization, and execution limits — and explicit props take precedence over the generated defaults. Alternatively, edit the generated construct in packages/common/constructs/src/app/harnesses/<name>/<name>.ts.

Omitting authorizer configuration (the default) means IAM inbound authorization; configure a custom JWT authorizer through the native field to change that.

The Harness deploys with no tools, so it starts with the least capability. Opt in to the tools it may use:

new MyHarness(this, 'Harness', { allowedTools: ['@builtin'] });

Narrow @builtin to specific patterns such as @builtin/file_operations to restrict what the agent loop can do. See Harness tools for the built-in tools you can add.

Supply a vpc to run the Harness inside it, so it can reach private resources such as a database. The construct implements IConnectable, so those resources grant it access the same way they would any other:

const harness = new MyHarness(this, 'MyHarness', { vpc });
database.connections.allowDefaultPortFrom(harness, 'Harness to database');

The Harness is placed in the VPC’s private subnets with egress, in a security group created for it. Override either with vpcSubnets and securityGroups. Both require vpc, and connections is only available when the Harness runs in a VPC.

The values configured in infrastructure are deployment defaults. The service also accepts per-invocation overrides for supported Harness fields (such as models, tools, and skills) in the InvokeHarness request; deployment defaults apply wherever a field is not overridden.