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