11import { ConfigIO , SecureCredentials } from '../../../lib' ;
22import type { DeployedState } from '../../../schema' ;
33import { AwsCredentialsError , validateAwsCredentials } from '../../aws/account' ;
4+ import { applyTargetRegionToEnv } from '../../aws/target-region' ;
45import { type CdkToolkitWrapper , type SwitchableIoHost , createSwitchableIoHost } from '../../cdk/toolkit-lib' ;
56import { getErrorMessage , isExpiredTokenError , isNoCredentialsError } from '../../errors' ;
67import type { ExecLogger } from '../../logging' ;
@@ -137,6 +138,11 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
137138 const isRunningRef = useRef ( false ) ;
138139 // Keep a ref to the wrapper so we can dispose it when starting a new run
139140 const wrapperRef = useRef < CdkToolkitWrapper | null > ( null ) ;
141+ // Restore function for AWS_REGION / AWS_DEFAULT_REGION overrides, applied
142+ // after target resolution so downstream SDK / CDK toolkit-lib clients use the
143+ // aws-targets.json region rather than whatever the SDK default chain resolves.
144+ // See https://github.com/aws/agentcore-cli/issues/924.
145+ const restoreRegionEnvRef = useRef < ( ( ) => void ) | null > ( null ) ;
140146
141147 const updateStep = ( index : number , update : Partial < Step > ) => {
142148 setSteps ( prev => prev . map ( ( s , i ) => ( i === index ? { ...s , ...update } : s ) ) ) ;
@@ -158,18 +164,26 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
158164 }
159165 } , [ ] ) ;
160166
167+ // Restore AWS_REGION / AWS_DEFAULT_REGION (no-op when nothing was applied)
168+ const restoreRegionEnv = useCallback ( ( ) => {
169+ restoreRegionEnvRef . current ?.( ) ;
170+ restoreRegionEnvRef . current = null ;
171+ } , [ ] ) ;
172+
161173 const startPreflight = useCallback ( async ( ) => {
162174 if ( isRunningRef . current ) return ;
163175 // Dispose any existing wrapper before starting a new run
164176 await disposeWrapper ( ) ;
177+ // Restore any previously-applied region env override before re-running
178+ restoreRegionEnv ( ) ;
165179 resetSteps ( ) ;
166180 setCdkToolkitWrapper ( null ) ;
167181 setStackNames ( [ ] ) ;
168182 setBootstrapContext ( null ) ;
169183 setHasTokenExpiredError ( false ) ; // Reset token expired state when retrying
170184 setHasCredentialsError ( false ) ; // Reset credentials error state when retrying
171185 setPhase ( 'running' ) ;
172- } , [ disposeWrapper ] ) ;
186+ } , [ disposeWrapper , restoreRegionEnv ] ) ;
173187
174188 const clearTokenExpiredError = useCallback ( ( ) => {
175189 setHasTokenExpiredError ( false ) ;
@@ -183,6 +197,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
183197 useEffect ( ( ) => {
184198 const handleInterrupt = ( ) => {
185199 void disposeWrapper ( ) ;
200+ restoreRegionEnv ( ) ;
186201 } ;
187202
188203 process . on ( 'SIGINT' , handleInterrupt ) ;
@@ -193,8 +208,9 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
193208 process . off ( 'SIGTERM' , handleInterrupt ) ;
194209 // Dispose on unmount (user navigated away)
195210 void disposeWrapper ( ) ;
211+ restoreRegionEnv ( ) ;
196212 } ;
197- } , [ disposeWrapper ] ) ;
213+ } , [ disposeWrapper , restoreRegionEnv ] ) ;
198214
199215 const confirmTeardown = useCallback ( ( ) => {
200216 // Mark teardown as confirmed and restart the preflight flow
@@ -206,7 +222,8 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
206222 const cancelTeardown = useCallback ( ( ) => {
207223 setPhase ( 'error' ) ;
208224 isRunningRef . current = false ;
209- } , [ ] ) ;
225+ restoreRegionEnv ( ) ;
226+ } , [ restoreRegionEnv ] ) ;
210227
211228 const confirmBootstrap = useCallback ( ( ) => {
212229 setPhase ( 'bootstrapping' ) ;
@@ -274,6 +291,15 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
274291 try {
275292 preflightContext = await validateProject ( ) ;
276293 setContext ( preflightContext ) ;
294+ // Make aws-targets.json region authoritative for downstream SDK / CDK
295+ // toolkit-lib clients that bypass explicit region options. Restored on
296+ // unmount, teardown rejection, or subsequent preflight start.
297+ // See https://github.com/aws/agentcore-cli/issues/924.
298+ const firstTarget = preflightContext . awsTargets [ 0 ] ;
299+ if ( firstTarget ) {
300+ restoreRegionEnv ( ) ;
301+ restoreRegionEnvRef . current = applyTargetRegionToEnv ( firstTarget . region ) ;
302+ }
277303 logger . endStep ( 'success' ) ;
278304 updateStep ( STEP_VALIDATE , { status : 'success' } ) ;
279305 } catch ( err ) {
@@ -493,7 +519,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
493519 return ( ) => {
494520 process . off ( 'unhandledRejection' , handleUnhandledRejection ) ;
495521 } ;
496- } , [ phase , logger , switchableIoHost , isInteractive , skipIdentityCheck , teardownConfirmed ] ) ;
522+ } , [ phase , logger , switchableIoHost , isInteractive , skipIdentityCheck , teardownConfirmed , restoreRegionEnv ] ) ;
497523
498524 // Handle identity-setup phase (after user provides credentials)
499525 useEffect ( ( ) => {
0 commit comments