diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 3c576f9e0..d8e8cea28 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -29,6 +29,7 @@ type Config struct { awscommon.AMIConfig `mapstructure:",squash"` awscommon.BlockDevices `mapstructure:",squash"` awscommon.RunConfig `mapstructure:",squash"` + VolumeRunTags map[string]string `mapstructure:"run_volume_tags"` ctx interpolate.Context } @@ -132,6 +133,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe BlockDevices: b.config.BlockDevices, Tags: b.config.RunTags, }, + &stepTagEBSVolumes{ + VolumeRunTags: b.config.VolumeRunTags, + }, &awscommon.StepGetPassword{ Debug: b.config.PackerDebug, Comm: &b.config.RunConfig.Comm, diff --git a/builder/amazon/ebs/step_tag_ebs_volumes.go b/builder/amazon/ebs/step_tag_ebs_volumes.go new file mode 100644 index 000000000..2a4f8c047 --- /dev/null +++ b/builder/amazon/ebs/step_tag_ebs_volumes.go @@ -0,0 +1,58 @@ +package ebs + +import ( + "fmt" + + "github.com/aws/aws-sdk-go/service/ec2" + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" +) + +type stepTagEBSVolumes struct { + VolumeRunTags map[string]string +} + +func (s *stepTagEBSVolumes) Run(state multistep.StateBag) multistep.StepAction { + ec2conn := state.Get("ec2").(*ec2.EC2) + 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(volumeIds) == 0 { + return multistep.ActionContinue + } + + tags := make([]*ec2.Tag, len(s.VolumeRunTags)) + for key, value := range s.VolumeRunTags { + tags = append(tags, &ec2.Tag{Key: &key, Value: &value}) + } + + _, 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 + } + } + + return multistep.ActionContinue +} + +func (s *stepTagEBSVolumes) Cleanup(state multistep.StateBag) { + // No cleanup... +} diff --git a/website/source/docs/builders/amazon-ebs.html.markdown b/website/source/docs/builders/amazon-ebs.html.markdown index 46c486c20..920cd10f5 100644 --- a/website/source/docs/builders/amazon-ebs.html.markdown +++ b/website/source/docs/builders/amazon-ebs.html.markdown @@ -138,6 +138,9 @@ builder. - `run_tags` (object of key/value strings) - Tags to apply to the instance that is *launched* to create the AMI. These tags are *not* applied to the resulting AMI unless they're duplicated in `tags`. +- `volume_run_tags` (object of key/value strings) - Tags to apply to the volumes + that are *launched* to create the AMI. These tags are *not* applied to the + resulting AMI unless they're duplicated in `tags`. - `security_group_id` (string) - The ID (*not* the name) of the security group to assign to the instance. By default this is not set and Packer will