diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 729ebce13..fa648dc96 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -44,10 +44,14 @@ type RunConfig struct { } func (c *RunConfig) Prepare(ctx *interpolate.Context) []error { - // if we are not given an explicit keypairname, create a temporary one + // If we are not given an explicit ssh_keypair_name, + // then create a temporary one, but only if the + // temporary_key_pair_name has not been provided. if c.SSHKeyPairName == "" { - c.TemporaryKeyPairName = fmt.Sprintf( - "packer %s", uuid.TimeOrderedUUID()) + if c.TemporaryKeyPairName == "" { + c.TemporaryKeyPairName = fmt.Sprintf( + "packer_%s", uuid.TimeOrderedUUID()) + } } if c.WindowsPasswordTimeout == 0 { diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index 0b029169b..7f307e288 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -3,6 +3,7 @@ package common import ( "io/ioutil" "os" + "regexp" "testing" "github.com/mitchellh/packer/helper/communicator" @@ -140,6 +141,21 @@ func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) { } if c.TemporaryKeyPairName == "" { - t.Fatal("keypair empty") + t.Fatal("keypair name is empty") + } + + // Match prefix and UUID, e.g. "packer_5790d491-a0b8-c84c-c9d2-2aea55086550". + r := regexp.MustCompile(`\Apacker_(?:(?i)[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}?)\z`) + if !r.MatchString(c.TemporaryKeyPairName) { + t.Fatal("keypair name is not valid") + } + + c.TemporaryKeyPairName = "ssh-key-123" + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("err: %s", err) + } + + if c.TemporaryKeyPairName != "ssh-key-123" { + t.Fatal("keypair name does not match") } } diff --git a/builder/amazon/common/step_run_source_instance.go b/builder/amazon/common/step_run_source_instance.go index 6eeb59604..abd9a5d50 100644 --- a/builder/amazon/common/step_run_source_instance.go +++ b/builder/amazon/common/step_run_source_instance.go @@ -156,16 +156,16 @@ func (s *StepRunSourceInstance) Run(state multistep.StateBag) multistep.StepActi if spotPrice == "" || spotPrice == "0" { runOpts := &ec2.RunInstancesInput{ - KeyName: &keyName, - ImageId: &s.SourceAMI, - InstanceType: &s.InstanceType, - UserData: &userData, - MaxCount: aws.Int64(1), - MinCount: aws.Int64(1), - IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile}, - BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), - Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone}, - EbsOptimized: &s.EbsOptimized, + KeyName: &keyName, + ImageId: &s.SourceAMI, + InstanceType: &s.InstanceType, + UserData: &userData, + MaxCount: aws.Int64(1), + MinCount: aws.Int64(1), + IamInstanceProfile: &ec2.IamInstanceProfileSpecification{Name: &s.IamInstanceProfile}, + BlockDeviceMappings: s.BlockDevices.BuildLaunchDevices(), + Placement: &ec2.Placement{AvailabilityZone: &s.AvailabilityZone}, + EbsOptimized: &s.EbsOptimized, } if s.SubnetId != "" && s.AssociatePublicIpAddress { diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 638efa5a3..7174d6b8e 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -117,21 +117,21 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe BlockDevices: b.config.BlockDevices, }, &awscommon.StepRunSourceInstance{ - Debug: b.config.PackerDebug, - ExpectedRootDevice: "ebs", - SpotPrice: b.config.SpotPrice, - SpotPriceProduct: b.config.SpotPriceAutoProduct, - InstanceType: b.config.InstanceType, - UserData: b.config.UserData, - UserDataFile: b.config.UserDataFile, - SourceAMI: b.config.SourceAmi, - IamInstanceProfile: b.config.IamInstanceProfile, - SubnetId: b.config.SubnetId, - AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, - EbsOptimized: b.config.EbsOptimized, - AvailabilityZone: b.config.AvailabilityZone, - BlockDevices: b.config.BlockDevices, - Tags: b.config.RunTags, + Debug: b.config.PackerDebug, + ExpectedRootDevice: "ebs", + SpotPrice: b.config.SpotPrice, + SpotPriceProduct: b.config.SpotPriceAutoProduct, + InstanceType: b.config.InstanceType, + UserData: b.config.UserData, + UserDataFile: b.config.UserDataFile, + SourceAMI: b.config.SourceAmi, + IamInstanceProfile: b.config.IamInstanceProfile, + SubnetId: b.config.SubnetId, + AssociatePublicIpAddress: b.config.AssociatePublicIpAddress, + EbsOptimized: b.config.EbsOptimized, + AvailabilityZone: b.config.AvailabilityZone, + BlockDevices: b.config.BlockDevices, + Tags: b.config.RunTags, InstanceInitiatedShutdownBehavior: b.config.InstanceInitiatedShutdownBehavior, }, &stepTagEBSVolumes{