// It copies the given source ami to target regions.
It copies the given source ami to target regions.
// It copies the given source ami to target regions .
func (r *Resource) copyAmi(sess *session.Session, sai SrcAmiInfo, tags []*ec2.Tag, errMap map[string]error) {
	defer wg.Done()
	svc := ec2.New(sess)
	amiMeta := awscommon.AmiMeta{Name: sai.Image.Name}
	defer func() {
		r.Record.TargetImages[sess.Config.Region] = amiMeta
	}()
	filters := make([]*ec2.Filter, 0)
	if len(tags) != 0 {
		for _, tag := range tags {
			filter := &ec2.Filter{
				Name:   aws.String(fmt.Sprintf("tag:%s", *tag.Key)),
				Values: []*string{tag.Value},
			}
			filters = append(filters, filter)
		}
	}
	targetAmiFilters := []*ec2.Filter{{
		Name:   aws.String("name"),
		Values: []*string{amiMeta.Name}}}
	targetAmiFilters = append(targetAmiFilters, filters...)
	ok, err := awscommon.IsAmiExist(svc, targetAmiFilters)
	if ok {
		ci := awscommon.AwsClientInfo{
			SVC:    ec2.New(sess),
			Region: sess.Config.Region,
		}
		images, _ := awscommon.GetAmiInfo(ci, targetAmiFilters)
		clogger.Warnf("AMI %s(%s) Already Exist In Account %s In Region %s", *sai.Image.Name, *images[0].ImageId, *sai.AccountID, *sess.Config.Region)
		amiMeta.ID = images[0].ImageId
		return
	} else if err != nil {
		errMap[*sess.Config.Region] = err
		return
	}
	clogger.Infof("Started Copying AMI In Account: %s Region: %s ...", *sai.AccountID, *sess.Config.Region)
	input := &ec2.CopyImageInput{
		Description:   sai.Image.Description,
		Name:          sai.Image.Name,
		SourceImageId: sai.Image.ImageId,
		SourceRegion:  sai.Region,
	}
	result, err := svc.CopyImage(input)
	if ok, err := awscommon.IsError(err); ok {
		errMap[*sess.Config.Region] = err
		return
	}
	err = svc.WaitUntilImageAvailable(&ec2.DescribeImagesInput{ImageIds: []*string{result.ImageId}})
	if err != nil {
		errMap[*sess.Config.Region] = err
		return
	}
	clogger.Infof("Copied AMI In Account: %s In Region: %s , New AMI Id Is: %s", *sai.AccountID, *sess.Config.Region, *result.ImageId)
	amiMeta.ID = result.ImageId
	if len(tags) == 0 {
		clogger.Debug("No Tags To Add Or Create")
		return
	}
	clogger.Debugf("Adding Following Tags to AMI: %s", *result.ImageId)
	clogger.Debug(tags)
	if err := awscommon.CreateEc2Tags(svc, []*string{result.ImageId}, tags); err != nil {
		errMap[*sess.Config.Region] = err
		return
	}
	clogger.Infof("Tags Have Copied/Added To AMI : %s , In Region: %s", *result.ImageId, *sess.Config.Region)
}
func ( r * Resource ) copyAmi ( sess * session . Session , sai SrcAmiInfo , tags [ ] * ec2 . Tag , errMap map [ string ] error ) { defer wg . Done ( ) svc := ec2 . New ( sess ) amiMeta := awscommon . AmiMeta { Name : sai . Image . Name } defer func ( ) { r . Record . TargetImages [ sess . Config . Region ] = amiMeta } ( ) filters := make ( [ ] * ec2 . Filter , 0 ) if len ( tags ) != 0 { for _ , tag := range tags { filter := & ec2 . Filter { Name : aws . String ( fmt . Sprintf ( " " , * tag . Key ) ) , Values : [ ] * string { tag . Value } , } filters = append ( filters , filter ) } } targetAmiFilters := [ ] * ec2 . Filter { { Name : aws . String ( " " ) , Values : [ ] * string { amiMeta . Name } } } targetAmiFilters = append ( targetAmiFilters , filters ... ) ok , err := awscommon . IsAmiExist ( svc , targetAmiFilters ) if ok { ci := awscommon . AwsClientInfo { SVC : ec2 . New ( sess ) , Region : sess . Config . Region , } images , _ := awscommon . GetAmiInfo ( ci , targetAmiFilters ) clogger . Warnf ( " " , * sai . Image . Name , * images [ 0 ] . ImageId , * sai . AccountID , * sess . Config . Region ) amiMeta . ID = images [ 0 ] . ImageId return } else if err != nil { errMap [ * sess . Config . Region ] = err return } clogger . Infof ( " " , * sai . AccountID , * sess . Config . Region ) input := & ec2 . CopyImageInput { Description : sai . Image . Description , Name : sai . Image . Name , SourceImageId : sai . Image . ImageId , SourceRegion : sai . Region , } result , err := svc . CopyImage ( input ) if ok , err := awscommon . IsError ( err ) ; ok { errMap [ * sess . Config . Region ] = err return } err = svc . WaitUntilImageAvailable ( & ec2 . DescribeImagesInput { ImageIds : [ ] * string { result . ImageId } } ) if err != nil { errMap [ * sess . Config . Region ] = err return } clogger . Infof ( " " , * sai . AccountID , * sess . Config . Region , * result . ImageId ) amiMeta . ID = result . ImageId if len ( tags ) == 0 { clogger . Debug ( " " ) return } clogger . Debugf ( " " , * result . ImageId ) clogger . Debug ( tags ) if err := awscommon . CreateEc2Tags ( svc , [ ] * string { result . ImageId } , tags ) ; err != nil { errMap [ * sess . Config . Region ] = err return } clogger . Infof ( " " , * result . ImageId , * sess . Config . Region ) }
It copies the given source ami to target regions.
It copies the given source ami to target regions .