@ -14,6 +14,7 @@ import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
"github.com/hashicorp/packer/common/random"
"github.com/hashicorp/packer/common/retry"
"github.com/hashicorp/packer/helper/communicator"
"github.com/hashicorp/packer/helper/multistep"
@ -259,6 +260,14 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
// Create a launch template for the instance
ui . Message ( "Loading User Data File..." )
// Generate a random name to avoid conflicting with other
// instances of packer running in this AWS account
launchTemplateName := fmt . Sprintf (
"packer-fleet-launch-template-%s" ,
random . AlphaNum ( 7 ) )
state . Put ( "launchTemplateName" , launchTemplateName ) // For the cleanup step
userData , err := s . LoadUserData ( )
if err != nil {
state . Put ( "error" , err )
@ -268,7 +277,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
templateData := s . CreateTemplateData ( & userData , az , state , marketOptions )
launchTemplate := & ec2 . CreateLaunchTemplateInput {
LaunchTemplateData : templateData ,
LaunchTemplateName : aws . String ( "packer-fleet-launch-template" ) ,
LaunchTemplateName : aws . String ( launchTemplateName ) ,
VersionDescription : aws . String ( "template generated by packer for launching spot instances" ) ,
}
@ -294,7 +303,7 @@ func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag)
LaunchTemplateConfigs : [ ] * ec2 . FleetLaunchTemplateConfigRequest {
{
LaunchTemplateSpecification : & ec2 . FleetLaunchTemplateSpecificationRequest {
LaunchTemplateName : aws . String ( "packer-fleet-launch-template" ) ,
LaunchTemplateName : aws . String ( launchTemplateName ) ,
Version : aws . String ( "1" ) ,
} ,
Overrides : overrides ,
@ -460,6 +469,7 @@ func (s *StepRunSpotInstance) Cleanup(state multistep.StateBag) {
ec2conn := state . Get ( "ec2" ) . ( * ec2 . EC2 )
ui := state . Get ( "ui" ) . ( packer . Ui )
launchTemplateName := state . Get ( "launchTemplateName" ) . ( string )
// Cancel the spot request if it exists
if s . spotRequest != nil {
@ -494,7 +504,7 @@ func (s *StepRunSpotInstance) Cleanup(state multistep.StateBag) {
// Delete the launch template used to create the spot fleet
deleteInput := & ec2 . DeleteLaunchTemplateInput {
LaunchTemplateName : aws . String ( "packer-fleet-launch-template" ) ,
LaunchTemplateName : aws . String ( launchTemplateName ) ,
}
if _ , err := ec2conn . DeleteLaunchTemplate ( deleteInput ) ; err != nil {
ui . Error ( err . Error ( ) )