Change EBS builder to do tag-on-creation

The EBS builder will now use the tag-on-creation pattern, so
that it's possible to restrict packer to only create volumes that are
properly tagged by using an AWS policy.
This commit is contained in:
Mark Meyer 2017-10-03 00:05:40 +02:00
parent 732e884105
commit cfd6b6fed5
2 changed files with 26 additions and 5 deletions

View File

@ -33,6 +33,7 @@ type StepRunSourceInstance struct {
SpotPriceProduct string SpotPriceProduct string
SubnetId string SubnetId string
Tags map[string]string Tags map[string]string
VolumeTags map[string]string
UserData string UserData string
UserDataFile string UserDataFile string
Ctx interpolate.Context Ctx interpolate.Context
@ -151,6 +152,14 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
} }
ReportTags(ui, ec2Tags) ReportTags(ui, ec2Tags)
volTags, err := ConvertToEC2Tags(s.VolumeTags, *ec2conn.Config.Region, s.SourceAMI, s.Ctx)
if err != nil {
err := fmt.Errorf("Error tagging volumes: %s", err)
state.Put("error", err)
ui.Error(err.Error())
return multistep.ActionHalt
}
if spotPrice == "" || spotPrice == "0" { if spotPrice == "" || spotPrice == "0" {
runOpts := &ec2.RunInstancesInput{ runOpts := &ec2.RunInstancesInput{
@ -165,16 +174,31 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi
EbsOptimized: &s.EbsOptimized, EbsOptimized: &s.EbsOptimized,
} }
var tagSpecs []*ec2.TagSpecification
if len(ec2Tags) > 0 { if len(ec2Tags) > 0 {
runTags := &ec2.TagSpecification{ runTags := &ec2.TagSpecification{
ResourceType: aws.String("instance"), ResourceType: aws.String("instance"),
Tags: ec2Tags, Tags: ec2Tags,
} }
runOpts.SetTagSpecifications([]*ec2.TagSpecification{runTags}) tagSpecs = append(tagSpecs, runTags)
createTagsAfterInstanceStarts = false createTagsAfterInstanceStarts = false
} }
if len(volTags) > 0 {
runVolTags := &ec2.TagSpecification{
ResourceType: aws.String("volume"),
Tags: volTags,
}
tagSpecs = append(tagSpecs, runVolTags)
}
if len(tagSpecs) > 0 {
runOpts.SetTagSpecifications(tagSpecs)
}
if keyName != "" { if keyName != "" {
runOpts.KeyName = &keyName runOpts.KeyName = &keyName
} }

View File

@ -152,13 +152,10 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
AvailabilityZone: b.config.AvailabilityZone, AvailabilityZone: b.config.AvailabilityZone,
BlockDevices: b.config.BlockDevices, BlockDevices: b.config.BlockDevices,
Tags: b.config.RunTags, Tags: b.config.RunTags,
VolumeTags: b.config.VolumeRunTags,
Ctx: b.config.ctx, Ctx: b.config.ctx,
InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior,
}, },
&awscommon.StepTagEBSVolumes{
VolumeRunTags: b.config.VolumeRunTags,
Ctx: b.config.ctx,
},
&awscommon.StepGetPassword{ &awscommon.StepGetPassword{
Debug: b.config.PackerDebug, Debug: b.config.PackerDebug,
Comm: &b.config.RunConfig.Comm, Comm: &b.config.RunConfig.Comm,