Iran struck back at the United States early on Wednesday for killing its most powerful military commander, firing a barrage of ballistic missiles at two Iraqi military bases that house American troops in what the Iranian supreme leader said was a “slap” against America's military presence in the region.

The retaliation risks a dangerous escalation in the Middle East. Still, behind the dramatic barrage of more than a dozen missiles, Iran averted casualties by giving some advance warning before hitting heavily fortified bases. This could open a path to step away from an immediate spiral of more direct strikes between U.S. and Iran.

Iran's Supreme Leader Ayatollah Ali Khamenei made clear that Iran's ballistic missile strikes were in revenge for the U.S. killing of Revolutionary Guard Gen. Qasem Soleimani, whose death last week in an American drone strike in Baghdad prompted angry calls to avenge his slaying and drew crowds of Iranians, said to number in the millions, to the streets to mourn him. Khamenei himself wept at the funeral in a sign of his bond with the commander.

“Last night they received a slap,” Khamenei said in a speech after the missile strikes. “These military actions are not sufficient (for revenge). What is important is that the corrupt presence of America in this region comes to an end."

Despite the heightened rhetoric, there were some indications that there would not be more immediate retaliation on either side.

'All is well!' President Donald Trump tweeted shortly after the missile attacks, adding, 'So far, so good' regarding casualties.

All is well! Missiles launched from Iran at two military bases located in Iraq. Assessment of casualties & damages taking place now. So far, so good! We have the most powerful and well equipped military anywhere in the world, by far! I will be making a statement tomorrow morning. — Donald J. Trump (@realDonaldTrump) January 8, 2020

Moments earlier, Iran's foreign minister tweeted that Tehran had taken “& concluded proportionate measures in self-defense,” adding that Tehran did “not seek escalation" but would defend itself against further aggression.

It appeared Iran gave advance warning of the strikes. Iraqi Prime Minister Adel Abdul-Mahdi said he received notification from Iran just after midnight that its retaliation “was starting or would start soon” and would focus only on American positions. Finland and Lithuania's militaries, which had personnel at one of the targeted bases, said they received information about an imminent attack and had time to move to shelters or leave the base.

Iran’s attacks “appeared designed for maximum domestic effect with minimum escalatory risk,” said Henry Rome, analyst with Eurasia Group.

"For a president who wants to avoid a war in the Middle East during an election year, the Iranians have provided an off-ramp he will likely take," Rome said.

Tensions have risen steadily in the Middle East since Trump's decision to unilaterally withdraw America from Tehran's nuclear deal with world powers.

The Iranian strikes on the bases marked the first time in recent years that Iran has directly attacked U.S. positions rather than through proxies in the region.

It raised the chances of open conflict erupting between the two rivals, who have been at odds since Iran's 1979 Islamic Revolution and the subsequent U.S. Embassy takeover and hostage crisis.

Adding to the chaos, a Ukrainian airplane with 176 people crashed after takeoff just outside Tehran on Wednesday morning, killing all on board, Iranian state TV and Ukrainian officials said. Mechanical issues were suspected.

Two U.S. military bases in Iraq were hit by missiles early Wednesday in an attack Iran said was retaliation for the assassination of General Qasem Soleimani.

The Boeing 737-800 had taken off from Imam Khomeini International Airport, bound for the Ukrainian capital, Kyiv. The plane carried 167 passengers and nine crew members. Ukraine's foreign minister, Vadym Prystaiko, said there were 82 Iranians, 63 Canadians and 11 Ukrainians on board — the Ukrainian nationals included two passengers and the nine crew. The rest were Swedish, Afghan, German and British nationals.

The U.S. Federation Aviation Administration earlier warned of a "potential for miscalculation or mis-identification" for civilian aircraft in the Persian Gulf. The agency barred U.S. pilots and carriers from flying over areas of Iraqi, Iranian and some Persian Gulf airspace.

The U.S. has been deploying more troops in the region. U.S. Gulf allies that host thousands of American troops are concerned of an outbreak of direct conflict and retaliation from Iran. The Saudi Arabia and the United Arab Emirates have called for de-escalation.

“The situation is not currently a war situation,” UAE Energy Minister Suhail Al-Mazrouei told reporters Wednesday, stressing that Iran is a neighbor and the last thing the country wants is more tension in the region.

Iran’s Revolutionary Guard warned the U.S. and its allies against retaliating over the missile attack.

“We are warning all American allies, who gave their bases to its terrorist army, that any territory that is the starting point of aggressive acts against Iran will be targeted,” the Guard said in a statement carried by Iran’s state-run IRNA news agency. It also threatened Israel.

The Iranians fired a total of 15 missiles in Wednesday's strike, two U.S. officials said. Ten hit the Ain al-Asad air base in Iraq’s western Anbar province and one targeted a base in Irbil in Iraq's semi-autonomous Kurdish region. Four failed, said the officials, who were not authorized to speak publicly about a military operation.

“As we evaluate the situation and our response, we will take all necessary measures to protect and defend U.S. personnel, partners and allies in the region,” Jonathan Hoffman, an assistant to the U.S. defense secretary, said.

U.S. Secretary of State Mike Pompeo said every action taken will be “conducted inside the international laws of war.”

Two Iraqi security officials said a missile appeared to have struck a plane at Ain al-Asad, igniting a fire. There were no immediate reports of casualties from the attacks, according to the officials, who spoke on condition of anonymity as they had no permission to talk to journalists.

Ain al-Asad was first used by American forces after the 2003 U.S.-led invasion that toppled dictator Saddam Hussein, and it later was used by American troops amid the fight against the Islamic State group. It houses about 1,500 U.S. and coalition forces. Trump visited it in December 2018, making his first presidential visit to troops in the region. Vice President Mike Pence also has visited the base.

Take a look back at the video and tweets Donald Trump released and posted about potential military conflict with Iran while Barack Obama was president.

As Iran reels from Soleimani's death and the loss of life on the plane crash, it is also contending with the aftermath of a deadly stampede that broke out Tuesday at Soleimani's funeral, killing 56 people and injuring more than 200. Shortly after Wednesday's missile attack, Soleimani's shroud-wrapped remains were lowered into the ground as mourners wailed at the grave site in his hometown of Kerman.

The general's funeral procession in major cities spanning three days was an unprecedented honor for Soleimani, seen by Iranians as a national hero for his work leading the Guard’s expeditionary Quds Force in the face of U.S. pressure.

The U.S. blames him for killing U.S. troops in Iraq. The Trump administration alleges he'd been plotting new attacks just before he was killed.

Many Sunni Muslims in the region, however, view him as a destabilizing figure who commanded dangerous and deadly Shiite proxy militias. Soleimani led forces supporting Syrian President Bashar Assad in that country's civil war.

Since his killing Jan. 3, Tehran has abandoned the remaining limits of its 2015 nuclear deal with world powers. In Iraq, lawmakers and pro-Iranian factions in parliament have voted to oust American troops from Iraqi territory.

Kuwait said Wednesday that its state-run KUNA news agency’s Twitter account was hacked and posted a false story on U.S. troops withdrawing from the nation. The fake alert went out on its account, Kuwait said. More than 13,000 U.S. troops are stationed in Kuwait, with more now on the way.

Gambrell reported from Dubai, United Arab Emirates. Associated Press writers Matthew Lee, Lolita C. Baldor and Zeke Miller in Washington, Qassim Abdul-Zahra in Baghdad and Zeina Karam in Beirut contributed.