Modern banking systems are highly interconnected. Despite their various benefits, the linkages that exist between banks carry the risk of contagion. In this paper we investigate how banks decide on direct balance sheet linkages and the implications for contagion risk. In particular, we model a network formation process in the banking system. Banks form links order to reduce the risk of contagion. The network is formed endogenously and serves as an insurance mechanism. We show that banks manage to form networks that are resilient to contagion. Thus, in an equilibrium network, the probability of contagion is virtually 0.