From 4bc381183dc7d533c6c300584c0224ba9a644c0a Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Wed, 9 Oct 2019 16:59:51 -0700 Subject: [PATCH] make aws instsance builder work with recent changes for volume copy --- builder/amazon/instance/builder.go | 1 + builder/amazon/instance/step_register_ami.go | 22 +++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/builder/amazon/instance/builder.go b/builder/amazon/instance/builder.go index bcdb00d91..766bb4b81 100644 --- a/builder/amazon/instance/builder.go +++ b/builder/amazon/instance/builder.go @@ -350,6 +350,7 @@ func (b *Builder) Run(ctx context.Context, ui packer.Ui, hook packer.Hook) (pack &StepRegisterAMI{ EnableAMISriovNetSupport: b.config.AMISriovNetSupport, EnableAMIENASupport: b.config.AMIENASupport, + AMISkipBuildRegion: b.config.AMISkipBuildRegion, }, &awscommon.StepAMIRegionCopy{ AccessConfig: &b.config.AccessConfig, diff --git a/builder/amazon/instance/step_register_ami.go b/builder/amazon/instance/step_register_ami.go index 1240064d2..9abee7a51 100644 --- a/builder/amazon/instance/step_register_ami.go +++ b/builder/amazon/instance/step_register_ami.go @@ -7,6 +7,7 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/ec2" awscommon "github.com/hashicorp/packer/builder/amazon/common" + "github.com/hashicorp/packer/common/random" confighelper "github.com/hashicorp/packer/helper/config" "github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/packer" @@ -15,6 +16,7 @@ import ( type StepRegisterAMI struct { EnableAMIENASupport confighelper.Trilean EnableAMISriovNetSupport bool + AMISkipBuildRegion bool } func (s *StepRegisterAMI) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction { @@ -24,9 +26,27 @@ func (s *StepRegisterAMI) Run(ctx context.Context, state multistep.StateBag) mul ui := state.Get("ui").(packer.Ui) ui.Say("Registering the AMI...") + + // Create the image + amiName := config.AMIName + state.Put("intermediary_image", false) + if config.AMIEncryptBootVolume.True() || s.AMISkipBuildRegion { + state.Put("intermediary_image", true) + + // From AWS SDK docs: You can encrypt a copy of an unencrypted snapshot, + // but you cannot use it to create an unencrypted copy of an encrypted + // snapshot. Your default CMK for EBS is used unless you specify a + // non-default key using KmsKeyId. + + // If encrypt_boot is nil or true, we need to create a temporary image + // so that in step_region_copy, we can copy it with the correct + // encryption + amiName = random.AlphaNum(7) + } + registerOpts := &ec2.RegisterImageInput{ ImageLocation: &manifestPath, - Name: aws.String(config.AMIName), + Name: aws.String(amiName), BlockDeviceMappings: config.AMIMappings.BuildEC2BlockDeviceMappings(), }