diff --git a/builder/amazon/common/run_config.go b/builder/amazon/common/run_config.go index 7ed6f85a6..6c162d052 100644 --- a/builder/amazon/common/run_config.go +++ b/builder/amazon/common/run_config.go @@ -22,6 +22,7 @@ type RunConfig struct { SecurityGroupId string `mapstructure:"security_group_id"` SubnetId string `mapstructure:"subnet_id"` VpcId string `mapstructure:"vpc_id"` + SSHKeyPairPattern string `mapstructure:"ssh_keypair_pattern"` // Unexported fields that are calculated from others sshTimeout time.Duration @@ -45,6 +46,10 @@ func (c *RunConfig) Prepare(t *packer.ConfigTemplate) []error { c.RawSSHTimeout = "1m" } + if c.SSHKeyPairPattern == "" { + c.SSHKeyPairPattern = "packer %s" + } + // Validation var err error errs := make([]error, 0) diff --git a/builder/amazon/common/run_config_test.go b/builder/amazon/common/run_config_test.go index d13320686..a9f054fc2 100644 --- a/builder/amazon/common/run_config_test.go +++ b/builder/amazon/common/run_config_test.go @@ -126,3 +126,24 @@ func TestRunConfigPrepare_UserDataFile(t *testing.T) { t.Fatalf("err: %s", err) } } + +func TestRunConfigPrepare_SSHKeyPairPattern(t *testing.T) { + c := testConfig() + c.SSHKeyPairPattern = "" + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("err: %s", err) + } + + if c.SSHKeyPairPattern != "packer %s" { + t.Fatalf("invalid value: %s", c.SSHKeyPairPattern) + } + + c.SSHKeyPairPattern = "valid-%s" + if err := c.Prepare(nil); len(err) != 0 { + t.Fatalf("err: %s", err) + } + + if c.SSHKeyPairPattern != "valid-%s" { + t.Fatalf("invalid value: %s", c.SSHKeyPairPattern) + } +} diff --git a/builder/amazon/common/step_key_pair.go b/builder/amazon/common/step_key_pair.go index 63c0247bc..772e390c5 100644 --- a/builder/amazon/common/step_key_pair.go +++ b/builder/amazon/common/step_key_pair.go @@ -13,8 +13,9 @@ import ( ) type StepKeyPair struct { - Debug bool - DebugKeyPath string + Debug bool + DebugKeyPath string + KeyPairPattern string keyName string } @@ -24,7 +25,7 @@ func (s *StepKeyPair) Run(state multistep.StateBag) multistep.StepAction { ui := state.Get("ui").(packer.Ui) ui.Say("Creating temporary keypair for this instance...") - keyName := fmt.Sprintf("packer %s", hex.EncodeToString(identifier.NewUUID().Raw())) + keyName := fmt.Sprintf(s.KeyPairPattern, hex.EncodeToString(identifier.NewUUID().Raw())) log.Printf("temporary keypair name: %s", keyName) keyResp, err := ec2conn.CreateKeyPair(keyName) if err != nil { diff --git a/builder/amazon/ebs/builder.go b/builder/amazon/ebs/builder.go index 5c6cd16dd..59de87b33 100644 --- a/builder/amazon/ebs/builder.go +++ b/builder/amazon/ebs/builder.go @@ -82,8 +82,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe // Build the steps steps := []multistep.Step{ &awscommon.StepKeyPair{ - Debug: b.config.PackerDebug, - DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + Debug: b.config.PackerDebug, + DebugKeyPath: fmt.Sprintf("ec2_%s.pem", b.config.PackerBuildName), + KeyPairPattern: b.config.SSHKeyPairPattern, }, &awscommon.StepSecurityGroup{ SecurityGroupId: b.config.SecurityGroupId,