diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index 013394ee7..f2f3a10e8 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -189,15 +189,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepEarlyCleanup{}, &StepSnapshot{}, &StepRegisterAMI{}, + &awscommon.StepAMIRegionCopy{ + Regions: b.config.AMIRegions, + }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, Users: b.config.AMIUsers, Groups: b.config.AMIGroups, }, - &awscommon.StepAMIRegionCopy{ - Regions: b.config.AMIRegions, - Tags: b.config.AMITags, - }, &awscommon.StepCreateTags{ Tags: b.config.AMITags, }, diff --git a/builder/amazon/common/step_ami_region_copy.go b/builder/amazon/common/step_ami_region_copy.go index 109e6de6d..8d50f5dfc 100644 --- a/builder/amazon/common/step_ami_region_copy.go +++ b/builder/amazon/common/step_ami_region_copy.go @@ -10,7 +10,6 @@ import ( type StepAMIRegionCopy struct { Regions []string - Tags map[string]string } func (s *StepAMIRegionCopy) Run(state multistep.StateBag) multistep.StepAction { @@ -49,25 +48,6 @@ func (s *StepAMIRegionCopy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - // Need to re-apply Tags since they are not copied with the AMI - if len(s.Tags) > 0 { - ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", resp.ImageId)) - - var ec2Tags []ec2.Tag - for key, value := range s.Tags { - ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value)) - ec2Tags = append(ec2Tags, ec2.Tag{key, value}) - } - - _, err := regionconn.CreateTags([]string{resp.ImageId}, ec2Tags) - if err != nil { - err := fmt.Errorf("Error adding tags to AMI (%s): %s", resp.ImageId, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - } - amis[region] = resp.ImageId } diff --git a/builder/amazon/common/step_create_tags.go b/builder/amazon/common/step_create_tags.go index 030211509..a204ca321 100644 --- a/builder/amazon/common/step_create_tags.go +++ b/builder/amazon/common/step_create_tags.go @@ -2,6 +2,7 @@ package common import ( "fmt" + "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" @@ -15,23 +16,25 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { ec2conn := state.Get("ec2").(*ec2.EC2) ui := state.Get("ui").(packer.Ui) amis := state.Get("amis").(map[string]string) - ami := amis[ec2conn.Region.Name] if len(s.Tags) > 0 { - ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami)) + for region, ami := range amis { + ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami)) - var ec2Tags []ec2.Tag - for key, value := range s.Tags { - ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value)) - ec2Tags = append(ec2Tags, ec2.Tag{key, value}) - } + var ec2Tags []ec2.Tag + for key, value := range s.Tags { + ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value)) + ec2Tags = append(ec2Tags, ec2.Tag{key, value}) + } - _, err := ec2conn.CreateTags([]string{ami}, ec2Tags) - if err != nil { - err := fmt.Errorf("Error adding tags to AMI (%s): %s", ami, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + regionconn := ec2.New(ec2conn.Auth, aws.Regions[region]) + _, err := regionconn.CreateTags([]string{ami}, ec2Tags) + if err != nil { + err := fmt.Errorf("Error adding tags to AMI (%s): %s", ami, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } } diff --git a/builder/amazon/common/step_modify_ami_attributes.go b/builder/amazon/common/step_modify_ami_attributes.go index 25ba79c2c..533d4cfd9 100644 --- a/builder/amazon/common/step_modify_ami_attributes.go +++ b/builder/amazon/common/step_modify_ami_attributes.go @@ -2,6 +2,7 @@ package common import ( "fmt" + "github.com/mitchellh/goamz/aws" "github.com/mitchellh/goamz/ec2" "github.com/mitchellh/multistep" "github.com/mitchellh/packer/packer" @@ -18,7 +19,6 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc ec2conn := state.Get("ec2").(*ec2.EC2) ui := state.Get("ui").(packer.Ui) amis := state.Get("amis").(map[string]string) - ami := amis[ec2conn.Region.Name] // Determine if there is any work to do. valid := false @@ -59,15 +59,18 @@ func (s *StepModifyAMIAttributes) Run(state multistep.StateBag) multistep.StepAc } } - ui.Say("Modifying AMI attributes...") - for name, opts := range options { - ui.Message(fmt.Sprintf("Modifying: %s", name)) - _, err := ec2conn.ModifyImageAttribute(ami, opts) - if err != nil { - err := fmt.Errorf("Error modify AMI attributes: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt + for region, ami := range amis { + ui.Say(fmt.Sprintf("Modifying attributes on AMI (%s)...", ami)) + regionconn := ec2.New(ec2conn.Auth, aws.Regions[region]) + for name, opts := range options { + ui.Message(fmt.Sprintf("Modifying: %s", name)) + _, err := regionconn.ModifyImageAttribute(ami, opts) + if err != nil { + err := fmt.Errorf("Error modify AMI attributes: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } } } diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 5c6cd16dd..ca899ab6f 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -109,15 +109,14 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &common.StepProvision{}, &stepStopInstance{}, &stepCreateAMI{}, + &awscommon.StepAMIRegionCopy{ + Regions: b.config.AMIRegions, + }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, Users: b.config.AMIUsers, Groups: b.config.AMIGroups, }, - &awscommon.StepAMIRegionCopy{ - Regions: b.config.AMIRegions, - Tags: b.config.AMITags, - }, &awscommon.StepCreateTags{ Tags: b.config.AMITags, }, diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index 2fd5b0664..10fd3516e 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -214,16 +214,15 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepBundleVolume{}, &StepUploadBundle{}, &StepRegisterAMI{}, + &awscommon.StepAMIRegionCopy{ + Regions: b.config.AMIRegions, + }, &awscommon.StepModifyAMIAttributes{ Description: b.config.AMIDescription, Users: b.config.AMIUsers, Groups: b.config.AMIGroups, ProductCodes: b.config.AMIProductCodes, }, - &awscommon.StepAMIRegionCopy{ - Regions: b.config.AMIRegions, - Tags: b.config.AMITags, - }, &awscommon.StepCreateTags{ Tags: b.config.AMITags, },