From 83a672f2c946a21779cf23cec71893dbf28f5cab Mon Sep 17 00:00:00 2001 From: Nahum Shalman Date: Wed, 21 Oct 2020 16:17:37 -0400 Subject: [PATCH] amazon/ebssurrogate: apply snapshot tags right when taking snapshot --- builder/amazon/ebssurrogate/builder.go | 2 ++ .../ebssurrogate/step_snapshot_volumes.go | 30 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/builder/amazon/ebssurrogate/builder.go b/builder/amazon/ebssurrogate/builder.go index d938cdd46..ec4957ce5 100644 --- a/builder/amazon/ebssurrogate/builder.go +++ b/builder/amazon/ebssurrogate/builder.go @@ -333,6 +333,8 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack PollingConfig: b.config.PollingConfig, LaunchDevices: launchDevices, SnapshotOmitMap: b.config.LaunchMappings.GetOmissions(), + SnapshotTags: b.config.SnapshotTags, + Ctx: b.config.ctx, }, &awscommon.StepDeregisterAMI{ AccessConfig: &b.config.AccessConfig, diff --git a/builder/amazon/ebssurrogate/step_snapshot_volumes.go b/builder/amazon/ebssurrogate/step_snapshot_volumes.go index 60cb393f2..543b9edb5 100644 --- a/builder/amazon/ebssurrogate/step_snapshot_volumes.go +++ b/builder/amazon/ebssurrogate/step_snapshot_volumes.go @@ -6,11 +6,13 @@ import ( "sync" "time" + "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" multierror "github.com/hashicorp/go-multierror" 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" ) // StepSnapshotVolumes creates snapshots of the created volumes. @@ -23,6 +25,8 @@ type StepSnapshotVolumes struct { snapshotIds map[string]string snapshotMutex sync.Mutex SnapshotOmitMap map[string]bool + SnapshotTags map[string]string + Ctx interpolate.Context } func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName string, state multistep.StateBag) error { @@ -40,12 +44,34 @@ func (s *StepSnapshotVolumes) snapshotVolume(ctx context.Context, deviceName str return fmt.Errorf("Volume ID for device %s not found", deviceName) } + ui.Say("Creating snapshot tags") + snapshotTags, err := awscommon.TagMap(s.SnapshotTags).EC2Tags(s.Ctx, *ec2conn.Config.Region, state) + if err != nil { + state.Put("error", err) + ui.Error(err.Error()) + return err + } + snapshotTags.Report(ui) + ui.Say(fmt.Sprintf("Creating snapshot of EBS Volume %s...", volumeId)) description := fmt.Sprintf("Packer: %s", time.Now().String()) + // Collect tags for tagging on resource creation + var tagSpecs []*ec2.TagSpecification + + if len(snapshotTags) > 0 { + snapTags := &ec2.TagSpecification{ + ResourceType: aws.String("snapshot"), + Tags: snapshotTags, + } + + tagSpecs = append(tagSpecs, snapTags) + } + createSnapResp, err := ec2conn.CreateSnapshot(&ec2.CreateSnapshotInput{ - VolumeId: &volumeId, - Description: &description, + VolumeId: &volumeId, + Description: &description, + TagSpecifications: tagSpecs, }) if err != nil { return err