Battery as an energy storage system is a key element in Microgrids (MGs). Therefore, determining the optimal size of battery energy storage systems (BESS) can reduce the operational costs of MGs. The goal of this paper is to calculate the optimal size of a BESS in an off-grid MG while minimizing the total cost using convex optimization methods. This paper proposes a new two-step cost-based method to determine the optimal size of a BESS in an MG. In the first step, in order to optimize the operation of the MG, a unit commitment (UC) problem is solved. In the next step, the optimal size of the BESS is determined using convex optimization considering several physical and operational constraints. The resulting problem is a non-convex non-linear problem. However, the problem can be transformed into a convex problem using convex relaxation methods. In the next step, the convexity and feasibility of the optimization problem is proved. The CVX toolbox package integrated with MATLAB is used to solve the problem. To assess the effects of BESS on operational and total costs of the MG, four scenarios are studied. Additionally, the results have been compared with PSO and GA algorithms and effectiveness of the proposed method has been shown.