Skip to content

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
mainfrom
fix/924
Closed

fix: honor aws-targets.json region for AWS SDK calls when AWS_DEFAULT_REGION is unset#1127
aidandaly24 wants to merge 1 commit into
mainfrom
fix/924

Conversation

@aidandaly24

Copy link
Copy Markdown
Contributor

Description

Make the resolved deployment target's region from aws-targets.json 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.

Background

Previously, when a user had a non-default region in aws-targets.json but no AWS_DEFAULT_REGION set in their shell, the CLI would silently fall back to the SDK's default region (typically us-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 from aws-targets.json.

A partial fix already existed for deploy, teardown, and the TUI preflight hook via applyTargetRegionToEnv() in src/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 a runWithTargetRegion(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 explicit region option must apply the override after resolving its target.
  • src/cli/aws/index.ts — re-exports runWithTargetRegion.
  • src/cli/commands/import/actions.ts — replaces the bare process.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) with applyTargetRegionToEnv(). Re-applies the override with the resolved target region (which overrides the YAML-based initial region) and restores in a top-level finally block on both success and error paths.
  • src/cli/commands/abtest/command.tsgetRegion() now also promotes the resolved region onto AWS_REGION / AWS_DEFAULT_REGION so any helper constructing an SDK client without an explicit region behaves consistently. (The previous fallback could silently land on us-east-1 even when aws-targets.json was present.)
  • Tests:
    • New src/cli/commands/deploy/__tests__/region-override.test.ts — verifies handleDeploy propagates target.region to process.env during execution and restores after, on both happy and error paths, and that env is not mutated when no target can be resolved.
    • New src/cli/commands/import/__tests__/region-override.test.ts — analogous coverage for handleImport, including the no-physical-IDs (light) path.
    • Extended src/cli/aws/__tests__/target-region.test.ts with cases for the new runWithTargetRegion wrapper (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 from aws-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

  • 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

Targeted test runs that pass on this branch:

  • src/cli/aws/__tests__/target-region.test.ts — 11/11 pass (includes new runWithTargetRegion cases).
  • src/cli/commands/deploy/__tests__/region-override.test.ts — 3/3 pass.
  • src/cli/commands/import/__tests__/region-override.test.ts — 3/3 pass.
  • Regression checks on touched import/actions.ts: import-no-deploy.test.ts + idempotency.test.ts — 39/39 pass.
  • npm run typecheck is clean.

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.

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
@aidandaly24 aidandaly24 requested a review from a team May 6, 2026 03:53
@github-actions github-actions Bot added size/l PR size: L agentcore-harness-reviewing AgentCore Harness review in progress labels May 6, 2026
@aidandaly24 aidandaly24 marked this pull request as draft May 6, 2026 03:55
@agentcore-cli-automation

Copy link
Copy Markdown

Reviewed the diff end-to-end against src/cli/commands/deploy/actions.ts, src/cli/commands/import/actions.ts, src/cli/commands/abtest/command.ts, and src/cli/aws/target-region.ts. No blocking issues found.

A few things I specifically verified:

  • setRegionEnv chaining in import/actions.ts — tracing through the call sequence (YAML-region override → target-region override → outer finally restore), the pattern correctly restores to the pre-handleImport env because each applyTargetRegionToEnv call snapshots the current env, and setRegionEnv restores the previous snapshot before taking a new one. The final restoreEnv?.() in the outer finally reverts to the original values on all paths (happy, early return, thrown error).
  • Deploy pathrestoreEnv = applyTargetRegionToEnv(target.region) at the top of try + restoreEnv?.() in finally is correct; the new test's assertion that env is observed as TARGET_REGION inside validateProject and restored afterward on the throw path matches what the source actually does.
  • AB-test getRegion — the intentional no-restore is fine: the command is CLI-entry-only (registered once in cli.ts), is called exactly once per invocation, and the process exits shortly after. The comment explaining this is clear.
  • Policy comment + runWithTargetRegion — the helper is not yet used by any handler, but it's documented as scaffolding for future commands and has its own tests. Reasonable to land alongside the fix.

LGTM to merge once CI is green.

@github-actions github-actions Bot removed the agentcore-harness-reviewing AgentCore Harness review in progress label May 6, 2026
@github-actions

github-actions Bot commented May 6, 2026

Copy link
Copy Markdown
Contributor

Coverage Report

Status Category Percentage Covered / Total
🔵 Lines 43.47% 9100 / 20931
🔵 Statements 42.73% 9659 / 22600
🔵 Functions 40.04% 1559 / 3893
🔵 Branches 40.16% 5839 / 14536
Generated in workflow #2504 for commit ca1a7ee by the Vitest Coverage Report Action

@aidandaly24

Copy link
Copy Markdown
Contributor Author

Closing: created by a test run with broken extract phase (0-file diff → insufficient review)

@aidandaly24 aidandaly24 closed this May 6, 2026
@aidandaly24 aidandaly24 deleted the fix/924 branch May 6, 2026 04:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/l PR size: L

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