Skip to content

fix(deploy): honor aws-targets.json region for all SDK and CDK calls#925

Open
aidandaly24 wants to merge 2 commits intomainfrom
fix/issue-924-region-from-aws-targets
Open

fix(deploy): honor aws-targets.json region for all SDK and CDK calls#925
aidandaly24 wants to merge 2 commits intomainfrom
fix/issue-924-region-from-aws-targets

Conversation

@aidandaly24
Copy link
Copy Markdown
Contributor

Description

The agentcore CLI did not use the region field from aws-targets.json when making API calls for resources like runtimes, evaluators, and online eval configs. Changing aws-targets.json had no effect on where resources were created unless AWS_DEFAULT_REGION was also set.

Root cause

The CLI passes target.region explicitly to its own SDK clients, but @aws-cdk/toolkit-lib constructs internal SDK clients (for CloudFormation, S3 asset upload, asset publishing, etc.) that do not receive an explicit region option and fall back to the SDK default region resolution chain: AWS_REGIONAWS_DEFAULT_REGION → shared config profile. When aws-targets.json specified a non-default region but those env vars were unset, CDK's internal clients resolved the wrong region and resources were created in the SDK default region.

Fix

New helper src/cli/aws/target-region.ts exports:

  • applyTargetRegionToEnv(region) — sets AWS_REGION and AWS_DEFAULT_REGION to the target region, returns a restore function.
  • withTargetRegion(region, fn) — try/finally wrapper.

Applied at:

  • src/cli/commands/deploy/actions.tshandleDeploy() calls applyTargetRegionToEnv(target.region) after target resolution; restore runs in the existing finally block.
  • src/cli/operations/deploy/teardown.tsdestroyTarget() wraps CDK initialize()/destroy() in withTargetRegion(...).
  • src/cli/tui/hooks/useCdkPreflight.ts — TUI path applies the override after validateProject(); restored on unmount, cancelTeardown, preflight retry, SIGINT/SIGTERM.

Invoke/status/eval/logs commands already pass target.region explicitly to every SDK client — no changes needed there.

Related Issue

Closes #924

Documentation PR

N/A — behavior-only fix.

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 tests

7 new unit tests in `src/cli/aws/tests/target-region.test.ts` cover:

  • sets both env vars to the provided region
  • restore clears env vars when they were previously unset
  • restore preserves previous values when they were set
  • restore handles each env var independently
  • applies inside callback and restores afterwards
  • restores even when the callback throws
  • returns the callback result

All 7 pass. Relevant targeted suites (aws, operations/deploy, config-io, tui/hooks): 354/354 pass. tsc: 0 errors. eslint: 0 errors on modified files.

End-to-end testing against live AWS

Tested full lifecycle (create → add agent → deploy → invoke → teardown) against AWS account 603141041947 across 5 scenarios:

# Scenario Region Result
1 CLI non-interactive, no env vars `ap-southeast-2` ✅ Runtime in apse2, none in us-east-1
2 Env isolation — success + error paths `ap-southeast-2` ✅ Env vars remain unset after both paths
3 CLI non-interactive, no env vars `eu-west-1` ✅ Runtime in euw1, invoke returned "Hello!"
4 Interactive TUI (tui-harness) `ap-southeast-2` ✅ Full interactive deploy + teardown
5 Key test: target beats env var `eu-west-2` + `AWS_DEFAULT_REGION=us-east-1` ✅ Runtime in euw2, none in us-east-1

All 5 verified via `aws bedrock-agentcore-control list-agent-runtimes` and teardown confirmed cleanup.

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.

…924)

AWS SDK clients constructed by @aws-cdk/toolkit-lib internally (for
CloudFormation, S3 asset upload, etc.) do not receive an explicit region
option and fall back to the SDK's default region resolution chain
(AWS_REGION -> AWS_DEFAULT_REGION -> shared config). When a user's
aws-targets.json specified a non-default region but those env vars were
unset, resources were created in the SDK default region instead of the
configured target.

Promote target.region to AWS_REGION and AWS_DEFAULT_REGION for the
lifetime of deploy and teardown operations, restoring prior values in a
finally block. This ensures downstream SDK clients (explicit and
toolkit-lib internal) agree on the target region.

Covers CLI non-interactive deploy (handleDeploy) and the interactive TUI
deploy/teardown (useCdkPreflight, destroyTarget). Invoke/status/eval
already pass target.region explicitly.
@aidandaly24 aidandaly24 requested a review from a team April 22, 2026 22:02
@github-actions github-actions Bot added the size/m PR size: M label Apr 22, 2026
@github-actions
Copy link
Copy Markdown
Contributor

Package Tarball

aws-agentcore-0.9.1.tgz

How to install

npm install https://github.com/aws/agentcore-cli/releases/download/pr-925-tarball/aws-agentcore-0.9.1.tgz

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 22, 2026

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 42.02% 7241 / 17229
🔵 Statements 41.44% 7675 / 18518
🔵 Functions 40.13% 1283 / 3197
🔵 Branches 40.49% 4826 / 11917
Generated in workflow #1892 for commit f2bfd20 by the Vitest Coverage Report Action

Copy link
Copy Markdown
Contributor

@notgitika notgitika left a comment

Choose a reason for hiding this comment

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

Nice fix — the approach of promoting the target region onto env vars is the right call since CDK toolkit-lib's internal clients don't take an explicit region.

Two small things:

  1. TUI error states don't restore the env override — when run() errors out after the region override is applied (e.g. build/synth/stack-status failures), we call setPhase('error') but never call restoreRegionEnv(). It stays overridden until the user retries or unmounts. Probably fine in practice since the TUI is short-lived, but a small useEffect guard would close this gap cleanly:

    useEffect(() => {
      if (phase === 'error') restoreRegionEnv();
    }, [phase, restoreRegionEnv]);
  2. Minor export inconsistencywithTargetRegion is exported from the barrel (aws/index.ts) but applyTargetRegionToEnv is imported directly from target-region.ts in both actions.ts and useCdkPreflight.ts. Not a big deal, just noticed the mismatch — either export both from the barrel or import both directly.

Everything else looks solid — tests are thorough, the teardown double-wrapping is a nice defensive touch, and the env var save/restore logic correctly handles the undefined vs set distinction.

…el exports

Review feedback:
1. TUI preflight error branches called setPhase('error') without calling
   restoreRegionEnv(). Add a useEffect guarded on phase === 'error' so every
   error path restores the env override without threading the call into
   every branch.
2. Export applyTargetRegionToEnv from the aws barrel for consistency with
   withTargetRegion. Update CLI deploy, teardown, and TUI preflight hook
   to import from the barrel instead of the deep path.
@github-actions github-actions Bot added size/m PR size: M and removed size/m PR size: M labels Apr 23, 2026
@aidandaly24 aidandaly24 deployed to e2e-testing April 23, 2026 01:24 — with GitHub Actions Active
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.

CLI ignores region from aws-targets.json for API calls unless AWS_DEFAULT_REGION is set

2 participants