fix: honor aws-targets.json region for AWS SDK calls when AWS_DEFAULT_REGION is unset#1127
Closed
aidandaly24 wants to merge 1 commit into
Closed
fix: honor aws-targets.json region for AWS SDK calls when AWS_DEFAULT_REGION is unset#1127aidandaly24 wants to merge 1 commit into
aidandaly24 wants to merge 1 commit into
Conversation
Make the resolved deployment target's region authoritative on the process environment for every CLI command that may construct AWS SDK clients (or invoke CDK toolkit-lib internals) without an explicit `region` option. Previously this override existed only in `deploy`, `teardown`, and the TUI preflight hook. The `import` command mutated AWS_REGION / AWS_DEFAULT_REGION directly without a finally restore (leaking env state to subsequent in-process work) and `abtest`'s region resolver could silently fall back to us-east-1 even when aws-targets.json was present. Changes - target-region: add `runWithTargetRegion` convenience wrapper and a policy comment documenting that new entry points MUST apply the override after resolving their target. - import/actions: replace bare process.env mutation with `applyTargetRegionToEnv`; re-apply with the resolved target region (overrides the YAML-based initial region) and restore in a top-level finally block on both success and error paths. - abtest/command: have `getRegion()` also promote the resolved region onto AWS_REGION/AWS_DEFAULT_REGION so any helper constructing an SDK client without an explicit region behaves consistently. - Add focused region-override tests for handleDeploy and handleImport that exercise both the happy path and the error path, and extend the target-region unit tests to cover `runWithTargetRegion`. Refs #924
|
Reviewed the diff end-to-end against A few things I specifically verified:
LGTM to merge once CI is green. |
Contributor
Coverage Report
|
Contributor
Author
|
Closing: created by a test run with broken extract phase (0-file diff → insufficient review) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Make the resolved deployment target's region from
aws-targets.jsonauthoritative on the process environment for every CLI command that may construct AWS SDK clients (or invoke CDK toolkit-lib internals) without an explicitregionoption.Background
Previously, when a user had a non-default region in
aws-targets.jsonbut noAWS_DEFAULT_REGIONset in their shell, the CLI would silently fall back to the SDK's default region (typicallyus-east-1) when making API calls — creating runtimes, evaluators, online eval configs, etc. in the wrong region. This was inconsistent with CDK synthesis, which correctly reads the region fromaws-targets.json.A partial fix already existed for
deploy,teardown, and the TUI preflight hook viaapplyTargetRegionToEnv()insrc/cli/aws/target-region.ts. This PR closes the remaining gaps and adds a structural guarantee plus tests so the behaviour cannot silently regress.Changes
src/cli/aws/target-region.ts— adds arunWithTargetRegion(getTarget, fn)convenience wrapper for handlers where target resolution and the AWS SDK work live in the same lexical scope. Adds a top-of-file policy comment documenting that any new CLI entry point that may construct SDK clients without an explicitregionoption must apply the override after resolving its target.src/cli/aws/index.ts— re-exportsrunWithTargetRegion.src/cli/commands/import/actions.ts— replaces the bareprocess.env.AWS_REGION = …/process.env.AWS_DEFAULT_REGION = …mutation (which leaked env state to subsequent in-process work and was not undone on early returns) withapplyTargetRegionToEnv(). Re-applies the override with the resolved target region (which overrides the YAML-based initial region) and restores in a top-levelfinallyblock on both success and error paths.src/cli/commands/abtest/command.ts—getRegion()now also promotes the resolved region ontoAWS_REGION/AWS_DEFAULT_REGIONso any helper constructing an SDK client without an explicit region behaves consistently. (The previous fallback could silently land onus-east-1even whenaws-targets.jsonwas present.)src/cli/commands/deploy/__tests__/region-override.test.ts— verifieshandleDeploypropagatestarget.regiontoprocess.envduring execution and restores after, on both happy and error paths, and that env is not mutated when no target can be resolved.src/cli/commands/import/__tests__/region-override.test.ts— analogous coverage forhandleImport, including the no-physical-IDs (light) path.src/cli/aws/__tests__/target-region.test.tswith cases for the newrunWithTargetRegionwrapper (success, no-target skip, throw-from-callback, throw-from-resolver).Out of scope
The CDK construct library (
agentcore-l3-cdk-constructs) is not affected — CDK synthesis already reads the region directly fromaws-targets.json. This bug was exclusively in the CLI's runtime AWS SDK calls.Related Issue
Closes #924
Documentation PR
N/A — internal behaviour fix, no user-facing documentation changes.
Type of Change
Testing
How have you tested the change?
npm run test:unitandnpm run test:integnpm run typechecknpm run lintsrc/assets/, I rannpm run test:update-snapshotsand committed the updated snapshotsTargeted test runs that pass on this branch:
src/cli/aws/__tests__/target-region.test.ts— 11/11 pass (includes newrunWithTargetRegioncases).src/cli/commands/deploy/__tests__/region-override.test.ts— 3/3 pass.src/cli/commands/import/__tests__/region-override.test.ts— 3/3 pass.import/actions.ts:import-no-deploy.test.ts+idempotency.test.ts— 39/39 pass.npm run typecheckis clean.Checklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the
terms of your choice.