Commercial services for provisioning software components and virtual infrastructure in the cloud are emerging. For customers, this creates a multitude of possibilities for outsourcing part of the IT-stack to third parties in order to run their applications. These possibilities are associated with different running costs, so cloud customers have to determine the optimal solution. In this paper, we present and experimentally evaluate an algorithm that solves the corresponding optimization problem. We assume that applications are described as templates, fixing the deployment structure and constraining the properties of the used soft- and hardware components. Different parts of the application may be outsourced to different providers and several levels of outsourcing can be considered. However, dependencies between different parts of the application have to be respected. Our algorithm decomposes the application graph in a first step in order to discover all suitable cloud provisioning services from a registry. It determines the optimal solution by representing the problem as constraint optimization problem that can be solved by an existing solver implementation.