James D. Melville Jr. addresses dignitaries in front of a U.S. Army tank at a handover ceremony of the upgraded NATO military base in Tapa, Estonia, in 2016. (Vitnija Saldava/AP)

The U.S. ambassador to Estonia — a NATO ally bordering Russia — abruptly resigned Friday, telling friends that he cannot abide President Trump's apparent hostility toward institutions that have stabilized Europe since the end of the Cold War.

James D. Melville Jr.'s resignation comes at a crucial moment for independent countries along Russia's western border — amid the possibility of military conflicts and as Trump suggests he is rethinking the United States' traditional support for its allies in Moscow's shadow.

“The honorable course is to resign,” Melville wrote on Facebook. “Having served under six presidents and 11 secretaries of state, I never really thought it would reach that point for me.”

He added: “For the President to say the [European Union] was 'set up to take advantage of the United States, to attack our piggy bank,' or that 'NATO is as bad as NAFTA' is not only factually wrong, but proves to me that it's time to go.”

Estonia is one of several formerly Soviet-controlled countries that have joined the North Atlantic Treaty Organization — essentially allying with the United States and Western Europe, whose militaries protect Estonia against Russian aggression.

Situated between Russia and the Baltic sea, tiny Estonia has been especially wary of its former occupier since 2007, when a massive cyberattack from servers in Russia crippled Estonia's government, banks and news organizations.

Since then, Russia has sent military forces into other neighboring countries — Georgia and Ukraine — raising fears that it could one day target the Baltic states.

Just before the U.S. presidential election in 2016, The Washington Post documented how NATO fighter jets routinely scrambled from an Estonian air base to meet Russian warplanes testing the country's airspace. Both Russia and NATO have recently staged military exercises that some analysts see as thinly disguised simulations of a war over the Baltic region.

As for the leaders of the two most powerful countries in this conflict: Russian President Vladimir Putin sounds increasingly hawkish toward what many Russians see as an aggressive, expansionist military bloc on their country's western border — and Trump sounds increasingly amenable to the Kremlin's point of view.

A few months into Trump's presidency, The Post asked Estonian President Kersti Kaljulaid whether she was worried about a Russian military attack.

“Actually, no,” she replied. “Because of NATO.”

But, Kaljulaid added, “I am afraid now that the resolve of the Western countries may not hold in the case of Ukraine.”

Trump has given her a lot more to worry about since that interview.

As special counsel Robert S. Mueller III looks into possible coordination between Trump's presidential campaign and Moscow, Trump has suggested that the United States may recognize Russia's 2014 annexation of the Ukrainian territory of Crimea. He has appeared to undermine the European Union, to which Estonia belongs. He has accused NATO members of not spending enough on defense and — as Axios has reported — disparaged the alliance in front of other world leaders.

As Foreign Policy noted when it reported Melville's resignation Friday, NATO countries fear that Trump will snub them further at an upcoming summit — before he meets personally with Putin.

The State Department confirmed that Melville announced his immediate resignation on Friday, without going into details as to why or answering questions about who now runs the U.S. Embassy in Estonia.

During more than three decades as a diplomat, Melville had served at U.S. embassies in Moscow, London and Berlin before President Barack Obama appointed him to the ambassadorship in Estonia in 2015.

Trump nominated a retired Navy admiral, Edward Masso, to replace Melville late last year — but withdrew the nomination last month for unknown reasons, according to Estonian public radio.

In a Facebook post obtained by The Post, Melville wrote that he had been planning to wait for a replacement to be approved before retiring from public service — but that his principles would not allow him to continue serving under Trump.

So he will instead follow diplomats such as the U.S. ambassador to Panama, who last year resigned from serving under Trump “because my values were not his values.”

“I suppose I could have stayed on for many more months,” Melville wrote near the end of his Facebook message. “I do love Estonia, its wonderful people and beautiful landscape.”

“But on balance, I'm glad not to be staying, for all the reasons I've just explained,” he continued. “So I leave willingly and with deep gratitude for being able to serve my nation with integrity for many years, and with great confidence that America, which is and has always been, great, will someday return to being right.”

The full text of Melville's Facebook message follows:

I suppose this is as good a time as any to tell my friends that I'm leaving Estonia one month from today, on July 29, to retire from the Foreign Service and begin life as a private citizen after 33 years of public service.

I've always admired the professionalism of my colleagues in supporting U.S. Government policy as articulated and directed by our elected leaders and their administrations, without regard to partisan politics. A Foreign Service Officer's DNA is programmed to support policy and we're schooled right from the start, that if there ever comes a point where one can no longer do so, particularly if one is in a position of leadership, the honorable course is to resign.

Having served under six presidents and 11 secretaries of state, I never really thought it would reach that point for me.

I truly believe and have said many times, national interests don't change from one administration to the next. Senator Vandenberg made a great point 70 years ago when he said “politics stop at the water's edge.” I've spent the vast majority of my career on US-European relations and issues, and I've always been very proud of the U.S. role in the aftermath of World War II, of rebuilding a Europe that ideally would be “whole, free and at peace.” From the Marshall Plan, through the Cold War and until very recently, supporting Europe's integration was a fundamental element of U.S. foreign policy which directly undergirds democracy, peace and prosperity.

The E.U. and NATO are the gorgeous and vital fruits of that policy and the world is a much, much better place for their existence. I believe that to my marrow.

For the President to say the E.U. was “set up to take advantage of the United States, to attack our piggy bank,” or that “NATO is as bad as NAFTA” is not only factually wrong, but proves to me that it's time to go.

It takes no courage on my part to do so and I can't hold a candle to my friends who have honorably resigned without the benefit of the full pension I have waiting for me. The truth is I've had a full tour in Tallinn and intended to retire upon the confirmation of a successor. Since there's no longer anyone in sight for that role, I suppose I could have stayed on for many more months.

I do love Estonia, its wonderful people and beautiful landscape. Tallinn is one of the nicest cities on earth and I've got the best public housing I've ever had. But on balance, I'm glad not to be staying, for all the reasons I've just explained.

So I leave willingly and with deep gratitude for being able to serve my nation with integrity for many years, and with great confidence that America, which is and has always been, great, will someday return to being right.

Michael Birnbaum contributed to this report.