Example #1
0
// copyAMI starts the AMI copy
func copyAMI(awsec2dest *ec2.EC2, s *session, amis *map[string]string) {
	if s.destRegion.Name != s.sourceRegion.Name {
		for amiId, instanceId := range *amis {
			backupAmiName := fmt.Sprintf("%s-%s-%s", s.instanceNameTag, timeStamp, amiId)
			backupDesc := fmt.Sprintf("%s %s %s", s.instanceNameTag, timeString, amiId)
			copyOpts := ec2.CopyImage{
				SourceRegion:  s.sourceRegion.Name,
				SourceImageId: amiId,
				Name:          backupAmiName,
				Description:   backupDesc,
				ClientToken:   "",
			}
			copyResp, err := awsec2dest.CopyImage(&copyOpts)
			if err != nil {
				s.fatal("EC2 API CopyImage failed")
			}
			s.debug(fmt.Sprintf("Started copy of %s from %s (%s) to %s (%s).", s.instanceNameTag, s.sourceRegion.Name, amiId, s.destRegion.Name, copyResp.ImageId))
			_, err = awsec2dest.CreateTags([]string{copyResp.ImageId}, []ec2.Tag{
				{"hostname", s.instanceNameTag},
				{"instance", instanceId},
				{"sourceregion", s.sourceRegion.Name},
				{"date", timeString},
				{"timestamp", timeSecs},
			})
			if err != nil {
				s.fatal(fmt.Sprintf("Error tagging new AMI: %s", err.Error()))
			}
		}
	} else {
		s.debug("Not copying AMI - source and dest regions match")
	}
}