SummaryNetwork function virtualization (NFV) is a name of technology for replacing hardware‐based network functions with software programs. Virtual network function (VNF) is a software program that replaces the hardware‐based network functionality. The replacement of the hardware‐based network functions (middleboxes) with software programs promises the on‐demand provisioning of network functions and reduces capital and operational expenses of the network. Due to this replaced network can adapt to the different network functions. Network service providers deploy various network services with different objectives, such as reducing the network's active servers and traffic latency or network operational expenses. In this article, a VNF placement problem is studied to optimize the total operating costs of the networks. To solve the VNF placement problem, we proposed an integer linear program (ILP) model, which has been implemented using CPLEX. Although an ILP‐based approach gives an optimal solution, it takes a long execution time to find the solution. Due to the long execution time, the ILP‐based approach is not suitable for the real‐time VNF placement problem. To address this challenge, we proposed a heuristic based on dynamic programming that performs better than the existing approaches. The simulation results of the proposed solution using real‐world topologies show that the heuristic approach finds a feasible solution that is only 1 to 1.34 times far from the optimal one. Moreover, experimental results show that the proposed heuristic is 15 to 423 times faster than the ILP.