github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/amazon/instance/step_register_ami.go (about) 1 package instance 2 3 import ( 4 "fmt" 5 6 "github.com/aws/aws-sdk-go/aws" 7 "github.com/aws/aws-sdk-go/service/ec2" 8 awscommon "github.com/hashicorp/packer/builder/amazon/common" 9 "github.com/hashicorp/packer/packer" 10 "github.com/mitchellh/multistep" 11 ) 12 13 type StepRegisterAMI struct { 14 EnableAMIENASupport bool 15 EnableAMISriovNetSupport bool 16 } 17 18 func (s *StepRegisterAMI) Run(state multistep.StateBag) multistep.StepAction { 19 config := state.Get("config").(*Config) 20 ec2conn := state.Get("ec2").(*ec2.EC2) 21 manifestPath := state.Get("remote_manifest_path").(string) 22 ui := state.Get("ui").(packer.Ui) 23 24 ui.Say("Registering the AMI...") 25 registerOpts := &ec2.RegisterImageInput{ 26 ImageLocation: &manifestPath, 27 Name: aws.String(config.AMIName), 28 BlockDeviceMappings: config.BlockDevices.BuildAMIDevices(), 29 } 30 31 if config.AMIVirtType != "" { 32 registerOpts.VirtualizationType = aws.String(config.AMIVirtType) 33 } 34 35 if s.EnableAMISriovNetSupport { 36 // Set SriovNetSupport to "simple". See http://goo.gl/icuXh5 37 // As of February 2017, this applies to C3, C4, D2, I2, R3, and M4 (excluding m4.16xlarge) 38 registerOpts.SriovNetSupport = aws.String("simple") 39 } 40 if s.EnableAMIENASupport { 41 // Set EnaSupport to true 42 // As of February 2017, this applies to C5, I3, P2, R4, X1, and m4.16xlarge 43 registerOpts.EnaSupport = aws.Bool(true) 44 } 45 46 registerResp, err := ec2conn.RegisterImage(registerOpts) 47 if err != nil { 48 state.Put("error", fmt.Errorf("Error registering AMI: %s", err)) 49 ui.Error(state.Get("error").(error).Error()) 50 return multistep.ActionHalt 51 } 52 53 // Set the AMI ID in the state 54 ui.Say(fmt.Sprintf("AMI: %s", *registerResp.ImageId)) 55 amis := make(map[string]string) 56 amis[*ec2conn.Config.Region] = *registerResp.ImageId 57 state.Put("amis", amis) 58 59 // Wait for the image to become ready 60 stateChange := awscommon.StateChangeConf{ 61 Pending: []string{"pending"}, 62 Target: "available", 63 Refresh: awscommon.AMIStateRefreshFunc(ec2conn, *registerResp.ImageId), 64 StepState: state, 65 } 66 67 ui.Say("Waiting for AMI to become ready...") 68 if _, err := awscommon.WaitForState(&stateChange); err != nil { 69 err := fmt.Errorf("Error waiting for AMI: %s", err) 70 state.Put("error", err) 71 ui.Error(err.Error()) 72 return multistep.ActionHalt 73 } 74 75 state.Put("snapshots", map[string][]string{}) 76 77 return multistep.ActionContinue 78 } 79 80 func (s *StepRegisterAMI) Cleanup(multistep.StateBag) {}