From a90c45d9bb3f2abd56ea77c8a456df19baaa60a7 Mon Sep 17 00:00:00 2001 From: Matthew Hooker Date: Thu, 7 Dec 2017 12:31:50 -0800 Subject: [PATCH] Wait until source instance OK before continuing --- .../amazon/common/step_run_source_instance.go | 22 ++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index a3dca8057..43a63493f 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -174,21 +174,27 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi ui.Message(fmt.Sprintf("Instance ID: %s", instanceId)) ui.Say(fmt.Sprintf("Waiting for instance (%v) to become ready...", instanceId)) - stateChange := StateChangeConf{ - Pending: []string{"pending"}, - Target: "running", - Refresh: InstanceStateRefreshFunc(ec2conn, instanceId), - StepState: state, + + describeInstanceStatus := &ec2.DescribeInstanceStatusInput{ + InstanceIds: []*string{aws.String(instanceId)}, } - latestInstance, err := WaitForState(&stateChange) - if err != nil { + if err := ec2conn.WaitUntilInstanceStatusOk(describeInstanceStatus); err != nil { err := fmt.Errorf("Error waiting for instance (%s) to become ready: %s", instanceId, err) state.Put("error", err) ui.Error(err.Error()) return multistep.ActionHalt } - instance := latestInstance.(*ec2.Instance) + r, err := ec2conn.DescribeInstances(&ec2.DescribeInstancesInput{ + InstanceIds: []*string{aws.String(instanceId)}, + }) + if err != nil || len(r.Reservations) == 0 || len(r.Reservations[0].Instances) == 0 { + err := fmt.Errorf("Error finding source instance.") + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + instance := r.Reservations[0].Instances[0] if s.Debug { if instance.PublicDnsName != nil && *instance.PublicDnsName != "" {