github.com/phobos182/packer@v0.2.3-0.20130819023704-c84d2aeffc68/builder/amazon/common/ami.go (about)

     1  package common
     2  
     3  import (
     4  	"github.com/mitchellh/goamz/ec2"
     5  	"log"
     6  	"time"
     7  )
     8  
     9  // WaitForAMI waits for the given AMI ID to become ready.
    10  func WaitForAMI(c *ec2.EC2, imageId string) error {
    11  	for {
    12  		imageResp, err := c.Images([]string{imageId}, ec2.NewFilter())
    13  		if err != nil {
    14  			if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidAMIID.NotFound" {
    15  				log.Println("AMI not found, probably state issues on AWS side. Trying again.")
    16  				continue
    17  			}
    18  
    19  			return err
    20  		}
    21  
    22  		if imageResp.Images[0].State == "available" {
    23  			return nil
    24  		}
    25  
    26  		log.Printf("Image in state %s, sleeping 2s before checking again",
    27  			imageResp.Images[0].State)
    28  		time.Sleep(2 * time.Second)
    29  	}
    30  }