fix(deploy): honor aws-targets.json region for all SDK and CDK calls#925
fix(deploy): honor aws-targets.json region for all SDK and CDK calls#925aidandaly24 wants to merge 2 commits intomainfrom
Conversation
…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.
Package TarballHow to installnpm install https://github.com/aws/agentcore-cli/releases/download/pr-925-tarball/aws-agentcore-0.9.1.tgz |
Coverage Report
|
notgitika
left a comment
There was a problem hiding this comment.
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:
-
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 callsetPhase('error')but never callrestoreRegionEnv(). It stays overridden until the user retries or unmounts. Probably fine in practice since the TUI is short-lived, but a smalluseEffectguard would close this gap cleanly:useEffect(() => { if (phase === 'error') restoreRegionEnv(); }, [phase, restoreRegionEnv]);
-
Minor export inconsistency —
withTargetRegionis exported from the barrel (aws/index.ts) butapplyTargetRegionToEnvis imported directly fromtarget-region.tsin bothactions.tsanduseCdkPreflight.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.
Description
The agentcore CLI did not use the
regionfield fromaws-targets.jsonwhen making API calls for resources like runtimes, evaluators, and online eval configs. Changingaws-targets.jsonhad no effect on where resources were created unlessAWS_DEFAULT_REGIONwas also set.Root cause
The CLI passes
target.regionexplicitly to its own SDK clients, but@aws-cdk/toolkit-libconstructs 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_REGION→AWS_DEFAULT_REGION→ shared config profile. Whenaws-targets.jsonspecified 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.tsexports:applyTargetRegionToEnv(region)— setsAWS_REGIONandAWS_DEFAULT_REGIONto the target region, returns a restore function.withTargetRegion(region, fn)— try/finally wrapper.Applied at:
src/cli/commands/deploy/actions.ts—handleDeploy()callsapplyTargetRegionToEnv(target.region)after target resolution; restore runs in the existingfinallyblock.src/cli/operations/deploy/teardown.ts—destroyTarget()wraps CDKinitialize()/destroy()inwithTargetRegion(...).src/cli/tui/hooks/useCdkPreflight.ts— TUI path applies the override aftervalidateProject(); restored on unmount,cancelTeardown, preflight retry, SIGINT/SIGTERM.Invoke/status/eval/logs commands already pass
target.regionexplicitly to every SDK client — no changes needed there.Related Issue
Closes #924
Documentation PR
N/A — behavior-only fix.
Type of Change
Testing
How have you tested the change?
Unit tests
7 new unit tests in `src/cli/aws/tests/target-region.test.ts` cover:
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:
All 5 verified via `aws bedrock-agentcore-control list-agent-runtimes` and teardown confirmed cleanup.
Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.