in pkg/aws/ec2/api/helper.go [319:360]
func (h *ec2APIHelper) CreateAndAttachNetworkInterface(instanceId *string, subnetId *string, securityGroups []string,
tags []ec2types.Tag, deviceIndex *int32, description *string, interfaceType *string, ipResourceCount *config.IPResourceCount,
) (*ec2types.NetworkInterface, error) {
nwInterface, err := h.CreateNetworkInterface(description, subnetId, securityGroups, tags, ipResourceCount, interfaceType)
if err != nil {
return nil, fmt.Errorf("creating network interface, %w", err)
}
var attachmentId *string
attachmentId, err = h.AttachNetworkInterfaceToInstance(instanceId, nwInterface.NetworkInterfaceId, deviceIndex)
if err != nil {
errDelete := h.DeleteNetworkInterface(nwInterface.NetworkInterfaceId)
if errDelete != nil {
return nwInterface, fmt.Errorf("failed to attach the network interface %v: failed to delete the nw interfac %v",
err, errDelete)
}
return nil, fmt.Errorf("attaching network interface, %w", err)
}
err = h.SetDeleteOnTermination(attachmentId, nwInterface.NetworkInterfaceId)
if err != nil {
errDelete := h.DetachAndDeleteNetworkInterface(attachmentId, nwInterface.NetworkInterfaceId)
if errDelete != nil {
return nwInterface, fmt.Errorf("failed to set deletion on termination: %v: failed to delete nw interface: %v",
err, errDelete)
}
return nil, fmt.Errorf("enabling delete on termination, %w", err)
}
err = h.WaitForNetworkInterfaceStatusChange(nwInterface.NetworkInterfaceId, string(ec2types.AttachmentStatusAttached))
if err != nil {
errDelete := h.DetachAndDeleteNetworkInterface(attachmentId, nwInterface.NetworkInterfaceId)
if errDelete != nil {
return nwInterface, fmt.Errorf("failed to verify status attached: %v: failed to delete nw interface: %v",
err, errDelete)
}
return nil, fmt.Errorf("waiting for network attachement, %w", err)
}
return nwInterface, nil
}