aws: when building an AMI with 'encrypt_boot: true', randomize the name of the temporary AMI

This commit is contained in:
Adrien Delorme 2018-09-03 15:01:14 +02:00
parent 170b2f3383
commit b80e1d51c6
1 changed files with 10 additions and 2 deletions

View File

@ -7,6 +7,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
awscommon "github.com/hashicorp/packer/builder/amazon/common" awscommon "github.com/hashicorp/packer/builder/amazon/common"
"github.com/hashicorp/packer/common/random"
"github.com/hashicorp/packer/helper/multistep" "github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer" "github.com/hashicorp/packer/packer"
) )
@ -22,10 +23,17 @@ func (s *stepCreateAMI) Run(ctx context.Context, state multistep.StateBag) multi
ui := state.Get("ui").(packer.Ui) ui := state.Get("ui").(packer.Ui)
// Create the image // Create the image
ui.Say(fmt.Sprintf("Creating the AMI: %s", config.AMIName)) amiName := config.AMIName
if config.AMIEncryptBootVolume {
// to avoid having a temporary unencrypted
// image named config.AMIName
amiName = random.AlphaNum(7)
}
ui.Say(fmt.Sprintf("Creating unencrypted AMI %s from instance %s", amiName, *instance.InstanceId))
createOpts := &ec2.CreateImageInput{ createOpts := &ec2.CreateImageInput{
InstanceId: instance.InstanceId, InstanceId: instance.InstanceId,
Name: &config.AMIName, Name: &amiName,
BlockDeviceMappings: config.BlockDevices.BuildAMIDevices(), BlockDeviceMappings: config.BlockDevices.BuildAMIDevices(),
} }