Three years ago this week, an attempted coup sought to oust Turkish strongman Recep Tayyip Erdogan and reverse his efforts to transform Turkey. Instead, it gave Erdogan a chance to accelerate his changes.

Today, Erdogan’s “new Turkey” has turned its back against its Western allies and the values it once shared with them.

Erdogan’s determination to transform Turkey was evident long before the coup. But the events of July 15, 2016, pushed it into overdrive. That night, soldiers blocked the main bridges in Istanbul, detained military leaders and forced their way into the state broadcaster to declare a coup. The parliament building was bombed, more than 100 civilians were killed and Erdogan’s jet was almost shot down — with the president onboard.

But Erdogan soon broadcast a message urging people to take to the streets to oppose the coup — which they did. High-ranking officers quickly reaffirmed their loyalty to the government; the coup fizzled.

“This uprising is a gift from God,” Erdogan declared upon arriving in Istanbul. The pious president wouldn’t waste this providential gift.

He accused Fethullah Gulen, an aging Islamist guru and erstwhile ally living in self-imposed exile in the Poconos, of having masterminded the coup. It was time to “cleanse” Turkey of Gulen’s influence. Within hours, some 2,000 soldiers, nearly 3,000 judges and 8,000 police officers had been arrested.

The authorities forced out two-thirds of Turkey’s generals and admirals, many of them trained in the United States. They also fired more than 150,000 civil servants. All told, Erdogan’s government arrested nearly 100,000 on suspicion of complicity in Gulen’s failed coup.

In Erdogan’s telling, the Gulenist threat is a murky force, bent on Turkey’s destruction in cahoots with Kurdish separatists, the CIA, even ISIS. A traumatized Turkey was willing to believe him, including his core socially conservative supporters as well as secular liberals, who detest Gulen, and the pro-Russian nationalists, with whom Erdogan’s conspiracy theories most resonated. Thus Erdogan won an opening to grab more power and turn Turkey further away from the West.

It is fitting, then, that the third anniversary of the coup attempt has coincided with the arrival in Turkey of the S-400, the signature Russian air-defense system. Erdogan sees the S-400 as an insurance policy for his power. The S-400 is also a sign of Erdogan’s disregard for Turkey’s place in the Western alliance.

But while Turks are coming to resent Erdogan’s autocratic overreach at home — voters defeating his candidate in the Istanbul rerun by a huge margin — his anti-Americanism is still gaining ground. Erdogan’s ideologues portray the United States as an “enemy country.” Turks increasingly buy that line. Seven out of 10 Turks now report feeling threatened by US power, a 28-point increase since 2013 — a higher jump than in any country recently polled.

This drift away from the West is the biggest challenge facing the US-Turkish relationship. Never since the end of the Cold War has Washington had greater need of a strong partnership with Turkey. Its strategic location, military strength, (still extant) republican political institutions and historical connections to contested regions make it an invaluable asset amid threats from China, Russia and Iran.

Erdogan might have thrown in his lot with these American competitors, but the orientation of Turkey is still undetermined. Countering his propaganda and rebuilding the trust of Turkish society should be at the heart of US strategy.

In responding to Turkey’s S-400 purchase, then, US policymakers should check their punitive impulse. To be sure, America must protect its national-security interests. Congress should immediately pass legislation to ensure that, so long as Turkey possesses the S-400, which was designed to shoot down the F-35 fighter, the country won’t receive the fighter.

But a more measured response is needed when it comes to levying sanctions. Rather than hitting the government as a whole or seeking to strangle the faltering Turkish economy, the White House would do better to adopt a targeted approach. Making all Turks pay the price for Erdogan’s sins will do nothing to change his behavior. But it might convince the people that their loyalties are better placed with Moscow.

Instead, US sanctions should target specific individuals in the Erdogan regime — those most responsible for driving Ankara toward Moscow. This move would maintain US credibility, while weakening pro-Russian elements in Turkey and ­undermining Erdogan’s narrative of American spitefulness.

The coup attempt three years ago was a great boon to Erdogan. US policy should ensure that its anti-American effects don’t survive Erdogan.

Blaise Misztal is a Hudson Institute fellow.