Ever noticed how Amazon consistently breaks even every quarter? Sure there's like a hundred million loss, or sometimes profit in other quarters, but that's nothing when quarterly revenue is $20 billion. The company knows how much money is coming in, and they're using all of their profit to invest in their infrastructure, and grow out their businesses. They could decide at any moment to stop doing this, and the company would become hugely profitable overnight.
But their revenue last quarter is about 25% higher than it was this time last year, and it has consistently been seeing this kind of growth for years. The right thing for Amazon to do, from a shareholder's perspective, is to keep investing and ride out this wave of growth for as long as it lasts. To do otherwise would be to give up their long-term position just to maximize their short-term quarterly profits.
So to answer your question, "long term" happens when sales growth disappears, and the investments that Amazon makes into its infrastructure no longer provide any returns. With the sales growth that Amazon is seeing right now, this is clearly not the right time to stop building out the company.