amazon/ebssurrogate: apply snapshot tags right when taking snapshot

This commit is contained in:
Nahum Shalman 2020-10-21 16:17:37 -04:00
parent 1621ab59d8
commit 83a672f2c9
2 changed files with 30 additions and 2 deletions

View File

@ -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,

View File

@ -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