Amazon did not consistently lose money. They were basically breaking even since the early 2000s and they proved they could turn on the profit faucet anytime they want once AWS came around. Although, they still heavily invest in infrastructure development, hence the continued low profit margins.
There are some technical for-profit companies that effectively drive profit to zero and the shareholders don't care, because that was the purpose.