As a race to the top of the artificial intelligence (AI) market heats up, multiple companies have been pitted against each other. Cloud companies have become particularly competitive as they increasingly use AI services to attract new clientele. 

Amazon (AMZN -1.14%) and Microsoft (MSFT -1.84%) hold the two largest market shares in cloud computing with their respective services: Amazon Web Services (AWS) with a 32% share and Azure with 23%. As a result, one of these companies looks poised to become a dominant figure in both the cloud and AI markets, making their stocks compelling investments.

While both companies' stocks would likely prove assets to any portfolio, it's best to understand which has the potential to grant more consistent gains over the long term. So let's examine whether Amazon or Microsoft is the better buy.

Expect Amazon's cloud dominance to continue

Amazon's biggest asset is its lead in the cloud market with AWS. The platform has massively boosted the company's earnings and diversified its business. 

AWS proved particularly valuable during economic headwinds last year, which triggered reductions in Amazon's e-commerce business. Despite that downturn, AWS kept the company profitable, earning 100% of Amazon's $12 billion in operating income in fiscal 2022. AWS' wide adoption by businesses gives the company a leg up when touting its AI services.

However, high interest rates and increased competition have caused slowing growth for AWS. The cloud platform's year-over-year revenue growth hit 40% in the fourth quarter of 2021, with that figure down to 20% as of Q4 2022. 

The decline is concerning, as Amazon's e-commerce business is still working on a recovery. The company's latest quarter suggests it's headed back to profitability, but cloud earnings are still essential to Amazon's success.

Despite recent challenges, Amazon continues to have a solid long-term outlook. Inflation has eased for nine consecutive months, and the company has much to gain from the improvement. Consumers and businesses will likely have more cash to spend, bolstering Amazon's e-commerce and cloud businesses.

Amazon's potential is backed by Wall Street, with 46 out of 50 analysts currently holding a strong buy/buy rating for the company. Meanwhile, its 12-month price target of $138 projects stock growth of 13%. Amazon is an excellent long-term buy, with its dominance in cloud services and e-commerce likely to offer patient investors consistent stock growth.

Microsoft's AI prospects help set it apart from the pack

Microsoft may be behind Amazon in the cloud market, but it's making big moves to change that with the help of AI. The company has become one of the biggest names in AI after investing $1 billion in ChatGPT developer OpenAI in 2019. The success of the start-up's advanced chatbot prompted Microsoft to invest a further $10 billion in OpenAI this year, enabling the Windows company to introduce AI upgrades across many of its services.

For instance, Microsoft's Office programs Excel and Word now have AI capabilities, ChatGPT has become available on the company's cloud service Azure, and the search engine Bing also now offers AI features. In addition to its priority of expanding its AI software offerings, Microsoft is ensuring it has the hardware necessary to run such workloads by bolstering Advanced Micro Devices' AI chip expansion with financial and engineering resources.

Like AWS, Microsoft's Azure also experienced slowing growth amid macroeconomic hurdles. Azure's year-over-year growth has fallen from 50% to 35% in the same time frame as AWS' slowdown. However, Azure's decreased growth is far less severe than AWS', suggesting the Microsoft cloud service could have an easier recovery path.

The table below illustrates that Microsoft's stock is currently the better value.

AMZN PE Ratio (Forward) Chart

Data by YCharts

Amazon's forward P/E is more than double Microsoft's, making its stock far more expensive. 

Microsoft's AI development priority and better-performing cloud platform give it a stronger outlook than Amazon. Alongside a significantly lower P/E, Microsoft's shares are a better buy and a bargain in comparison.