@@ -2,7 +2,8 @@ import { ConfigIO, SecureCredentials } from '../../../lib';
22import type { AgentCoreMcpSpec , DeployedState } from '../../../schema' ;
33import { applyTargetRegionToEnv } from '../../aws' ;
44import { validateAwsCredentials } from '../../aws/account' ;
5- import { createSwitchableIoHost } from '../../cdk/toolkit-lib' ;
5+ import { CdkToolkitWrapper , createSwitchableIoHost } from '../../cdk/toolkit-lib' ;
6+ import type { SwitchableIoHost } from '../../cdk/toolkit-lib' ;
67import {
78 buildDeployedState ,
89 getStackOutputs ,
@@ -40,7 +41,9 @@ import {
4041} from '../../operations/deploy/post-deploy-config-bundles' ;
4142import { setupHttpGateways } from '../../operations/deploy/post-deploy-http-gateways' ;
4243import { enableOnlineEvalConfigs } from '../../operations/deploy/post-deploy-online-evals' ;
44+ import { toStackName } from '../import/import-utils' ;
4345import type { DeployResult } from './types' ;
46+ import { StackSelectionStrategy } from '@aws-cdk/toolkit-lib' ;
4447
4548export interface ValidatedDeployOptions {
4649 target : string ;
@@ -55,6 +58,38 @@ export interface ValidatedDeployOptions {
5558const AGENT_NEXT_STEPS = [ 'agentcore invoke' , 'agentcore status' ] ;
5659const MEMORY_ONLY_NEXT_STEPS = [ 'agentcore add agent' , 'agentcore status' ] ;
5760
61+ export async function runDiff (
62+ toolkitWrapper : CdkToolkitWrapper ,
63+ stackName : string ,
64+ switchableIoHost ?: SwitchableIoHost
65+ ) : Promise < void > {
66+ const diffIoHost = switchableIoHost ?? createSwitchableIoHost ( ) ;
67+ let hasDiffContent = false ;
68+ diffIoHost . setOnRawMessage ( ( code , _level , message ) => {
69+ if ( ! message ) return ;
70+ // I4002: formatted diff per stack, I4001: overall diff summary
71+ if ( code === 'CDK_TOOLKIT_I4002' || code === 'CDK_TOOLKIT_I4001' ) {
72+ hasDiffContent = true ;
73+ console . log ( message ) ;
74+ }
75+ } ) ;
76+ diffIoHost . setVerbose ( true ) ;
77+ await toolkitWrapper . diff ( {
78+ stacks : { strategy : StackSelectionStrategy . PATTERN_MUST_MATCH , patterns : [ stackName ] } ,
79+ } ) ;
80+ if ( ! hasDiffContent ) {
81+ console . log ( 'No stack differences detected.' ) ;
82+ }
83+ diffIoHost . setVerbose ( false ) ;
84+ diffIoHost . setOnRawMessage ( null ) ;
85+ }
86+
87+ export async function runDeploy ( toolkitWrapper : CdkToolkitWrapper , stackName : string ) : Promise < void > {
88+ await toolkitWrapper . deploy ( {
89+ stacks : { strategy : StackSelectionStrategy . PATTERN_MUST_MATCH , patterns : [ stackName ] } ,
90+ } ) ;
91+ }
92+
5893export async function handleDeploy ( options : ValidatedDeployOptions ) : Promise < DeployResult > {
5994 let toolkitWrapper = null ;
6095 let restoreEnv : ( ( ) => void ) | null = null ;
@@ -247,6 +282,8 @@ export async function handleDeploy(options: ValidatedDeployOptions): Promise<Dep
247282 const stackName = stackNames [ 0 ] ! ;
248283 endStep ( 'success' ) ;
249284
285+ const targetStackName = toStackName ( context . projectSpec . name , target . name ) ;
286+
250287 // Check if bootstrap needed
251288 startStep ( 'Check bootstrap status' ) ;
252289 const bootstrapCheck = await checkBootstrapNeeded ( context . awsTargets ) ;
@@ -296,23 +333,7 @@ export async function handleDeploy(options: ValidatedDeployOptions): Promise<Dep
296333 // Diff mode: run cdk diff and exit without deploying
297334 if ( options . diff ) {
298335 startStep ( 'Run CDK diff' ) ;
299- const diffIoHost = switchableIoHost ?? createSwitchableIoHost ( ) ;
300- let hasDiffContent = false ;
301- diffIoHost . setOnRawMessage ( ( code , _level , message ) => {
302- if ( ! message ) return ;
303- // I4002: formatted diff per stack, I4001: overall diff summary
304- if ( code === 'CDK_TOOLKIT_I4002' || code === 'CDK_TOOLKIT_I4001' ) {
305- hasDiffContent = true ;
306- console . log ( message ) ;
307- }
308- } ) ;
309- diffIoHost . setVerbose ( true ) ;
310- await toolkitWrapper . diff ( ) ;
311- if ( ! hasDiffContent ) {
312- console . log ( 'No stack differences detected.' ) ;
313- }
314- diffIoHost . setVerbose ( false ) ;
315- diffIoHost . setOnRawMessage ( null ) ;
336+ await runDiff ( toolkitWrapper , targetStackName , switchableIoHost ) ;
316337 endStep ( 'success' ) ;
317338
318339 logger . finalize ( true ) ;
@@ -339,7 +360,7 @@ export async function handleDeploy(options: ValidatedDeployOptions): Promise<Dep
339360 switchableIoHost . setVerbose ( true ) ;
340361 }
341362
342- await toolkitWrapper . deploy ( ) ;
363+ await runDeploy ( toolkitWrapper , targetStackName ) ;
343364
344365 // Disable verbose output
345366 if ( switchableIoHost ) {
0 commit comments