11import { ConfigIO , SecureCredentials , toError } from '../../../lib' ;
22import type { DependencySyncResult } from '../../../lib/dependency-management' ;
33import { AwsCredentialsError , DependencySyncError , UserCancellationError } from '../../../lib/errors/types' ;
4- import type { DeployedState } from '../../../schema' ;
4+ import type { AwsDeploymentTarget , DeployedState } from '../../../schema' ;
55import { applyTargetRegionToEnv } from '../../aws' ;
66import { validateAwsCredentials } from '../../aws/account' ;
77import { type CdkToolkitWrapper , type SwitchableIoHost , createSwitchableIoHost } from '../../cdk/toolkit-lib' ;
@@ -40,7 +40,7 @@ const LABEL_PAYMENTS = 'Creating payment infrastructure';
4040
4141interface RunPaymentSetupOptions {
4242 projectSpec : PreflightContext [ 'projectSpec' ] ;
43- awsTargets : PreflightContext [ 'awsTargets' ] ;
43+ target : NonNullable < PreflightContext [ 'awsTargets' ] [ 0 ] > ;
4444 runtimeCredentials ?: SecureCredentials ;
4545 logger : ExecLogger ;
4646 setSteps : React . Dispatch < React . SetStateAction < Step [ ] > > ;
@@ -57,7 +57,7 @@ interface RunPaymentSetupOptions {
5757async function runPaymentPreDeploy ( opts : RunPaymentSetupOptions ) : Promise < boolean > {
5858 const {
5959 projectSpec,
60- awsTargets ,
60+ target ,
6161 runtimeCredentials,
6262 logger,
6363 setSteps,
@@ -75,7 +75,6 @@ async function runPaymentPreDeploy(opts: RunPaymentSetupOptions): Promise<boolea
7575 } ) ;
7676 logger . startStep ( 'Setting up payment credentials...' ) ;
7777
78- const target = awsTargets [ 0 ] ! ;
7978 const paymentConfigIO = new ConfigIO ( ) ;
8079
8180 const paymentResult = await setupPaymentCredentialProviders ( {
@@ -147,6 +146,8 @@ export interface PreflightOptions {
147146 isInteractive ?: boolean ;
148147 /** Skip identity provider check (for plan command which only synthesizes) */
149148 skipIdentityCheck ?: boolean ;
149+ /** Target selected by the TUI. Falls back to the first configured target when omitted. */
150+ selectedTarget ?: AwsDeploymentTarget ;
150151 /**
151152 * Preview mode (diff): the managed-dependency sync runs check-only, computing the plan and a
152153 * future-tense notice without writing package.json or running npm install. Previews must never
@@ -220,7 +221,13 @@ const IDENTITY_STEP: Step = { label: LABEL_API_KEY, status: 'pending' };
220221const BOOTSTRAP_STEP : Step = { label : 'Bootstrap AWS environment' , status : 'pending' } ;
221222
222223export function useCdkPreflight ( options : PreflightOptions ) : PreflightResult {
223- const { logger, isInteractive = false , skipIdentityCheck = false , dependencySyncCheckOnly = false } = options ;
224+ const {
225+ logger,
226+ isInteractive = false ,
227+ skipIdentityCheck = false ,
228+ selectedTarget,
229+ dependencySyncCheckOnly = false ,
230+ } = options ;
224231
225232 // Create switchable ioHost - starts silent, can be flipped to verbose for deploy
226233 const switchableIoHost = useMemo ( ( ) => createSwitchableIoHost ( ) , [ ] ) ;
@@ -416,17 +423,18 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
416423 updateStep ( STEP_VALIDATE , { status : 'running' } ) ;
417424 logger . startStep ( 'Validate project' ) ;
418425 let preflightContext : PreflightContext ;
426+ let target : AwsDeploymentTarget | undefined ;
419427 try {
420- preflightContext = await validateProject ( ) ;
428+ preflightContext = await validateProject ( selectedTarget ) ;
429+ target = selectedTarget ?? preflightContext . awsTargets [ 0 ] ;
421430 setContext ( preflightContext ) ;
422431 // Make aws-targets.json region authoritative for downstream SDK / CDK
423432 // toolkit-lib clients that bypass explicit region options. Restored on
424433 // unmount, teardown rejection, or subsequent preflight start.
425434 // See https://github.com/aws/agentcore-cli/issues/924.
426- const firstTarget = preflightContext . awsTargets [ 0 ] ;
427- if ( firstTarget ) {
435+ if ( target ) {
428436 restoreRegionEnv ( ) ;
429- restoreRegionEnvRef . current = applyTargetRegionToEnv ( firstTarget . region ) ;
437+ restoreRegionEnvRef . current = applyTargetRegionToEnv ( target . region ) ;
430438 }
431439 logger . endStep ( 'success' ) ;
432440 updateStep ( STEP_VALIDATE , { status : 'success' } ) ;
@@ -460,7 +468,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
460468 // Validate AWS credentials (deferred for teardown deploys until after confirmation)
461469 if ( preflightContext . isTeardownDeploy ) {
462470 try {
463- await validateAwsCredentials ( ) ;
471+ await validateAwsCredentials ( target ) ;
464472 } catch ( err ) {
465473 const errorMsg = formatError ( err ) ;
466474 logger . endStep ( 'error' , errorMsg ) ;
@@ -584,7 +592,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
584592 // Set up payment resources (no-identity-providers path)
585593 const paymentOk = await runPaymentPreDeploy ( {
586594 projectSpec : preflightContext . projectSpec ,
587- awsTargets : preflightContext . awsTargets ,
595+ target : target ! ,
588596 logger,
589597 setSteps,
590598 updateStepByLabel,
@@ -625,7 +633,6 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
625633 }
626634
627635 // Step: Check stack status (ensure stacks are not in UPDATE_IN_PROGRESS etc.)
628- const target = preflightContext . awsTargets [ 0 ] ;
629636 if ( target && synthStackNames . length > 0 ) {
630637 updateStepByLabel ( LABEL_STACK_STATUS , { status : 'running' } ) ;
631638 logger . startStep ( 'Check stack status' ) ;
@@ -705,6 +712,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
705712 dependencySyncCheckOnly ,
706713 teardownConfirmed ,
707714 restoreRegionEnv ,
715+ selectedTarget ,
708716 ] ) ;
709717
710718 // Handle identity-setup phase (after user provides credentials)
@@ -722,7 +730,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
722730 // Set up payment resources even when identity is skipped
723731 const paymentOkSkip = await runPaymentPreDeploy ( {
724732 projectSpec : context . projectSpec ,
725- awsTargets : context . awsTargets ,
733+ target : ( selectedTarget ?? context . awsTargets [ 0 ] ) ! ,
726734 runtimeCredentials : runtimeCredentials ?? undefined ,
727735 logger,
728736 setSteps,
@@ -760,7 +768,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
760768 }
761769
762770 // Check stack status
763- const target = context . awsTargets [ 0 ] ;
771+ const target = selectedTarget ?? context . awsTargets [ 0 ] ;
764772 if ( target && synthStackNames . length > 0 ) {
765773 updateStepByLabel ( LABEL_STACK_STATUS , { status : 'running' } ) ;
766774 logger . startStep ( 'Check stack status' ) ;
@@ -824,7 +832,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
824832 logger . startStep ( 'Set up API key providers' ) ;
825833 }
826834
827- const target = context . awsTargets [ 0 ] ;
835+ const target = selectedTarget ?? context . awsTargets [ 0 ] ;
828836 if ( ! target ) {
829837 const errorMsg = 'No AWS target configured' ;
830838 if ( hasApiKeys ) {
@@ -953,7 +961,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
953961 if ( Object . keys ( deployedCredentials ) . length > 0 ) {
954962 setAllCredentials ( deployedCredentials ) ;
955963 const configIO = new ConfigIO ( ) ;
956- const target = context . awsTargets [ 0 ] ;
964+ const target = selectedTarget ?? context . awsTargets [ 0 ] ;
957965 const existingState = await configIO . readDeployedState ( ) . catch ( ( ) => ( { targets : { } } ) as DeployedState ) ;
958966 const targetState = existingState . targets ?. [ target ! . name ] ?? { resources : { } } ;
959967 targetState . resources ??= { } ;
@@ -968,7 +976,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
968976 // Set up payment resources (before CDK synth so ARNs are in deployed state)
969977 const paymentOkIdentity = await runPaymentPreDeploy ( {
970978 projectSpec : context . projectSpec ,
971- awsTargets : context . awsTargets ,
979+ target : ( selectedTarget ?? context . awsTargets [ 0 ] ) ! ,
972980 runtimeCredentials : runtimeCredentials ?? undefined ,
973981 logger,
974982 setSteps,
@@ -1071,7 +1079,7 @@ export function useCdkPreflight(options: PreflightOptions): PreflightResult {
10711079 } ;
10721080
10731081 void runIdentitySetup ( ) ;
1074- } , [ phase , context , skipIdentitySetup , runtimeCredentials , logger , switchableIoHost . ioHost ] ) ;
1082+ } , [ phase , context , skipIdentitySetup , runtimeCredentials , logger , switchableIoHost . ioHost , selectedTarget ] ) ;
10751083
10761084 // Handle bootstrapping phase
10771085 useEffect ( ( ) => {
0 commit comments