The latest weapon in this country’s ideological war with Iran arrived late last month in an armada of jets from California, accompanied by a private security force, dazzling pyrotechnics and a wardrobe that consisted of sequins and not much else.

A crowd of nearly 30,000 gathered to watch as the leader of this mini-invasion pranced onto a stage built on the edge of the Caspian Sea. With a shout of “Hello, lovers!” Jennifer Lopez wiggled out of her skirt and launched into a throbbing disco anthem, delighting her Azerbaijani fans and — it was hoped — infuriating the turbaned ayatollahs who live just across the water.

“You could almost feel the Iranians seething,” said an Azerbaijani official who attended the U.S. pop star’s first concert in this predominantly Shiite Muslim country of 9 million. “This stuff makes them crazy.”

The effect on Iran’s leaders is real enough, and it is at least partly by design. Azerbaijan, Iran’s neighbor and longtime rival, is coming to relish its role as the region’s anti-Iran, a secular, Western-leaning country that is working mightily to become everything that Iran is not.

As Iran sinks ever deeper into isolation and economic distress, its northern neighbor is sprinting in the opposite direction, building political and cultural ties to the West along with new pipelines connecting energy-hungry Europe with the country’s rich petroleum fields on the Caspian Sea. Where Iran is repressive and theocratic, Azerbaijan is socially and religiously tolerant, offering itself as a model of a nonsectarian, Muslim-majority society that champions women’s athletics and embraces Western music and entertainers.

It also enthusiastically pursues diplomatic and business ties with Israel, the Jewish state that Iranian officials have threatened to destroy.

Azerbaijan’s leaders insist that such policies have nothing to do with Iran, and they point to a record of mostly cordial relations with the vastly larger, notoriously peevish republic to the south. Yet, with each stride toward modernity — and with every Western diva who arrives to croon and titillate on Baku’s expanding international stage — Azerbaijan chips away at the legitimacy of Iran’s government and fuels discontent among ordinary Iranians, say Western officials who study the region.

“It is one of the most serious threats to the long-term viability of the Iranian regime,” said Matthew Bryza, a former U.S. ambassador to Azerbaijan who now works as a private consultant. “Every day that Azerbaijan grows stronger economically and more connected to the Euro-Atlantic community — that’s another day in which the Iranian regime grows weaker.”

It is hardly a perfect role model. The government in Baku is dominated by a single political party, and it has frequently come under criticism by independent watchdogs for its human rights record and alleged corruption. Azerbaijan also is mired in a nearly two-decade-old conflict with another of its neighbors, Armenia, over control of the disputed enclave known as Nagorno-Karabakh.

Still, even as they press for faster reforms, Western governments are seeing a lot to like about a country whose steady ascent makes Iran’s failings appear even more wretched by comparison.

“Iranians now see Azerbaijan emerging as a regional player at a time when they are being sidelined by sanctions,” said a Baku-based Western diplomat who insisted on anonymity in discussing his country’s geopolitical assessments. “They see new embassies opening and new foreign investment pouring in. . . . Azerbaijan is gaining, and Iran is losing.”

A cultural coming out

The visit to Baku by the pop star known as J-Lo was only one in a string of events marking the cultural coming out of a newly assertive Azerbaijan. Concert promoters have lured a steady stream of A-list Western entertainers to the country in recent weeks, including fellow pop icon Rihanna, who arrived in this Caspian seaport two weeks after Lopez’s Baku premiere on Sept. 23. Rihanna was followed by blond songstress Shakira, the closer in a triumvirate of female performers known for skimpy costumes and sexually provocative dance moves.

If Baku’s mostly Muslim concert-goers were offended, they showed no signs of it. Tens of thousands of young Azerbaijanis paid the equivalent of a week’s salary to dance along with Lopez at her show in Baku’s Crystal Hall, a lavish arena built on a small finger of land jutting into the Caspian. Many others camped outside a downtown hotel for a chance to glimpse the American pop icon.

“Best show I’ve ever seen,” gushed “Leyla,” a Baku woman who posted a pithy review on a fan site after Lopez’s performance.

Azerbaijani officials say they invited female vocalists to help draw attention to yet another event: the under-17 Women’s World Cup soccer tournament, hosted this year by Baku. The overall idea, they say, is to burnish Azerbaijan’s standing as a progressive country that not only protects women’s rights, but also promotes female participation in sports and the arts.

“We recognize that having a secular, progressive state is important for the well-being of this country,” said Novruz Mammadov, an Azerbaijani diplomat and director of foreign relations in the administration of President Ilham Aliyev.

Azerbaijani officials say it’s by happenstance that the country’s policies run against the grain of more conservative countries in the region. In fact, the contrasts could not be more striking — particularly with regard to Iran. And Western concerts and soccer games are only part of it.

In Baku, an ancient seaport in which Zoroastrian ruins coexist with brilliantly lighted glass skyscrapers, young couples hold hands or embrace on park benches along the broad, tree-lined promenade that hugs the Caspian shoreline — public displays of affection that are officially banned in Tehran. Women and girls in designer jeans hunt for bargains at Western clothing stores such as Bebe and Benetton. Head scarves are rare, but karaoke bars and nightclubs are plentiful along the downtown thoroughfares choked with traffic and new construction.

Fueled by a booming oil industry, the country’s gross domestic product rocketed forward at a 35 percent annual rate — the world’s highest — in the mid-2000s before cooling off in the face of the European recession. The cash influx paid for the city’s gleaming skyline while helping lower the official poverty rate from nearly 50 percent to less than 16 percent in a single decade.

The central government has struggled to raise standards for education and health care, particularly in rural areas. But, while most Azerbaijanis share the same Shiite beliefs as their Iranian cousins, Baku has managed to prevent the emergence of religious extremism — at times trampling on political freedoms to do so. Government officials say an overwhelming majority of Azerbaijanis are proud of the country’s secular traditions, which already were well established before Azerbaijan became part of the Soviet Union.

“It’s not so much that we have changed, it’s that others have come to understand who we are,” said Mikayil Jabbarov, director of the country’s historical and architectural preservation agency. “We had girls’ schools 100 years ago, and we were the first Muslim country to give women the vote. The mentality was shaped back then.”

Song contest crisis

Only more recently have these traditions become a problem for Azerbaijan’s neighbors, government officials say. Iran’s irritation with Western-leaning Azerbaijan turned to resentment and then hostility in the wake of published reports last year that Azerbaijan supplied assassins for an Israeli effort to kill Iran’s nuclear scientists — an allegation that Azerbaijan vehemently denies.

Then, in February, Azerbaijani authorities disrupted what they said was an Iranian plot to kill Israeli diplomats and Jewish schoolteachers in Baku. An investigation would later implicate 22 Iranian operatives in a series of alleged schemes to target Western embassies and businesses, including the U.S. diplomatic mission in Baku.

Relations between the two capitals cratered. But the worst crisis was yet to come, and it was over a cultural event: Azerbaijan’s election to become the host of this year’s televised and highly popular Eurovision Song Contest. The contest was Azerbaijan’s chance to shine, and the country spent billions of dollars building an arena and sprucing up its central avenues for the expected onslaught of tourists. Iran, however, attacked the event as an anti-Islamic “gay parade” and withdrew its ambassador in protest.

The harsh reaction left Azerbaijanis shaking their heads. “I do not know who got this idea into their heads in Iran,” Ali Hasanov, head of the administration’s public and political issues department, told reporters at the time. “We are hosting a song contest, not a gay parade.”

But by then, Azerbaijanis had acquired a taste for Hollywood-style glamour, and their government was enjoying the international attention as well as an awareness of Iran’s extreme discomfort. Tickets for the Jennifer Lopez concert went on sale the following month and sold out quickly — delighting the city’s concert promoters and winning new admirers for a country that appears to have sided firmly with musicians over mullahs, with implications that extend far beyond its borders.

“It’s easy to make fun, but this is part of their foreign policy strategy, and it’s actually smart,” a second Western diplomat said of Azerbaijan’s canny embrace of pop. “On one level, it says to the world, ‘We’re a real country, and we can attract world-class entertainment.’ On another level, it drives the Iranians to distraction.”