@@ -185,26 +185,21 @@ func runBuildPhase1(cmd *cobra.Command, args []string) error {
185185 fmt .Printf ("\n ✓ Packer build completed successfully!\n " )
186186 fmt .Println ("✓ AMI created by packer" )
187187
188- // Parse AMI ID from packer output (it's already in stdout above )
189- // For now, use AWS API to find the AMI by tags
188+ // Find AMI by execution ID tag (injected by our template rewriter )
189+ var amiID string
190190 ec2Client , err := aws .NewEC2Client (ctx , region )
191191 if err != nil {
192192 fmt .Printf ("⚠ Could not create EC2 client to find AMI: %v\n " , err )
193- fmt .Println ("\n ✓ Build phase 1 complete!" )
194- return nil
195- }
196-
197- // Find AMI by execution ID tag
198- amiID , err := ec2Client .FindAMIByTag (ctx , "packerExecutionId" , executionID )
199- if err != nil {
200- fmt .Printf ("⚠ Could not find created AMI: %v\n " , err )
201- fmt .Println ("\n ✓ Build phase 1 complete!" )
202- return nil
193+ } else {
194+ amiID , err = ec2Client .FindAMIByTag (ctx , "packerExecutionId" , executionID )
195+ if err != nil {
196+ fmt .Printf ("⚠ Could not find created AMI: %v\n " , err )
197+ } else {
198+ fmt .Printf ("✓ AMI ID: %s\n " , amiID )
199+ }
203200 }
204201
205- fmt .Printf ("✓ AMI ID: %s\n " , amiID )
206-
207- // Save AMI ID to state for phase2
202+ // Save state even if we couldn't find the AMI
208203 stateFilePath := stateFile
209204 if stateFilePath == "" {
210205 stateFilePath , err = state .GetDefaultStateFile ()
@@ -215,14 +210,19 @@ func runBuildPhase1(cmd *cobra.Command, args []string) error {
215210 }
216211 }
217212
218- buildState := & state.State {
219- Region : region ,
220- PostgresVersion : postgresVersion ,
221- GitSHA : sha ,
213+ // Load existing state or create new
214+ buildState , err := state .LoadState (stateFilePath )
215+ if err != nil {
216+ buildState = & state.State {
217+ Region : region ,
218+ PostgresVersion : postgresVersion ,
219+ GitSHA : sha ,
220+ }
222221 }
222+
223223 buildState .SetPhaseState ("phase1" , & state.PhaseState {
224224 ExecutionID : executionID ,
225- AMIID : amiID ,
225+ AMIID : amiID , // Will be empty if not found
226226 Timestamp : time .Now ().Format (time .RFC3339 ),
227227 })
228228
@@ -233,8 +233,13 @@ func runBuildPhase1(cmd *cobra.Command, args []string) error {
233233 }
234234
235235 fmt .Println ("\n ✓ Build phase 1 complete!" )
236- fmt .Printf ("\n Next: Run phase 2 with:\n " )
237- fmt .Printf (" pg-ami-builder build phase2 --postgres-version %s\n " , postgresVersion )
236+ if amiID != "" {
237+ fmt .Printf ("\n Next: Run phase 2 with:\n " )
238+ fmt .Printf (" pg-ami-builder build phase2 --postgres-version %s\n " , postgresVersion )
239+ } else {
240+ fmt .Printf ("\n Note: AMI ID not automatically detected. You can manually add it to:\n " )
241+ fmt .Printf (" %s\n " , stateFilePath )
242+ }
238243 return nil
239244}
240245
@@ -398,7 +403,61 @@ func runBuildPhase2(cmd *cobra.Command, args []string) error {
398403
399404 fmt .Printf ("\n ✓ Packer build completed successfully!\n " )
400405 fmt .Println ("✓ Final production AMI created by packer" )
406+
407+ // Find AMI by execution ID tag (injected by our template rewriter)
408+ var amiID string
409+ ec2Client , err := aws .NewEC2Client (ctx , region )
410+ if err != nil {
411+ fmt .Printf ("⚠ Could not create EC2 client to find AMI: %v\n " , err )
412+ } else {
413+ amiID , err = ec2Client .FindAMIByTag (ctx , "packerExecutionId" , executionID )
414+ if err != nil {
415+ fmt .Printf ("⚠ Could not find created AMI: %v\n " , err )
416+ } else {
417+ fmt .Printf ("✓ AMI ID: %s\n " , amiID )
418+ }
419+ }
420+
421+ // Save state even if we couldn't find the AMI
422+ stateFilePath := stateFile
423+ if stateFilePath == "" {
424+ stateFilePath , err = state .GetDefaultStateFile ()
425+ if err != nil {
426+ fmt .Printf ("⚠ Could not get state file path: %v\n " , err )
427+ fmt .Println ("\n ✓ Build phase 2 complete!" )
428+ return nil
429+ }
430+ }
431+
432+ // Load existing state or create new
433+ buildState , err := state .LoadState (stateFilePath )
434+ if err != nil {
435+ buildState = & state.State {
436+ Region : region ,
437+ PostgresVersion : postgresVersion ,
438+ GitSHA : sha ,
439+ }
440+ }
441+
442+ buildState .SetPhaseState ("phase2" , & state.PhaseState {
443+ ExecutionID : executionID ,
444+ AMIID : amiID , // Will be empty if not found
445+ Timestamp : time .Now ().Format (time .RFC3339 ),
446+ })
447+
448+ if err := state .SaveState (stateFilePath , buildState ); err != nil {
449+ fmt .Printf ("⚠ Could not save state: %v\n " , err )
450+ } else {
451+ fmt .Printf ("✓ State saved to: %s\n " , stateFilePath )
452+ }
453+
401454 fmt .Println ("\n ✓ Build phase 2 complete!" )
455+ if amiID != "" {
456+ fmt .Printf ("\n Production AMI ready: %s\n " , amiID )
457+ } else {
458+ fmt .Printf ("\n Note: AMI ID not automatically detected. You can manually add it to:\n " )
459+ fmt .Printf (" %s\n " , stateFilePath )
460+ }
402461 return nil
403462}
404463
0 commit comments