Target Display Mode can be used.
iMac 27" 2009 and 2010 can be connected to eGPU via DisplayPort to Mini DisplayPort adapter.
iMac 2011- Mid 2014 can be connected to the MacBook and using an iMac in this mode will not allow for accelerated graphics options. There are exceptions when using certain applications. More details »
iMac (Retina 5K, 27-inch, Late 2014) and later iMac models can't be used as Target Display Mode displays.