(Bloomberg) -- Amazon.com Inc. reminded investors that luring shoppers away from stores and dominating the cloud-computing industry isn’t cheap.

The company on Thursday forecast a potential quarterly loss for the first time in two years. Amazon said it’s boosting spending on new warehouses to meet growing e-commerce demand, data centers for its Amazon Web Services division, video programming to keep customers engaged and gadgets like the Echo line of voice-activated speakers to stay on the cutting edge of the emerging smart-home market.

The outlook underscored the high cost of Amazon’s business model, which consistently delivers big sales gains and plows a chunk of that money back into the company by hiring workers, expanding its footprint and launching new products.

Chief Financial Officer Brian Olsavsky said Amazon’s expenses would climb in the second half of the year as it hires workers and works out the kinks in new warehouses to prepare for the peak shopping season. That’s why Amazon projected operating income in the current period ranging from a loss of $400 million to a gain of $300 million. The last projected quarterly loss came in July 2015 for similar reasons.

The third quarter “is generally a high investment period for the holiday,” he said.

Analysts estimated operating profit of $863.5 million, and shares fell as much as 4.3 percent to $1,001.80 in extended trading Thursday after the forecast and earnings were reported. Friday morning the stock was down about 3 percent in early trading. The shares have gained 39 percent this year, and Chief Executive Officer Jeff Bezos briefly surpassed Microsoft Corp. co-founder Bill Gates earlier Thursday to be the world’s wealthiest person, before falling back to the No. 2 spot as Amazon’s shares declined.

“Spending is always a concern with Amazon, but investors eventually give Amazon a pass because Amazon invests in growth opportunities,” said Victor Anthony, an analyst at Aegis Capital Corp.

Amazon didn’t wait until the second half to pick up the pace of spending. Second-quarter expenses increased 28 percent to $37.3 billion, the Seattle-based company said in a statement.

Investors have put increasing faith in Bezos to keep Amazon growing by entering new categories such as groceries and appliances and expanding abroad. The company is moving into India and Australia, speeding up delivery times to as little as an hour on select products, adding new skills and devices for its voice-activated Alexa platform and producing more original movies and shows. Amazon hired more than 30,000 new employees “in the last few months,” Bezos said in the statement.

Second-quarter sales gained 25 percent to $38 billion, topping analysts’ projections of $37.2 billion, according to data compiled by Bloomberg. Net income declined to $197 million, or 40 cents a share, from $857 million, or $1.78 a share, a year earlier. Analysts estimated profit of $1.42 a share, according to data compiled by Bloomberg.

Net sales in the third quarter will be $39.25 billion to $41.75 billion -- growth of 20 percent to 28 percent from a year earlier. Analysts estimated $40 billion.

The forecast excludes Amazon’s proposed $13.7 billion deal for Whole Foods Market Inc., where the company is betting it can replicate its online selling success. With 460 stores across the U.S., the acquisition is a major push into the $800 billion grocery category dominated by Wal-Mart Stores Inc., which has more than 26 percent of the market. The upscale grocery-store chain on Wednesday reported improving results in the second quarter, with sales declining less than analysts expected.

Amazon dominates e-commerce in the U.S. with its $99-a-year Amazon Prime subscription, which includes delivery discounts, music and video streaming and is intended to keep shoppers engaged with the website. The company had 85 million Prime subscribers in the U.S. as of June 30, an increase of 35 percent from a year earlier, according to Consumer Intelligence Research Partners. Amazon’s subscription services revenue, which is mostly from Prime memberships, increased 51 percent to $2.17 billion in the second quarter -- faster than 49 percent in the previous quarter. The company doesn’t disclose the number of Prime subscribers.

Revenue from Amazon Web Services, its profitable cloud-computing division, increased 42 percent to $4.1 billion. The unit’s sales increased 43 percent in the previous quarter.

Sales of warehousing, packaging and other logistics services Amazon provides for e-commerce merchants increased 38 percent to $7 billion. That’s faster than 34 percent in the previous quarter.