Skip to content

fix(exec): derive region from the ARN so --runtime needs no project - #1996

Merged
notgitika merged 1 commit into
mainfrom
fix/exec-region-from-arn
Aug 13, 2026
Merged

fix(exec): derive region from the ARN so --runtime needs no project#1996
notgitika merged 1 commit into
mainfrom
fix/exec-region-from-arn

Conversation

@notgitika

@notgitika notgitika commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

Description

agentcore exec --runtime <full-arn> fails outside an AgentCore project unless --region is also passed:

$ agentcore exec --it --runtime arn:aws:bedrock-agentcore:us-east-1:111122223333:runtime/MyRuntime-abc123 --session-id <id>
AWS Targets config file not found at: /path/to/cwd/agentcore/aws-targets.json

loadExecContext gated its "explicit ARN, skip deployed state" short-circuit on both flags (options.runtimeArn?.startsWith('arn:') && options.region), so an ARN alone fell through to readAWSDeploymentTargets() / readDeployedState(), which throw first. The branch further down labelled "--runtime <arn> with no --region" sat after those reads and used config only for options.region ?? targetConfig.region — a value already present as field 3 of the ARN that was just supplied. So the CLI demanded a project, a hand-written aws-targets.json, and a completed deploy purely to recover something the caller had handed it, which rules out exec for anyone whose runtimes are deployed by CDK, a pipeline, or a personal stack.

This derives the region from the ARN so --region is optional, and the short-circuit fires on the ARN alone. Config is now read only when a name has to be resolved. A region-less or malformed ARN still falls through to config as a last resort, so nothing that worked before stops working. --harness <arn> had the identical gate and gets the identical treatment.

regionFromArn already existed in src/cli/operations/jobs/shared/region.ts, so it is reused rather than rewritten. It moves to a new src/cli/aws/arn.ts and is re-exported from its old location to keep the jobs-facing import path stable. Two test files factory-mock the whole aws/region module, so it could not live there — and the split is cleaner regardless: aws/region.ts detects the ambient region from env and shared config files, while this is a pure string function over an ARN the caller already holds. Splitting on : rather than matching a partition keeps it correct for GovCloud and China ARNs.

Related Issue

Closes #1995

Documentation PR

N/A

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Other (please describe):

Testing

How have you tested the change?

  • I ran npm run test:unit and npm run test:integ
  • I ran npm run typecheck
  • I ran npm run lint
  • If I modified src/assets/, I ran npm run test:update-snapshots and committed the updated snapshots

Unit: 423/423 files, 6095/6095 tests. Typecheck, lint, prettier and secretlint clean. test:integ did not run to completion — I stopped it rather than block this PR, so it never printed a summary. Every integ test that did run passed and no failures appeared in the output, but I am not claiming a green integ run. No integ test touches exec, cli/aws, or the jobs region path; please let CI be the authority there.

Also verified end-to-end against a built CLI rather than by unit test alone, in an empty directory with no project:

  • installed 0.25.0 → AWS Targets config file not found at: /private/tmp/exec-fix-check/agentcore/aws-targets.json
  • this branch → reaches the real InvokeAgentRuntime call in us-east-1 and fails only on cross-account authorization for a runtime in an account I have no access to

That is the correct terminal state: region resolved from the ARN, config wall gone, no project and no aws-targets.json on disk.

New unit tests cover region-from-ARN with config readers mocked to reject and asserted not.toHaveBeenCalled() (standing in for "no project at all"), an explicit --region overriding the ARN, aws-us-gov and aws-cn partitions, and the malformed-ARN fall-back to config.

Checklist

  • I have read the CONTRIBUTING document
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.

`agentcore exec --runtime <arn>` failed with "AWS Targets config file not
found" unless --region was also passed. The ARN short-circuit in
loadExecContext was gated on `startsWith('arn:') && options.region`, so
omitting --region fell through to readAWSDeploymentTargets() /
readDeployedState(), which throw before anything else runs.

The fall-through branch already existed and was labelled "--runtime <arn>
with no --region", but it sat after those reads and used config for one
thing: `options.region ?? targetConfig.region`. That region is field 3 of
the ARN the caller just supplied, so exec was demanding a project, an
aws-targets.json and a completed deploy to recover a value it already had.
This blocks exec for anyone deploying runtimes outside the CLI (CDK,
pipelines, personal stacks), who have no reason to own an agentcore
project at all.

Parse the region from the ARN instead and drop the --region requirement
from both the --runtime and --harness short-circuits. Config is now read
only when a *name* needs resolving, or when the ARN's region field is
empty or malformed. An explicit --region still wins.

regionFromArn moves from operations/jobs/shared/region to a new cli/aws/arn
module so exec does not have to depend on operations/jobs; the jobs path
re-exports it. It lives apart from cli/aws/region because that module
detects the ambient region via env and shared config files, while this is a
pure function over an ARN the caller already holds -- and several jobs tests
replace cli/aws/region wholesale with a detectRegion-only factory mock.
@notgitika
notgitika requested a review from a team August 13, 2026 17:38
@github-actions github-actions Bot added the size/m PR size: M label Aug 13, 2026
@github-actions github-actions Bot added size/m PR size: M agentcore-harness-reviewing AgentCore Harness review in progress and removed size/m PR size: M labels Aug 13, 2026
@agentcore-devx-automation agentcore-devx-automation Bot added the claude-security-reviewing Claude Code /security-review in progress label Aug 13, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Package Tarball

aws-agentcore-0.27.0.tgz

How to install

gh release download pr-1996-tarball --repo aws/agentcore-cli --pattern "*.tgz" --dir /tmp/pr-tarball
npm install -g /tmp/pr-tarball/aws-agentcore-0.27.0.tgz

@agentcore-devx-automation

Copy link
Copy Markdown
Contributor

Claude Security Review: no high-confidence findings. (run)

@agentcore-devx-automation agentcore-devx-automation Bot removed the claude-security-reviewing Claude Code /security-review in progress label Aug 13, 2026
@github-actions github-actions Bot removed the agentcore-harness-reviewing AgentCore Harness review in progress label Aug 13, 2026

@nborges-aws nborges-aws left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for fixing!

@notgitika
notgitika merged commit ba4a1bd into main Aug 13, 2026
38 checks passed
@notgitika
notgitika deleted the fix/exec-region-from-arn branch August 13, 2026 17:44
@github-actions

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 40.65% 15274 / 37572
🔵 Statements 39.92% 16287 / 40799
🔵 Functions 34.85% 2614 / 7499
🔵 Branches 33.96% 10185 / 29984
Generated in workflow #4337 for commit eb4ea4f by the Vitest Coverage Report Action

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/m PR size: M

Projects

None yet

Development

Successfully merging this pull request may close these issues.

exec: --runtime <arn> requires --region, and otherwise demands a project + aws-targets.json + a deploy

2 participants