The contract has an outsize importance because it is central to the Pentagon’s efforts to modernize its technology. Much of the military operates on 1980s and 1990s computer systems, and the Defense Department has spent billions of dollars trying to make them talk to one another.

The decision was a surprise because Amazon had been considered the front-runner, in part because it had built cloud services for the Central Intelligence Agency. But that was before Mr. Trump became publicly hostile to Mr. Bezos, who also owns The Washington Post. The president often refers to the newspaper as the “Amazon Washington Post” and has accused it of spreading “fake news.”

In public, Mr. Trump said there were other “great companies” that should have a chance at the contract. But a speechwriter for former Defense Secretary Jim Mattis says in a book scheduled for publication next week that Mr. Trump had wanted to foil Amazon and give the contract to another company.