Merge pull request #7818 from StileEducation/master

Allow EC2 Spot Fleet packer instances to run in parallel
This commit is contained in:
Megan Marsh 2019-07-01 09:47:40 -07:00 committed by GitHub
commit 1678d66e31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 3 deletions

View File

@ -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())