diff --git a/builder/amazon/chroot/builder.go b/builder/amazon/chroot/builder.go index a8b20276a..052429e2e 100644 --- a/builder/amazon/chroot/builder.go +++ b/builder/amazon/chroot/builder.go @@ -44,6 +44,7 @@ type Config struct { RootVolumeSize int64 `mapstructure:"root_volume_size"` SourceAmi string `mapstructure:"source_ami"` SourceAmiFilter awscommon.AmiFilterOptions `mapstructure:"source_ami_filter"` + RootVolumeTags awscommon.TagMap `mapstructure:"root_volume_tags"` ctx interpolate.Context } @@ -67,6 +68,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { "ami_description", "snapshot_tags", "tags", + "root_volume_tags", "command_wrapper", "post_mount_commands", "pre_mount_commands", @@ -230,6 +232,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &StepPrepareDevice{}, &StepCreateVolume{ RootVolumeSize: b.config.RootVolumeSize, + RootVolumeTags: b.config.RootVolumeTags, + Ctx: b.config.ctx, }, &StepAttachVolume{}, &StepEarlyUnflock{}, diff --git a/builder/amazon/chroot/step_create_volume.go b/builder/amazon/chroot/step_create_volume.go index 725c8240b..4adf3074f 100644 --- a/builder/amazon/chroot/step_create_volume.go +++ b/builder/amazon/chroot/step_create_volume.go @@ -10,6 +10,7 @@ import ( awscommon "github.com/hashicorp/packer/builder/amazon/common" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" + "github.com/hashicorp/packer/template/interpolate" ) // StepCreateVolume creates a new volume from the snapshot of the root @@ -20,6 +21,8 @@ import ( type StepCreateVolume struct { volumeId string RootVolumeSize int64 + RootVolumeTags awscommon.TagMap + Ctx interpolate.Context } func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -28,6 +31,26 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu instance := state.Get("instance").(*ec2.Instance) ui := state.Get("ui").(packer.Ui) + volTags, err := s.RootVolumeTags.EC2Tags(s.Ctx, *ec2conn.Config.Region, state) + if err != nil { + err := fmt.Errorf("Error tagging volumes: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + // Collect tags for tagging on resource creation + var tagSpecs []*ec2.TagSpecification + + if len(volTags) > 0 { + runVolTags := &ec2.TagSpecification{ + ResourceType: aws.String("volume"), + Tags: volTags, + } + + tagSpecs = append(tagSpecs, runVolTags) + } + var createVolume *ec2.CreateVolumeInput if config.FromScratch { createVolume = &ec2.CreateVolumeInput{ @@ -69,6 +92,10 @@ func (s *StepCreateVolume) Run(ctx context.Context, state multistep.StateBag) mu } } + if len(tagSpecs) > 0 { + createVolume.SetTagSpecifications(tagSpecs) + volTags.Report(ui) + } log.Printf("Create args: %+v", createVolume) createVolumeResp, err := ec2conn.CreateVolume(createVolume) diff --git a/website/source/docs/builders/amazon-chroot.html.md b/website/source/docs/builders/amazon-chroot.html.md index 9c16fc320..116fd9ae5 100644 --- a/website/source/docs/builders/amazon-chroot.html.md +++ b/website/source/docs/builders/amazon-chroot.html.md @@ -254,6 +254,11 @@ each category, the available configuration keys are alphabetized. of the `source_ami` unless `from_scratch` is `true`, in which case this field must be defined. +- `root_volume_tags` (object of key/value strings) - Tags to apply to the volumes + that are *launched*. This is a + [template engine](/docs/templates/engine.html), + see [Build template data](#build-template-data) for more information. + - `skip_region_validation` (boolean) - Set to true if you want to skip validation of the `ami_regions` configuration option. Default `false`.