Skip to content

AgentCore Gateway to Agent

The connection generator can register an agent (either TypeScript or Python) as an AgentCore Runtime target of an AgentCore Gateway generated with protocol: http.

Once connected, the Gateway proxies requests for the agent under <gatewayUrl>/<targetName>/invocations, signing outbound traffic to the runtime with IAM SigV4. This gives your agents a single governed entry point — and since callers only need to reach the Gateway, the agent runtimes themselves can be deployed inside a VPC behind it.

Before using this generator, ensure you have:

  1. An agentcore-gateway project generated with protocol: http
  2. An agent component (ts#agent or py#agent) created with infra: agentcore. Either auth: iam (the Gateway invokes it with its own role) or auth: cognito (the Gateway forwards the caller’s JWT — see Forwarding caller identity) works.
  1. Install the Nx Console VSCode Plugin if you haven't already
  2. Open the Nx Console in VSCode
  3. Click Generate (UI) in the "Common Nx Commands" section
  4. Search for @aws/nx-plugin - connection
  5. Fill in the required parameters
    • Click Generate

    Select the Gateway project as the source and the agent project as the target. If the agent project contains multiple components, specify targetComponent to disambiguate.

    ParameterTypeDefaultDescription
    sourceProject Requiredstring-The source project
    targetProject Requiredstring-The target project to connect to
    sourceComponent string-The source component to connect from (component name, path relative to source project root, or generator id). Use '.' to explicitly select the project as the source.
    targetComponent string-The target component to connect to (component name, path relative to target project root, or generator id). Use '.' to explicitly select the project as the target.
    preferInstallDependencies booleantrueWhether to prefer installing dependencies after the generator runs. 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 wires existing projects together rather than emitting new source files. The following files are modified:

    • Directorypackages/<gateway>
      • project.json the Gateway’s dev target gains a dependency on the agent’s <agent>-dev
      • local-dev.ts ATTACHED_AGENTS updated so the local gateway proxies to the agent

    The generator cannot automatically wire the agent target into your infrastructure because it doesn’t know which stack or module instantiates the Gateway. Add a single call to gateway.addAgent(agent) yourself.

    In the stack where you instantiate the Gateway, register the agent as a target:

    packages/infra/src/stacks/application-stack.ts
    const myAgent = new MyAgent(this, 'MyAgent');
    const myGateway = new MyGateway(this, 'MyGateway');
    // Register the agent as a runtime target of the Gateway. The target name
    // defaults to the agent's `agentName` (its class name in kebab-case,
    // e.g. `MyAgent` -> `my-agent`), and forms the target's invocation path:
    // <gatewayUrl>/my-agent/invocations
    myGateway.addAgent(myAgent);

    To override the default target name, pass gatewayTargetName:

    myGateway.addAgent(myAgent, { gatewayTargetName: 'my-target' });

    The construct grants the Gateway’s execution role invoke access to the agent runtime and configures the target with the GATEWAY_IAM_ROLE credential provider, so the Gateway signs outbound calls with its own role.

    Requests to <gatewayUrl origin>/<targetName>/invocations are forwarded to the agent runtime without protocol translation, so callers use the same request shape they’d use against the runtime directly — SSE streams (AG-UI), JSON streaming (Python HTTP) and A2A JSON-RPC all proxy through. Callers authenticate with the Gateway (IAM SigV4 or Cognito JWT depending on the Gateway’s auth) rather than with the agent.

    To connect a website to the Gateway’s agents, use the connection generator.

    By default the Gateway signs outbound calls with its own IAM role (the GATEWAY_IAM_ROLE credential provider), so the runtime sees the Gateway’s identity, not the caller’s. If instead you want the agent to authorize on the caller — for example to read the user’s sub or scope claims — front a Cognito agent with a Cognito Gateway. The Gateway then forwards the caller’s JWT to the runtime unchanged (the JWT_PASSTHROUGH credential provider), and the runtime revalidates it.

    Generate both ends with auth: cognito and connect them as above:

    • an agent (ts#agent or py#agent) created with auth: cognito, and
    • a Gateway created with auth: cognito fronting the same Cognito user pool.

    Everything else is automatic — gateway.addAgent(agent) (CDK) and the generated Terraform runtime module handle the wiring for you based on the agent’s auth:

    • the target is created with the JWT_PASSTHROUGH credential provider (rather than GATEWAY_IAM_ROLE), and
    • the runtime allowlists the Authorization header so the forwarded token reaches your agent code. Without this allowlist AgentCore validates the token but strips the header before your container.

    Callers invoke the Gateway with Authorization: Bearer <jwt> (no SigV4), and the agent reads the claims from the Authorization header — skipping signature validation, since the runtime’s inbound authorizer has already verified the token:

    packages/py_project/.../my_agent/main.py
    import jwt # PyJWT
    @app.post('/invocations')
    async def invoke(input: InvokeInput, request: Request):
    token = request.headers['authorization'].removeprefix('Bearer ')
    claims = jwt.decode(token, options={'verify_signature': False})
    # authorize on claims['sub'], claims['scope'], ...

    Running the Gateway locally with:

    Terminal window
    pnpm nx dev <gateway-name>

    starts a local gateway plus every attached agent on its assigned local port. The local gateway proxies /<targetName>/... paths to each agent’s local server, matching the deployed Gateway’s path-based routing.