Washington (CNN) It was about 7:30 Thursday morning when Defense Secretary Jim Mattis decided he needed to see his Commander in Chief.

He was "livid" at the notion that the US was betraying an ally, two defense officials said. A military man of 40 years, Mattis felt President Donald Trump's stunning decision the previous day to pull US troops out of Syria undercut the cardinal rule of never leaving a friend on the battlefield.

The Turkish Defense Minister was already threatening to level a brutal assault on US-backed Kurdish allies and put them "in ditches" once the US pulled out. The remark set Mattis off. He had spent months trying to convince Trump that withdrawal was a bad idea and was now seeing early signs of what it could mean to US allies in the region.

A little past 3pm, Mattis was in the Oval Office, trying one last time to get Trump to change his mind about Syria. The meeting didn't last very long. The President refused to budge, and so Mattis made the only remaining choice he felt he had left— he pulled out a two-page letter he'd brought with him and resigned on the spot. Though he wouldn't be leaving immediately— Mattis will stay on until February— the decision was final.

By 3:27pm, the President was walking out of the West Wing and into the Eisenhower Executive Office building to celebrate the passage of the farm bill. Trump emerged on stage to the sound of his own voice at the 2005 Emmy's singing a rendition of Green Acres' theme song, all smiles. Meanwhile, Mattis headed back to the Pentagon, where he told his senior staff and the Joint Chiefs of his decision.

News of Mattis' resignation reverberated through Washington like a collective gut punch. The mood at the Pentagon was one of shock and dismay. On Capitol Hill, where lawmakers were stuck in the middle of a contentious budget battle they'd hoped to be done with, Republicans and Democrats alike reacted gloomily. Mattis was seen on both sides of the aisle as one of the last and most reassuring buffers to Trump's rash foreign policy decision-making.

Warrior Monk

For people who truly know Mattis, the news of his resignation likely did not come as a huge surprise. Abandoning the Kurds was his red line. Along with keeping troops in Syria, Mattis had been struggling to convince Trump to keep troops in Afghanistan, too. It became clear he had lost out on both fronts. Not long after Mattis resigned, word went out that Trump had ordered the military to start withdrawing roughly half of the 14,000 US troops there.

While the President's troop withdrawals provided the final impetus for Mattis to submit his resignation, his influence had been waning for months, a sharp break from the early days of the administration.

Back then, Mattis was a frequent lunch guest of the President's and Trump was enamored with the retired four-star general he viewed as straight out of "central casting": stoic, granite-faced, and endowed with a killer nickname: Mad Dog. Even though Mattis detests the moniker, Trump used it frequently to herald his choice. Mattis, a bachelor who served in the First Gulf War, Afghanistan and Iraq, has also been described as a "warrior monk," married to the Marines.

While Trump and Mattis butted heads, the President rarely assailed the retired-four star general with the same impatience and venom he leveled against other Cabinet officials during the first year-and-a-half of the administration.

"They certainly disagreed on things. But he was the only Cabinet official that the President had never yelled at or been visibly upset with," a former senior administration said.

But aides began to fear that Mattis' relationship with Trump would fray after the President ousted Secretary of State Rex Tillerson and national security adviser H.R. McMaster last March.

Mattis, Tillerson and McMaster -- along with outgoing White House chief of staff John Kelly -- frequently joined forces in efforts to dissuade Trump from foreign policy decisions they considered rash, unwise or even dangerous. Together, they often slow-walked presidential orders that fit those descriptions or took turns making their case directly to Trump in an effort to change his mind.

With Tillerson and McMaster gone -- replaced by advisers more aligned with Trump's worldview -- Mattis increasingly found himself alone as the last bulwark against Trump's most impulsive decisions and, as he voiced his opposition, a more frequent target of the President's ire.

"We always suspected that Mattis wasn't going to last a very long time after McMaster left," a former senior administration official told CNN. "That meant that if Mattis is going to have to stand up for his policy beliefs, he's going to have to do it himself."

Over the past several months, Mattis's rapport with Trump slowly degraded. He became less visible in the Oval Office. And Trump grew more exasperated with Mattis' efforts to dissuade him from his isolationist tendencies.

Unlike other Cabinet officials who sought to ingratiate themselves with the President and defend him publicly, Mattis maintained an independent streak -- repeatedly rebuffing the White House's requests to appear on Sunday talk shows.

That refusal came down to what his aides describe as two of Mattis' most important characteristics.

"He is A, genetically incapable of lying and B, genetically incapable of disloyalty," one former Pentagon official said. "So if he is asked on a Sunday show about fill in the blank, he is not going to lie about what his opinion is. But he is also not going to be disloyal to the president."

He acquiesced once, appearing on CBS's Face the Nation in May after Trump personally asked him to, a former defense official said. On the program , asked what keeps him awake at night, Mattis famously replied, "Nothing. I keep other people awake at night."

Butting Heads

It didn't take long for Trump to anger Mattis. During his first week in office, the President used a sacred hall in the Pentagon as the backdrop for one of his most controversial actions.

In a hall devoted to recipients of the Medal of Honor, Trump signed an executive order banning people from several Muslim majority countries from entering the US. Mattis was completely caught off guard, and he made no secret of his displeasure with West Wing officials.

"He was furious after that. Furious at the breakdown in process. And furious that it had been at the Hall of Heroes," a former Pentagon official said. "And that he had been used in the photo op and would be associated with something he had no role in developing."

Beyond guarding against some of Trump's most impulsive decisions, Mattis also sought to steer Trump onto a more traditional course of US foreign policy, a strategy that Trump began to bristle at over time.

According to one senior administration official, there were a string of decisions that Trump felt pressured into by Mattis, leaving the President feeling manipulated and regretful. That included forestalling his withdrawal from the Iran nuclear deal, a move Mattis advised against; signing onto a NATO declaration at the end of the defense bloc's summit in July; and a steady increase in the number of US troops in Afghanistan, Iraq and Syria.

Eight months before his resignation, Mattis managed to forestall the Syria troop withdrawal after the President announced during a rally in Ohio that US troops would soon be coming home.

Meeting later in the Situation Room , Trump told his military and national security advisers they had six months to wrap up the mission in Syria. Mattis and others warned that would not be sufficient, but the agreement at least bought US forces more time in Syria -- and gave Mattis more time to try and change Trump's mind.

It took 21 months for the relationship to fully unravel, a process that culminated in October, when Trump referred to Mattis as "sort of a Democrat," during an interview with 60 Minutes. Trump's decision to discard Mattis' advice over who to nominate as the next chairman of the Joint Chiefs was also a blow. Mattis recommended Air Force Chief of Staff Gen. David Goldfein. Trump instead choose Army Chief of Staff Gen. Mark Milley. That dispute did not play out publicly, but Mattis found Trump's willingness to discard his advice insulting, according to a senior defense official with direct knowledge.

North Korea split

While Trump frequently undercut Tillerson as secretary of state, the President kept his disagreements with Mattis private. That changed this summer as the issue of suspending joint US-South Korea military exercises near the Korean peninsula spilled into public view. At a Pentagon briefing in late August, Mattis suggested the war games were back on the table, despite Trump's pledge during his Singapore summit with Kim Jong Un to put them on hold.

The next day, Trump took to Twitter to make clear who was calling the shots.

"The President believes that his relationship with Kim Jong Un is a very good and warm one, and there is no reason at this time to be spending large amounts of money on joint US-South Korea war games," Trump tweeted, issuing what he called a "statement from the White House."

Trump and Mattis clashed repeatedly over how best to approach the North Korean nuclear threat and over Trump's wavering commitment to the US's military presence in South Korea, which Trump found costly.

As Trump mulled the merits of launching a limited military strike against North Korea, he took the provocative step of ordering the evacuation of military families in South Korea -- a move Mattis and other top national security officials feared could send a signal that the US was on a war footing.

As McMaster directed his NSC staff to begin drafting the order, Mattis worked with White House chief of staff John Kelly to dissuade the President, ultimately convincing him to issue a scaled-down directive barring military personnel in South Korea from bringing their families during future tours.

That memo was also never implemented -- one of many presidential directives Mattis slow-walked with the hope that Trump would forget about them and move on.

Clashing over alliances

From the very beginning, it didn't seem that Trump ever saw much beyond Mattis' military uniform, often mistaking his reputation for toughness as an indication he was onboard with Trump's America First agenda. That was particularly obvious the day Trump introduced Mattis as his defense secretary during a victory rally with his supporters in December 2016. As he began ticking down Mattis' resume, Trump appeared struck by a sudden realization.

"Jim is a Marine Corps four-star general, the former commander of US Central Command and NATO's Supreme Allied Commander..." he said, pausing in thought before offering the rest of Mattis' former title, his voice now conveying a sense of wonder, "for Transformation-- this is going to be so great for us!"

Trump had repeatedly denigrated NATO during his campaign for president, refusing to commit to the alliance's mutual defense clause and citing insufficient financial contributions from some of its members. Now, as President-elect, he seemed to believe his incoming defense secretary would be a partner in his mission to radically reshape -- or simply scrap -- the military alliance.

Two years later, there is no issue Mattis stressed more in his resignation letter than the importance of America's alliances.

"One core belief I have always held is that our strength as a nation is inextricably linked to the strength of our unique and comprehensive system of alliances and partnerships. While the US remains the indispensable nation in the free world, we cannot protect our interests or serve that role effectively without maintaining strong alliances and showing respect to those allies," Mattis wrote.

It was a lesson Mattis frequently sought to impart to Trump, whether he was explaining the importance of the US maintaining military bases around the world or urging Trump not to denigrate the NATO military alliance.

The letter amounts to a point-by-point rebuttal of Trump's foreign policy outlook. It is also notable for what it doesn't say. Mattis offers not a single word of praise for Trump.

"I very much appreciate this opportunity," Mattis wrote in his closing sentence, "to serve the nation and our men and women in uniform."

After delivering his letter to the President and returning to the Pentagon, Mattis spent Thursday night joking and laughing with shocked senior staff, according to the senior defense official. Mattis assured them that everything would be OK, repeatedly saying that it's not all about him— it's about the troops and the job.

The next morning, he was back at work and in his office at 6:15am, his usual time.