Merge pull request #9730 from hashicorp/better_errors_9705

improve error handling when waiting for image so that the resourcenot…
This commit is contained in:
Megan Marsh 2020-08-07 09:11:39 -07:00 committed by GitHub
commit 46364ddf34
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 5 deletions

View File

@ -85,18 +85,24 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi
// Wait for the image to become ready // Wait for the image to become ready
ui.Say("Waiting for AMI to become ready...") ui.Say("Waiting for AMI to become ready...")
if err := awscommon.WaitUntilAMIAvailable(ctx, ec2conn, *createResp.ImageId); err != nil {
log.Printf("Error waiting for AMI: %s", err) if waitErr := awscommon.WaitUntilAMIAvailable(ctx, ec2conn, *createResp.ImageId); waitErr != nil {
// waitErr should get bubbled up if the issue is a wait timeout
err := fmt.Errorf("Error waiting for AMI: %s", waitErr)
imResp, imerr := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{createResp.ImageId}}) imResp, imerr := ec2conn.DescribeImages(&ec2.DescribeImagesInput{ImageIds: []*string{createResp.ImageId}})
if imerr != nil { if imerr != nil {
log.Printf("Unable to determine reason waiting for AMI failed: %s", err) // If there's a failure describing images, bubble that error up too, but don't erase the waitErr.
err = fmt.Errorf("Unknown error waiting for AMI; %s", err) log.Printf("DescribeImages call was unable to determine reason waiting for AMI failed: %s", imerr)
err = fmt.Errorf("Unknown error waiting for AMI; %s. DescribeImages returned an error: %s", waitErr, imerr)
} }
if imResp != nil && len(imResp.Images) > 0 { if imResp != nil && len(imResp.Images) > 0 {
// Finally, if there's a stateReason, store that with the wait err
image := imResp.Images[0] image := imResp.Images[0]
if image != nil { if image != nil {
stateReason := image.StateReason stateReason := image.StateReason
err = fmt.Errorf("Error waiting for AMI. Reason: %s", stateReason) if stateReason != nil {
err = fmt.Errorf("Error waiting for AMI: %s. DescribeImages returned the state reason: %s", waitErr, stateReason)
}
} }
} }
state.Put("error", err) state.Put("error", err)