diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 52289ba50..60124db16 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -69,6 +69,10 @@ func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { c.WindowsPasswordTimeout = 10 * time.Minute } + if c.RunTags == nil { + c.RunTags = make(map[string]string) + } + // Validation errs := c.Comm.Prepare(ctx) if c.SourceAmi == "" && c.SourceAmiFilter.Empty() { diff --git a/builder/amazon/common/step_create_tags.go b/builder/amazon/common/step_create_tags.go index 87ce08840..4de8e50c3 100644 --- a/builder/amazon/common/step_create_tags.go +++ b/builder/amazon/common/step_create_tags.go @@ -31,9 +31,9 @@ func (s *StepCreateTags) Run(state multistep.StateBag) multistep.StepAction { ui.Say(fmt.Sprintf("Adding tags to AMI (%s)...", ami)) // Convert tags to ec2.Tag format - amiTags := ConvertToEC2Tags(s.Tags, ui) + amiTags := ConvertToEC2Tags(s.Tags) ui.Say(fmt.Sprintf("Snapshot tags:")) - snapshotTags := ConvertToEC2Tags(s.SnapshotTags, ui) + snapshotTags := ConvertToEC2Tags(s.SnapshotTags) // Declare list of resources to tag awsConfig := aws.Config{ @@ -128,10 +128,9 @@ func (s *StepCreateTags) Cleanup(state multistep.StateBag) { // No cleanup... } -func ConvertToEC2Tags(tags map[string]string, ui packer.Ui) []*ec2.Tag { +func ConvertToEC2Tags(tags map[string]string) []*ec2.Tag { var amiTags []*ec2.Tag for key, value := range tags { - ui.Message(fmt.Sprintf("Adding tag: \"%s\": \"%s\"", key, value)) amiTags = append(amiTags, &ec2.Tag{ Key: aws.String(key), Value: aws.String(value), diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 4b79131de..649ddfdec 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -291,19 +291,21 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi instance := latestInstance.(*ec2.Instance) - ec2Tags := make([]*ec2.Tag, 1, len(s.Tags)+1) - ec2Tags[0] = &ec2.Tag{Key: aws.String("Name"), Value: aws.String("Packer Builder")} - for k, v := range s.Tags { - ec2Tags = append(ec2Tags, &ec2.Tag{Key: aws.String(k), Value: aws.String(v)}) + ui.Say(fmt.Sprintf("Adding tags to source instance:")) + if _, exists := s.Tags["Name"]; !exists { + s.Tags["Name"] = "Packer Builder" } + ec2Tags := ConvertToEC2Tags(s.Tags) _, err = ec2conn.CreateTags(&ec2.CreateTagsInput{ Tags: ec2Tags, Resources: []*string{instance.InstanceId}, }) if err != nil { - ui.Message( - fmt.Sprintf("Failed to tag a Name on the builder instance: %s", err)) + err := fmt.Errorf("Error tagging source instance: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } if s.Debug { diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go index 2a4f8c047..ee5a26bb2 100644 --- a/builder/amazon/ebs/step_tag_ebs_volumes.go +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -5,6 +5,7 @@ import ( "github.com/aws/aws-sdk-go/service/ec2" "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/builder/amazon/common" "github.com/mitchellh/packer/packer" ) @@ -17,37 +18,33 @@ func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { instance := state.Get("instance").(*ec2.Instance) ui := state.Get("ui").(packer.Ui) - if len(s.VolumeRunTags) > 0 { - ui.Say("Tagging source EBS volumes...") - - volumeIds := make([]*string, 0) - for _, v := range instance.BlockDeviceMappings { - if ebs := v.Ebs; ebs != nil { - volumeIds = append(volumeIds, ebs.VolumeId) - } - } + if len(s.VolumeRunTags) == 0 { + return multistep.ActionContinue + } - if len(volumeIds) == 0 { - return multistep.ActionContinue + volumeIds := make([]*string, 0) + for _, v := range instance.BlockDeviceMappings { + if ebs := v.Ebs; ebs != nil { + volumeIds = append(volumeIds, ebs.VolumeId) } + } - tags := make([]*ec2.Tag, len(s.VolumeRunTags)) - for key, value := range s.VolumeRunTags { - tags = append(tags, &ec2.Tag{Key: &key, Value: &value}) - } + if len(volumeIds) == 0 { + return multistep.ActionContinue + } - _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ - Resources: []*string{ - instance.BlockDeviceMappings[0].Ebs.VolumeId, - }, - Tags: tags, - }) - if err != nil { - err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } + ui.Say(fmt.Sprintf("Adding tags to source EBS Volumes:")) + tags := common.ConvertToEC2Tags(s.VolumeRunTags) + + _, err := ec2conn.CreateTags(&ec2.CreateTagsInput{ + Resources: volumeIds, + Tags: tags, + }) + if err != nil { + err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt } return multistep.ActionContinue