WASHINGTON — In a move that caught administration officials off guard, Nikki Haley announced her resignation Tuesday as President Donald Trump's ambassador to the U.N.

Together in the Oval Office, Trump and Haley confirmed that she would leave his administration at the end of the year.

"We're all happy for you in one way, but hate to lose you," Trump said, sitting alongside Haley. "At the end of the year, Nikki will be leaving." The interaction between the two was warm, and they spent time praising each other.

A source close to Haley insisted that she's leaving on good terms and that she was not forced out. But inside the White House, officials were caught off guard by her announcement, learning of it from reporters who started inquiring, two White House officials told NBC News.

Two other officials said there was disappointment in the West Wing that Haley chose to step down before the midterm elections in less than a month.

Read Ambassador Nikki Haley's full letter of resignation

On the Hill, several Democrats and Republicans working for the Senate Foreign Relations Committee said they were also caught by surprise.

Haley said serving as the U.S. ambassador to the U.N. has been "the honor of a lifetime." She added that she didn't know what's next for her, but that she would not be running for president in 2020 and would be supporting Trump's re-election.

"It's been eight years of intense time and I'm a believer in term limits," she said, referring to her six-year tenure as governor of South Carolina and two years as ambassador. "I think you have to be selfless enough to know when you step aside."

She noted foreign policy achievements, such as North Korea sanctions packages, moving the U.S. Embassy in Israel to Jerusalem, pulling out of the Iran nuclear deal, and pressuring NATO members to pay more.

"Now, the United States is respected,” she said. "Countries may not like what we do, but they respect what we do."

Trump declined to name Haley's successor — saying he would announce his selection within two or three weeks — but noted he had heard from a number of people who would like the job.

Former White House official Dina Powell, an executive at Goldman Sachs, is under consideration, according to a source familiar with the matter. It is not known whether the former deputy national security adviser would be interested in the job.

"Dina is a person I would consider," Trump said later Tuesday, when asked about reports that Powell's name had been floated.

Speaking to reporters on the White House South Lawn, Trump added that Haley was going to help choose her replacement. He also said that his daughter, Ivanka Trump, would be "incredible" in the role — but he would be accused of "nepotism."

After his comments, Ivanka Trump said on Twitter that while she knows her father will "nominate a formidable replacement" to Haley, "that replacement will not be me."

Axios was first to report Haley's plan to resign.

Trump said Tuesday morning that Haley had come to him six months ago and asked to take a break.

NBC News obtained Haley's resignation letter, dated Oct. 3. In her letter, Haley touted her accomplishments as U.N. ambassador and previewed her future.

"We spoke out resolutely against dictatorships in Iran, Syria, Venezuela, Cuba, and yes, Russia. Through it all, we stood strong for American values and interests, always placing America first. I am proud of our record," she said.

She said she would serve until January 2019 to give Trump "time to select a replacement, and to give the Senate time to consider your selection."

"At that point, I will once again become a private citizen. I expect to continue to speak out from time to time on important public policy matters, but I will surely not be a candidate for any office in 2020," she said.

In a statement, a U.S. Mission to the U.N. spokesperson said Haley's departure has nothing to do with an outside watchdog group's recent request that the State Department's inspector general investigate possible ethics violations related to Haley's acceptance of flights on private planes.

“They are unrelated,” the USUN Spokesperson said. The spokesperson referred additional questions to the Office of Inspector General, which did not respond to a request for comment from NBC News.

House Speaker Paul Ryan, R-Wisc., said on Twitter that Haley "has been a clear, consistent, and powerful voice for America's interests and democratic principles on the world stage."

"I am saddened that she is leaving the administration, but so grateful for her service," he wrote.

Haley, considered a rising star within the GOP, was easily confirmed to her U.N. post in January 2017 by the Senate with bipartisan support, 96-4.

In September, Haley was among a number of senior administration officials who denied writing the explosive anonymous opinion article published in The New York Times in which the author claimed to be part of a secret "resistance" within the Trump administration out to undermine parts of the president's agenda.

Later that month, Haley forcefully countered any suggestion that she could be the author with an opinion piece of her own.

"I don't agree with the president on everything. When there is disagreement, there is a right way and a wrong way to address it. I pick up the phone and call him or meet with him in person,” she wrote in The Washington Post.

"Like my colleagues in the Cabinet and on the National Security Council, I have very open access to the president. He does not shut out his advisers, and he does not demand that everyone agree with him. I can talk to him most any time, and I frequently do."

She added: "If I disagree with something and believe it is important enough to raise with the president, I do it. And he listens."

As ambassador, Haley was a strong advocate for Trump's foreign policy goals, while occasionally breaking with the president on high-profile issues.

She oversaw the withdrawal of the U.S. from the United Nations Human Rights Council in June, which she called “a protector of human rights abusers, and a cesspool of political bias."

She was also an outspoken critic of Russia during her tenure, which caused some tension with the administration. For instance, in April, Trump’s economic adviser, Larry Kudlow, said Haley was suffering from "momentary confusion" when she announced at the time that new sanctions against Russia were imminent.

Haley fired back at Kudlow, saying, "With all due respect, I don't get confused."

Haley also broke with the administration in April when she said she believes Russia was responsible for the attempted assassination of a former Russian double agent and his daughter in Britain — and the U.N. Security Council should hold the Kremlin "accountable."

Trump at that time had not yet pinned the blame on the Kremlin, saying that "as soon as we get the facts straight," the United States would "condemn Russia or whoever it may be."

Speaking for the first time at the United Nations as ambassador in January 2017, Haley outlined the Trump administration’s vision for the role of the U.S. at the international organization. She said America would project strength and protect its allies.

"For those who don’t have our backs, we’re taking names,” she said.

Haley added, "Everything that is working, we are going to make it better. Everything that is not working, we are going to try and fix. And anything that seems to be obsolete and not necessary, we are going to do away with."

Trump has criticized the United Nations’ effectiveness in the past, describing it in December 2016 as “just a club for people to get together and talk.”

At Tuesday's Oval Office announcement, however, Trump praised Haley for improving America's image with foreign countries.

"I think the world is really respecting the United States again, much more so than they have in many, many decades," Trump said. "And people want to be on our side."

Haley, the child of Indian immigrants, was the first woman and the first minority to be elected governor of South Carolina. She has also been frequently mentioned as a potential presidential candidate.

As governor of South Carolina, Haley received national attention for criticizing the Confederate flag in the wake of a massacre at a black church in Charleston, in which an admitted white supremacist killed nine worshipers.

Haley had also criticized Trump during the 2016 presidential campaign.

“Every time someone criticizes him, he goes and makes a political attack back,” Haley said of Trump in 2015. “That’s not who we are as Republicans. That’s not what we do.”

She added, “[Americans] want to know they’re sending someone up to the White House that’s going to be calm and cool-tempered and not get mad at someone just because they criticize him. We would really have a world war if that happens.”

After she was named U.N. ambassador, she said in a statement, “When the president believes you have a major contribution to make to the welfare of our nation, and to our nation’s standing in the world, that is a calling that is important to heed."

Abigail Williams and Kristen Welker reported from Washington, and Dartunorro Clark reported from New York.