Renewable energy-based microgrids play a critical role in future smart grids. Due to the uncertainties of renewable generations, the microgrids face potential risk of load shedding during operation. To address this problem, we propose a contract-based approach to enhance the resilience of microgrids. Specifically, in the framework, the microgrids who may not be self-efficient to meet their local demands can purchase the needed power from their connected microgrids by signing a contract that specifies the power price in advance. We leverage a principal-agent model to capture the energy trading relationships between the microgrids through a resilience as a service (RaaS) paradigm. By focusing on the incentive compatible and individual rational constraints of the service requester, the service provider designs the optimal contracts for the transactive resilience that yields the largest payoff despite the incomplete information. We characterize analytical solutions of the optimal contracts for several scenarios where the service requester has various options on its hidden actions. Numerical simulations are used to illustrate and corroborate the obtained results.