2017-10-04 05:29:38 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 18:32:33 -05:00
|
|
|
"context"
|
2017-10-04 05:29:38 -04:00
|
|
|
"encoding/base64"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
retry spot instance creation when an "Invalid IAM Instance Profile name" error pops up (#9810)
PutRolePolicy & AddRoleToInstanceProfile are eventually consistent but it is not possible to wait for them to be done here: https://github.com/hashicorp/packer/blob/0785c2f6fca9c22bf25528e0176042799dd79df9/builder/amazon/common/step_iam_instance_profile.go#L117-L134 which was causing the `CreateFleet` to fail (100% for me). So for now we retry a bit later. Waiting 5 seconds after the previously linked code also fixed this.
Test file:
```json
{
"builders": [
{
"type": "amazon-ebs",
"region": "eu-west-1",
"ami_name": "ubuntu-16.04 test {{timestamp}}",
"ami_description": "Ubuntu 16.04 LTS - expand root partition",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": [
"099720109477"
],
"most_recent": true
},
"spot_price": "0.03",
"spot_instance_types": [
"t2.small"
],
"encrypt_boot": true,
"ssh_username": "ubuntu",
"ssh_interface": "session_manager",
"temporary_iam_instance_profile_policy_document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"*"
],
"Resource": "*"
}
]
},
"communicator": "ssh"
}
]}
```
2020-08-25 04:10:32 -04:00
|
|
|
"strings"
|
2017-10-04 05:29:38 -04:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/aws/aws-sdk-go/aws"
|
|
|
|
"github.com/aws/aws-sdk-go/service/ec2"
|
2020-11-01 09:43:49 -05:00
|
|
|
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
|
2020-12-17 16:29:25 -05:00
|
|
|
"github.com/hashicorp/packer-plugin-sdk/communicator"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/multistep"
|
|
|
|
packersdk "github.com/hashicorp/packer-plugin-sdk/packer"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/random"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/retry"
|
|
|
|
"github.com/hashicorp/packer-plugin-sdk/template/interpolate"
|
2020-10-29 06:57:14 -04:00
|
|
|
"github.com/hashicorp/packer/builder/amazon/common/awserrors"
|
2017-10-04 05:29:38 -04:00
|
|
|
)
|
|
|
|
|
2019-06-18 10:02:08 -04:00
|
|
|
type EC2BlockDeviceMappingsBuilder interface {
|
|
|
|
BuildEC2BlockDeviceMappings() []*ec2.BlockDeviceMapping
|
|
|
|
}
|
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
type StepRunSpotInstance struct {
|
2020-08-17 11:09:19 -04:00
|
|
|
PollingConfig *AWSPollingConfig
|
2017-10-04 05:29:38 -04:00
|
|
|
AssociatePublicIpAddress bool
|
2019-06-18 10:02:08 -04:00
|
|
|
LaunchMappings EC2BlockDeviceMappingsBuilder
|
2018-09-03 02:33:58 -04:00
|
|
|
BlockDurationMinutes int64
|
2017-10-04 05:29:38 -04:00
|
|
|
Debug bool
|
2018-08-29 05:23:59 -04:00
|
|
|
Comm *communicator.Config
|
2017-10-04 05:29:38 -04:00
|
|
|
EbsOptimized bool
|
|
|
|
ExpectedRootDevice string
|
2021-02-09 05:47:54 -05:00
|
|
|
HttpEndpoint string
|
|
|
|
HttpTokens string
|
|
|
|
HttpPutResponseHopLimit int64
|
2017-10-04 05:29:38 -04:00
|
|
|
InstanceInitiatedShutdownBehavior string
|
|
|
|
InstanceType string
|
2020-11-03 16:15:44 -05:00
|
|
|
Region string
|
2017-10-04 05:29:38 -04:00
|
|
|
SourceAMI string
|
|
|
|
SpotPrice string
|
2020-03-16 12:47:44 -04:00
|
|
|
SpotTags map[string]string
|
2019-05-22 13:16:42 -04:00
|
|
|
SpotInstanceTypes []string
|
2020-03-16 12:47:44 -04:00
|
|
|
Tags map[string]string
|
|
|
|
VolumeTags map[string]string
|
2017-10-04 05:29:38 -04:00
|
|
|
UserData string
|
|
|
|
UserDataFile string
|
|
|
|
Ctx interpolate.Context
|
2019-11-21 15:46:31 -05:00
|
|
|
NoEphemeral bool
|
2017-10-04 05:29:38 -04:00
|
|
|
|
2019-08-14 14:06:06 -04:00
|
|
|
instanceId string
|
2019-05-22 13:16:42 -04:00
|
|
|
}
|
2017-10-04 05:29:38 -04:00
|
|
|
|
2021-02-22 05:18:48 -05:00
|
|
|
// The EbsBlockDevice and LaunchTemplateEbsBlockDeviceRequest structs are
|
|
|
|
// nearly identical except for the struct's name and one extra field in
|
|
|
|
// EbsBlockDeviceResuest, which unfortunately means you can't just cast one
|
|
|
|
// into the other. THANKS AMAZON.
|
|
|
|
func castBlockDeviceToRequest(bd *ec2.EbsBlockDevice) *ec2.LaunchTemplateEbsBlockDeviceRequest {
|
|
|
|
out := &ec2.LaunchTemplateEbsBlockDeviceRequest{
|
|
|
|
DeleteOnTermination: bd.DeleteOnTermination,
|
|
|
|
Encrypted: bd.Encrypted,
|
|
|
|
Iops: bd.Iops,
|
|
|
|
KmsKeyId: bd.KmsKeyId,
|
|
|
|
SnapshotId: bd.SnapshotId,
|
|
|
|
Throughput: bd.Throughput,
|
|
|
|
VolumeSize: bd.VolumeSize,
|
|
|
|
VolumeType: bd.VolumeType,
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
func (s *StepRunSpotInstance) CreateTemplateData(userData *string, az string,
|
|
|
|
state multistep.StateBag, marketOptions *ec2.LaunchTemplateInstanceMarketOptionsRequest) *ec2.RequestLaunchTemplateData {
|
2019-11-21 15:46:31 -05:00
|
|
|
blockDeviceMappings := s.LaunchMappings.BuildEC2BlockDeviceMappings()
|
2019-05-22 13:16:42 -04:00
|
|
|
// Convert the BlockDeviceMapping into a
|
|
|
|
// LaunchTemplateBlockDeviceMappingRequest. These structs are identical,
|
|
|
|
// except for the EBS field -- on one, that field contains a
|
|
|
|
// LaunchTemplateEbsBlockDeviceRequest, and on the other, it contains an
|
2021-02-22 05:18:48 -05:00
|
|
|
// EbsBlockDevice.
|
2019-05-22 13:16:42 -04:00
|
|
|
var launchMappingRequests []*ec2.LaunchTemplateBlockDeviceMappingRequest
|
|
|
|
for _, mapping := range blockDeviceMappings {
|
|
|
|
launchRequest := &ec2.LaunchTemplateBlockDeviceMappingRequest{
|
|
|
|
DeviceName: mapping.DeviceName,
|
2021-02-22 05:18:48 -05:00
|
|
|
Ebs: castBlockDeviceToRequest(mapping.Ebs),
|
2019-05-22 13:16:42 -04:00
|
|
|
VirtualName: mapping.VirtualName,
|
|
|
|
}
|
|
|
|
launchMappingRequests = append(launchMappingRequests, launchRequest)
|
|
|
|
}
|
2020-03-24 11:44:20 -04:00
|
|
|
if s.NoEphemeral {
|
|
|
|
// This is only relevant for windows guests. Ephemeral drives by
|
|
|
|
// default are assigned to drive names xvdca-xvdcz.
|
|
|
|
// When vms are launched from the AWS console, they're automatically
|
|
|
|
// removed from the block devices if the user hasn't said to use them,
|
|
|
|
// but the SDK does not perform this cleanup. The following code just
|
|
|
|
// manually removes the ephemeral drives from the mapping so that they
|
|
|
|
// don't clutter up console views and cause confusion.
|
|
|
|
log.Printf("no_ephemeral was set, so creating drives xvdca-xvdcz as empty mappings")
|
|
|
|
DefaultEphemeralDeviceLetters := "abcdefghijklmnopqrstuvwxyz"
|
|
|
|
for _, letter := range DefaultEphemeralDeviceLetters {
|
|
|
|
launchRequest := &ec2.LaunchTemplateBlockDeviceMappingRequest{
|
|
|
|
DeviceName: aws.String("xvdc" + string(letter)),
|
|
|
|
NoDevice: aws.String(""),
|
|
|
|
}
|
|
|
|
launchMappingRequests = append(launchMappingRequests, launchRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-10-04 05:29:38 -04:00
|
|
|
|
2019-10-19 05:56:18 -04:00
|
|
|
iamInstanceProfile := aws.String(state.Get("iamInstanceProfile").(string))
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// Create a launch template.
|
|
|
|
templateData := ec2.RequestLaunchTemplateData{
|
|
|
|
BlockDeviceMappings: launchMappingRequests,
|
|
|
|
DisableApiTermination: aws.Bool(false),
|
|
|
|
EbsOptimized: &s.EbsOptimized,
|
2019-10-19 05:56:18 -04:00
|
|
|
IamInstanceProfile: &ec2.LaunchTemplateIamInstanceProfileSpecificationRequest{Name: iamInstanceProfile},
|
2019-05-22 13:16:42 -04:00
|
|
|
ImageId: &s.SourceAMI,
|
|
|
|
InstanceMarketOptions: marketOptions,
|
|
|
|
Placement: &ec2.LaunchTemplatePlacementRequest{
|
2018-08-14 06:04:13 -04:00
|
|
|
AvailabilityZone: &az,
|
2017-10-04 05:29:38 -04:00
|
|
|
},
|
2019-05-22 13:16:42 -04:00
|
|
|
UserData: userData,
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
// Create a network interface
|
|
|
|
securityGroupIds := aws.StringSlice(state.Get("securityGroupIds").([]string))
|
2018-08-14 06:04:13 -04:00
|
|
|
subnetId := state.Get("subnet_id").(string)
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
if subnetId != "" {
|
|
|
|
// Set up a full network interface
|
|
|
|
networkInterface := ec2.LaunchTemplateInstanceNetworkInterfaceSpecificationRequest{
|
|
|
|
Groups: securityGroupIds,
|
|
|
|
DeleteOnTermination: aws.Bool(true),
|
|
|
|
DeviceIndex: aws.Int64(0),
|
|
|
|
SubnetId: aws.String(subnetId),
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
if s.AssociatePublicIpAddress {
|
|
|
|
networkInterface.SetAssociatePublicIpAddress(s.AssociatePublicIpAddress)
|
|
|
|
}
|
|
|
|
templateData.SetNetworkInterfaces([]*ec2.LaunchTemplateInstanceNetworkInterfaceSpecificationRequest{&networkInterface})
|
2017-10-04 05:29:38 -04:00
|
|
|
} else {
|
2019-05-22 13:16:42 -04:00
|
|
|
templateData.SetSecurityGroupIds(securityGroupIds)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-09 05:47:54 -05:00
|
|
|
if s.HttpEndpoint == "enabled" {
|
|
|
|
templateData.MetadataOptions = &ec2.LaunchTemplateInstanceMetadataOptionsRequest{HttpEndpoint: &s.HttpEndpoint, HttpTokens: &s.HttpTokens, HttpPutResponseHopLimit: &s.HttpPutResponseHopLimit}
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// If instance type is not set, we'll just pick the lowest priced instance
|
|
|
|
// available.
|
|
|
|
if s.InstanceType != "" {
|
|
|
|
templateData.SetInstanceType(s.InstanceType)
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
|
|
|
|
2018-08-29 05:23:59 -04:00
|
|
|
if s.Comm.SSHKeyPairName != "" {
|
2019-05-22 13:16:42 -04:00
|
|
|
templateData.SetKeyName(s.Comm.SSHKeyPairName)
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
|
|
|
|
return &templateData
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRunSpotInstance) LoadUserData() (string, error) {
|
|
|
|
userData := s.UserData
|
|
|
|
if s.UserDataFile != "" {
|
|
|
|
contents, err := ioutil.ReadFile(s.UserDataFile)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Problem reading user data file: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userData = string(contents)
|
2018-09-12 13:21:39 -04:00
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
|
|
|
|
// Test if it is encoded already, and if not, encode it
|
|
|
|
if _, err := base64.StdEncoding.DecodeString(userData); err != nil {
|
|
|
|
log.Printf("[DEBUG] base64 encoding user data...")
|
|
|
|
userData = base64.StdEncoding.EncodeToString([]byte(userData))
|
2018-09-12 13:21:39 -04:00
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
return userData, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRunSpotInstance) Run(ctx context.Context, state multistep.StateBag) multistep.StepAction {
|
2020-11-01 09:43:49 -05:00
|
|
|
ec2conn := state.Get("ec2").(ec2iface.EC2API)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-05-22 13:16:42 -04:00
|
|
|
|
|
|
|
ui.Say("Launching a spot AWS instance...")
|
2017-10-04 05:29:38 -04:00
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// Get and validate the source AMI
|
|
|
|
image, ok := state.Get("source_image").(*ec2.Image)
|
|
|
|
if !ok {
|
|
|
|
state.Put("error", fmt.Errorf("source_image type assertion failed"))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
s.SourceAMI = *image.ImageId
|
|
|
|
|
|
|
|
if s.ExpectedRootDevice != "" && *image.RootDeviceType != s.ExpectedRootDevice {
|
|
|
|
state.Put("error", fmt.Errorf(
|
|
|
|
"The provided source AMI has an invalid root device type.\n"+
|
|
|
|
"Expected '%s', got '%s'.",
|
|
|
|
s.ExpectedRootDevice, *image.RootDeviceType))
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
azConfig := ""
|
|
|
|
if azRaw, ok := state.GetOk("availability_zone"); ok {
|
|
|
|
azConfig = azRaw.(string)
|
|
|
|
}
|
|
|
|
az := azConfig
|
|
|
|
|
|
|
|
var instanceId string
|
|
|
|
|
|
|
|
ui.Say("Interpolating tags for spot instance...")
|
|
|
|
// s.Tags will tag the eventually launched instance
|
|
|
|
// s.SpotTags apply to the spot request itself, and do not automatically
|
|
|
|
// get applied to the spot instance that is launched once the request is
|
|
|
|
// fulfilled
|
|
|
|
if _, exists := s.Tags["Name"]; !exists {
|
|
|
|
s.Tags["Name"] = "Packer Builder"
|
|
|
|
}
|
2017-10-04 05:29:38 -04:00
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// Convert tags from the tag map provided by the user into *ec2.Tag s
|
2020-11-03 16:15:44 -05:00
|
|
|
ec2Tags, err := TagMap(s.Tags).EC2Tags(s.Ctx, s.Region, state)
|
2017-10-04 05:29:38 -04:00
|
|
|
if err != nil {
|
2019-05-22 13:16:42 -04:00
|
|
|
err := fmt.Errorf("Error generating tags for source instance: %s", err)
|
2017-10-04 05:29:38 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
// This prints the tags to the ui; it doesn't actually add them to the
|
|
|
|
// instance yet
|
|
|
|
ec2Tags.Report(ui)
|
2017-10-04 05:29:38 -04:00
|
|
|
|
2021-01-12 04:46:41 -05:00
|
|
|
volumeTags, err := TagMap(s.VolumeTags).EC2Tags(s.Ctx, s.Region, state)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error generating volume tags: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
volumeTags.Report(ui)
|
|
|
|
|
2019-06-28 17:00:56 -04:00
|
|
|
spotOptions := ec2.LaunchTemplateSpotMarketOptionsRequest{}
|
|
|
|
// The default is to set the maximum price to the OnDemand price.
|
|
|
|
if s.SpotPrice != "auto" {
|
|
|
|
spotOptions.SetMaxPrice(s.SpotPrice)
|
2019-05-22 13:16:42 -04:00
|
|
|
}
|
|
|
|
if s.BlockDurationMinutes != 0 {
|
|
|
|
spotOptions.BlockDurationMinutes = &s.BlockDurationMinutes
|
|
|
|
}
|
|
|
|
marketOptions := &ec2.LaunchTemplateInstanceMarketOptionsRequest{
|
|
|
|
SpotOptions: &spotOptions,
|
|
|
|
}
|
|
|
|
marketOptions.SetMarketType(ec2.MarketTypeSpot)
|
|
|
|
|
2020-11-03 16:15:44 -05:00
|
|
|
spotTags, err := TagMap(s.SpotTags).EC2Tags(s.Ctx, s.Region, state)
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error generating tags for spot request: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// Create a launch template for the instance
|
|
|
|
ui.Message("Loading User Data File...")
|
2019-06-30 02:00:52 -04:00
|
|
|
|
|
|
|
// 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
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
userData, err := s.LoadUserData()
|
|
|
|
if err != nil {
|
|
|
|
state.Put("error", err)
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
ui.Message("Creating Spot Fleet launch template...")
|
|
|
|
templateData := s.CreateTemplateData(&userData, az, state, marketOptions)
|
|
|
|
launchTemplate := &ec2.CreateLaunchTemplateInput{
|
|
|
|
LaunchTemplateData: templateData,
|
2019-06-30 02:00:52 -04:00
|
|
|
LaunchTemplateName: aws.String(launchTemplateName),
|
2019-05-22 13:16:42 -04:00
|
|
|
VersionDescription: aws.String("template generated by packer for launching spot instances"),
|
2020-11-03 16:15:44 -05:00
|
|
|
}
|
|
|
|
if len(spotTags) > 0 {
|
|
|
|
launchTemplate.TagSpecifications = []*ec2.TagSpecification{
|
2020-11-01 09:43:49 -05:00
|
|
|
{
|
|
|
|
ResourceType: aws.String("launch-template"),
|
2020-11-03 16:15:44 -05:00
|
|
|
Tags: spotTags,
|
2020-11-01 09:43:49 -05:00
|
|
|
},
|
2020-11-03 16:15:44 -05:00
|
|
|
}
|
2021-01-12 04:46:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(ec2Tags) > 0 {
|
|
|
|
launchTemplate.LaunchTemplateData.TagSpecifications = append(
|
|
|
|
launchTemplate.LaunchTemplateData.TagSpecifications,
|
|
|
|
&ec2.LaunchTemplateTagSpecificationRequest{
|
2021-01-09 07:58:23 -05:00
|
|
|
ResourceType: aws.String("instance"),
|
2021-01-12 04:46:41 -05:00
|
|
|
Tags: ec2Tags,
|
2021-01-09 07:58:23 -05:00
|
|
|
},
|
2021-01-12 04:46:41 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(volumeTags) > 0 {
|
|
|
|
launchTemplate.LaunchTemplateData.TagSpecifications = append(
|
|
|
|
launchTemplate.LaunchTemplateData.TagSpecifications,
|
|
|
|
&ec2.LaunchTemplateTagSpecificationRequest{
|
2021-01-09 07:58:23 -05:00
|
|
|
ResourceType: aws.String("volume"),
|
2021-01-12 04:46:41 -05:00
|
|
|
Tags: volumeTags,
|
2021-01-09 07:58:23 -05:00
|
|
|
},
|
2021-01-12 04:46:41 -05:00
|
|
|
)
|
2019-05-22 13:16:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tell EC2 to create the template
|
2021-01-09 07:59:31 -05:00
|
|
|
createLaunchTemplateOutput, err := ec2conn.CreateLaunchTemplate(launchTemplate)
|
2017-10-04 05:29:38 -04:00
|
|
|
if err != nil {
|
2019-05-22 13:16:42 -04:00
|
|
|
err := fmt.Errorf("Error creating launch template for spot instance: %s", err)
|
2017-10-04 05:29:38 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
2021-01-09 07:59:31 -05:00
|
|
|
launchTemplateId := createLaunchTemplateOutput.LaunchTemplate.LaunchTemplateId
|
|
|
|
ui.Message(fmt.Sprintf("Created Spot Fleet launch template: %s", *launchTemplateId))
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// Add overrides for each user-provided instance type
|
|
|
|
var overrides []*ec2.FleetLaunchTemplateOverridesRequest
|
|
|
|
for _, instanceType := range s.SpotInstanceTypes {
|
|
|
|
override := ec2.FleetLaunchTemplateOverridesRequest{
|
|
|
|
InstanceType: aws.String(instanceType),
|
|
|
|
}
|
|
|
|
overrides = append(overrides, &override)
|
|
|
|
}
|
|
|
|
|
|
|
|
createFleetInput := &ec2.CreateFleetInput{
|
|
|
|
LaunchTemplateConfigs: []*ec2.FleetLaunchTemplateConfigRequest{
|
|
|
|
{
|
|
|
|
LaunchTemplateSpecification: &ec2.FleetLaunchTemplateSpecificationRequest{
|
2019-06-30 02:00:52 -04:00
|
|
|
LaunchTemplateName: aws.String(launchTemplateName),
|
2019-05-22 13:16:42 -04:00
|
|
|
Version: aws.String("1"),
|
|
|
|
},
|
|
|
|
Overrides: overrides,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ReplaceUnhealthyInstances: aws.Bool(false),
|
|
|
|
TargetCapacitySpecification: &ec2.TargetCapacitySpecificationRequest{
|
|
|
|
TotalTargetCapacity: aws.Int64(1),
|
|
|
|
DefaultTargetCapacityType: aws.String("spot"),
|
|
|
|
},
|
|
|
|
Type: aws.String("instant"),
|
|
|
|
}
|
|
|
|
|
retry spot instance creation when an "Invalid IAM Instance Profile name" error pops up (#9810)
PutRolePolicy & AddRoleToInstanceProfile are eventually consistent but it is not possible to wait for them to be done here: https://github.com/hashicorp/packer/blob/0785c2f6fca9c22bf25528e0176042799dd79df9/builder/amazon/common/step_iam_instance_profile.go#L117-L134 which was causing the `CreateFleet` to fail (100% for me). So for now we retry a bit later. Waiting 5 seconds after the previously linked code also fixed this.
Test file:
```json
{
"builders": [
{
"type": "amazon-ebs",
"region": "eu-west-1",
"ami_name": "ubuntu-16.04 test {{timestamp}}",
"ami_description": "Ubuntu 16.04 LTS - expand root partition",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": [
"099720109477"
],
"most_recent": true
},
"spot_price": "0.03",
"spot_instance_types": [
"t2.small"
],
"encrypt_boot": true,
"ssh_username": "ubuntu",
"ssh_interface": "session_manager",
"temporary_iam_instance_profile_policy_document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"*"
],
"Resource": "*"
}
]
},
"communicator": "ssh"
}
]}
```
2020-08-25 04:10:32 -04:00
|
|
|
var createOutput *ec2.CreateFleetOutput
|
|
|
|
err = retry.Config{
|
|
|
|
Tries: 11,
|
|
|
|
ShouldRetry: func(err error) bool {
|
|
|
|
if strings.Contains(err.Error(), "Invalid IAM Instance Profile name") {
|
|
|
|
// eventual consistency of the profile. PutRolePolicy &
|
|
|
|
// AddRoleToInstanceProfile are eventually consistent and once
|
|
|
|
// we can wait on those operations, this can be removed.
|
|
|
|
return true
|
|
|
|
}
|
2020-09-18 11:05:36 -04:00
|
|
|
return false
|
retry spot instance creation when an "Invalid IAM Instance Profile name" error pops up (#9810)
PutRolePolicy & AddRoleToInstanceProfile are eventually consistent but it is not possible to wait for them to be done here: https://github.com/hashicorp/packer/blob/0785c2f6fca9c22bf25528e0176042799dd79df9/builder/amazon/common/step_iam_instance_profile.go#L117-L134 which was causing the `CreateFleet` to fail (100% for me). So for now we retry a bit later. Waiting 5 seconds after the previously linked code also fixed this.
Test file:
```json
{
"builders": [
{
"type": "amazon-ebs",
"region": "eu-west-1",
"ami_name": "ubuntu-16.04 test {{timestamp}}",
"ami_description": "Ubuntu 16.04 LTS - expand root partition",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": [
"099720109477"
],
"most_recent": true
},
"spot_price": "0.03",
"spot_instance_types": [
"t2.small"
],
"encrypt_boot": true,
"ssh_username": "ubuntu",
"ssh_interface": "session_manager",
"temporary_iam_instance_profile_policy_document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"*"
],
"Resource": "*"
}
]
},
"communicator": "ssh"
}
]}
```
2020-08-25 04:10:32 -04:00
|
|
|
},
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 500 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
createOutput, err = ec2conn.CreateFleet(createFleetInput)
|
|
|
|
if err == nil && createOutput.Errors != nil {
|
|
|
|
err = fmt.Errorf("errors: %v", createOutput.Errors)
|
|
|
|
}
|
2020-09-18 11:05:36 -04:00
|
|
|
// We can end up with errors because one of the allowed availability
|
|
|
|
// zones doesn't have one of the allowed instance types; as long as
|
|
|
|
// an instance is launched, these errors aren't important.
|
|
|
|
if len(createOutput.Instances) > 0 {
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("create request failed for some instances %v", err.Error())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
retry spot instance creation when an "Invalid IAM Instance Profile name" error pops up (#9810)
PutRolePolicy & AddRoleToInstanceProfile are eventually consistent but it is not possible to wait for them to be done here: https://github.com/hashicorp/packer/blob/0785c2f6fca9c22bf25528e0176042799dd79df9/builder/amazon/common/step_iam_instance_profile.go#L117-L134 which was causing the `CreateFleet` to fail (100% for me). So for now we retry a bit later. Waiting 5 seconds after the previously linked code also fixed this.
Test file:
```json
{
"builders": [
{
"type": "amazon-ebs",
"region": "eu-west-1",
"ami_name": "ubuntu-16.04 test {{timestamp}}",
"ami_description": "Ubuntu 16.04 LTS - expand root partition",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": [
"099720109477"
],
"most_recent": true
},
"spot_price": "0.03",
"spot_instance_types": [
"t2.small"
],
"encrypt_boot": true,
"ssh_username": "ubuntu",
"ssh_interface": "session_manager",
"temporary_iam_instance_profile_policy_document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"*"
],
"Resource": "*"
}
]
},
"communicator": "ssh"
}
]}
```
2020-08-25 04:10:32 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("create request failed %v", err)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
if err != nil {
|
2019-08-19 16:44:39 -04:00
|
|
|
if createOutput.FleetId != nil {
|
|
|
|
err = fmt.Errorf("Error waiting for fleet request (%s): %s", *createOutput.FleetId, err)
|
|
|
|
}
|
2019-10-02 17:06:30 -04:00
|
|
|
if len(createOutput.Errors) > 0 {
|
|
|
|
errString := fmt.Sprintf("Error waiting for fleet request (%s) to become ready:", *createOutput.FleetId)
|
|
|
|
for _, outErr := range createOutput.Errors {
|
2020-03-04 15:31:30 -05:00
|
|
|
errString = errString + aws.StringValue(outErr.ErrorMessage)
|
2019-10-02 17:06:30 -04:00
|
|
|
}
|
|
|
|
err = fmt.Errorf(errString)
|
2019-08-14 14:06:06 -04:00
|
|
|
}
|
retry spot instance creation when an "Invalid IAM Instance Profile name" error pops up (#9810)
PutRolePolicy & AddRoleToInstanceProfile are eventually consistent but it is not possible to wait for them to be done here: https://github.com/hashicorp/packer/blob/0785c2f6fca9c22bf25528e0176042799dd79df9/builder/amazon/common/step_iam_instance_profile.go#L117-L134 which was causing the `CreateFleet` to fail (100% for me). So for now we retry a bit later. Waiting 5 seconds after the previously linked code also fixed this.
Test file:
```json
{
"builders": [
{
"type": "amazon-ebs",
"region": "eu-west-1",
"ami_name": "ubuntu-16.04 test {{timestamp}}",
"ami_description": "Ubuntu 16.04 LTS - expand root partition",
"source_ami_filter": {
"filters": {
"virtualization-type": "hvm",
"name": "ubuntu/images/*/ubuntu-xenial-16.04-amd64-server-*",
"root-device-type": "ebs"
},
"owners": [
"099720109477"
],
"most_recent": true
},
"spot_price": "0.03",
"spot_instance_types": [
"t2.small"
],
"encrypt_boot": true,
"ssh_username": "ubuntu",
"ssh_interface": "session_manager",
"temporary_iam_instance_profile_policy_document": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"*"
],
"Resource": "*"
}
]
},
"communicator": "ssh"
}
]}
```
2020-08-25 04:10:32 -04:00
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
instanceId = *createOutput.Instances[0].InstanceIds[0]
|
2019-09-26 18:47:01 -04:00
|
|
|
// Set the instance ID so that the cleanup works properly
|
|
|
|
s.instanceId = instanceId
|
|
|
|
|
|
|
|
ui.Message(fmt.Sprintf("Instance ID: %s", instanceId))
|
|
|
|
|
|
|
|
// Get information about the created instance
|
|
|
|
var describeOutput *ec2.DescribeInstancesOutput
|
|
|
|
err = retry.Config{
|
2019-09-27 07:04:52 -04:00
|
|
|
Tries: 11,
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
2019-09-26 18:47:01 -04:00
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
|
|
|
describeOutput, err = ec2conn.DescribeInstances(&ec2.DescribeInstancesInput{
|
|
|
|
InstanceIds: []*string{aws.String(instanceId)},
|
|
|
|
})
|
2020-07-08 14:55:56 -04:00
|
|
|
if len(describeOutput.Reservations) > 0 && len(describeOutput.Reservations[0].Instances) > 0 {
|
|
|
|
if len(s.LaunchMappings.BuildEC2BlockDeviceMappings()) > 0 && len(describeOutput.Reservations[0].Instances[0].BlockDeviceMappings) == 0 {
|
|
|
|
return fmt.Errorf("Instance has no block devices")
|
|
|
|
}
|
2020-06-25 04:48:24 -04:00
|
|
|
}
|
2019-09-26 18:47:01 -04:00
|
|
|
return err
|
|
|
|
})
|
|
|
|
if err != nil || len(describeOutput.Reservations) == 0 || len(describeOutput.Reservations[0].Instances) == 0 {
|
|
|
|
err := fmt.Errorf("Error finding source instance.")
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := describeOutput.Reservations[0].Instances[0]
|
|
|
|
|
2019-05-22 13:16:42 -04:00
|
|
|
// Tag the spot instance request (not the eventual spot instance)
|
2020-03-16 12:47:44 -04:00
|
|
|
if len(spotTags) > 0 && len(s.SpotTags) > 0 {
|
2019-08-15 19:06:16 -04:00
|
|
|
spotTags.Report(ui)
|
|
|
|
// Use the instance ID to find out the SIR, so that we can tag the spot
|
|
|
|
// request associated with this instance.
|
|
|
|
sir := describeOutput.Reservations[0].Instances[0].SpotInstanceRequestId
|
|
|
|
|
|
|
|
// Apply tags to the spot request.
|
2019-04-08 11:57:27 -04:00
|
|
|
err = retry.Config{
|
|
|
|
Tries: 11,
|
|
|
|
ShouldRetry: func(error) bool { return false },
|
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
2017-10-12 17:33:01 -04:00
|
|
|
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Tags: spotTags,
|
2019-08-15 19:06:16 -04:00
|
|
|
Resources: []*string{sir},
|
2017-10-12 17:33:01 -04:00
|
|
|
})
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2017-10-12 17:33:01 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging spot request: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-04 05:29:38 -04:00
|
|
|
// Retry creating tags for about 2.5 minutes
|
2020-12-15 12:12:32 -05:00
|
|
|
err = retry.Config{Tries: 11, ShouldRetry: func(err error) bool {
|
2020-10-29 06:57:14 -04:00
|
|
|
if awserrors.Matches(err, "InvalidInstanceID.NotFound", "") {
|
2019-11-12 15:27:47 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
},
|
2019-04-08 11:57:27 -04:00
|
|
|
RetryDelay: (&retry.Backoff{InitialBackoff: 200 * time.Millisecond, MaxBackoff: 30 * time.Second, Multiplier: 2}).Linear,
|
|
|
|
}.Run(ctx, func(ctx context.Context) error {
|
2017-10-04 05:29:38 -04:00
|
|
|
_, err := ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Tags: ec2Tags,
|
|
|
|
Resources: []*string{instance.InstanceId},
|
|
|
|
})
|
2019-04-08 11:57:27 -04:00
|
|
|
return err
|
2017-10-04 05:29:38 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source instance: %s", err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeIds := make([]*string, 0)
|
|
|
|
for _, v := range instance.BlockDeviceMappings {
|
|
|
|
if ebs := v.Ebs; ebs != nil {
|
|
|
|
volumeIds = append(volumeIds, ebs.VolumeId)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 12:47:44 -04:00
|
|
|
if len(volumeIds) > 0 && len(s.VolumeTags) > 0 {
|
2017-10-04 05:29:38 -04:00
|
|
|
ui.Say("Adding tags to source EBS Volumes")
|
|
|
|
_, err = ec2conn.CreateTags(&ec2.CreateTagsInput{
|
|
|
|
Resources: volumeIds,
|
2018-02-02 23:16:23 -05:00
|
|
|
Tags: volumeTags,
|
2017-10-04 05:29:38 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
err := fmt.Errorf("Error tagging source EBS Volumes on %s: %s", *instance.InstanceId, err)
|
|
|
|
state.Put("error", err)
|
|
|
|
ui.Error(err.Error())
|
|
|
|
return multistep.ActionHalt
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.Debug {
|
|
|
|
if instance.PublicDnsName != nil && *instance.PublicDnsName != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public DNS: %s", *instance.PublicDnsName))
|
|
|
|
}
|
|
|
|
|
|
|
|
if instance.PublicIpAddress != nil && *instance.PublicIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Public IP: %s", *instance.PublicIpAddress))
|
|
|
|
}
|
|
|
|
|
|
|
|
if instance.PrivateIpAddress != nil && *instance.PrivateIpAddress != "" {
|
|
|
|
ui.Message(fmt.Sprintf("Private IP: %s", *instance.PrivateIpAddress))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.Put("instance", instance)
|
2019-12-13 14:57:01 -05:00
|
|
|
// instance_id is the generic term used so that users can have access to the
|
|
|
|
// instance id inside of the provisioners, used in step_provision.
|
|
|
|
state.Put("instance_id", instance.InstanceId)
|
2017-10-04 05:29:38 -04:00
|
|
|
|
|
|
|
return multistep.ActionContinue
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StepRunSpotInstance) Cleanup(state multistep.StateBag) {
|
|
|
|
ec2conn := state.Get("ec2").(*ec2.EC2)
|
2020-11-19 14:54:31 -05:00
|
|
|
ui := state.Get("ui").(packersdk.Ui)
|
2019-06-30 02:00:52 -04:00
|
|
|
launchTemplateName := state.Get("launchTemplateName").(string)
|
2017-10-04 05:29:38 -04:00
|
|
|
|
|
|
|
// Terminate the source instance if it exists
|
|
|
|
if s.instanceId != "" {
|
|
|
|
ui.Say("Terminating the source AWS instance...")
|
|
|
|
if _, err := ec2conn.TerminateInstances(&ec2.TerminateInstancesInput{InstanceIds: []*string{&s.instanceId}}); err != nil {
|
|
|
|
ui.Error(fmt.Sprintf("Error terminating instance, may still be around: %s", err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-17 11:09:19 -04:00
|
|
|
if err := s.PollingConfig.WaitUntilInstanceTerminated(aws.BackgroundContext(), ec2conn, s.instanceId); err != nil {
|
2017-10-04 05:29:38 -04:00
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
|
|
|
}
|
2019-05-22 13:16:42 -04:00
|
|
|
|
|
|
|
// Delete the launch template used to create the spot fleet
|
|
|
|
deleteInput := &ec2.DeleteLaunchTemplateInput{
|
2019-06-30 02:00:52 -04:00
|
|
|
LaunchTemplateName: aws.String(launchTemplateName),
|
2019-05-22 13:16:42 -04:00
|
|
|
}
|
|
|
|
if _, err := ec2conn.DeleteLaunchTemplate(deleteInput); err != nil {
|
|
|
|
ui.Error(err.Error())
|
|
|
|
}
|
2017-10-04 05:29:38 -04:00
|
|
|
}
|