When Heydar Aliyev, Azerbaijan's former Communist leader, arrived in Baku in 1993 to take over control the country was facing defeat on many fronts. Azerbaijan was losing the war in Nagorno-Karabakh; cities were flooded by displaced persons; the economy was hit by the collapse throughout the Soviet economic space; Russia and Iran were hostile and the West was largely uninterested. Aliyev was able to give the population stability. He reversed many of Elchibey's foreign policy decisions and was able to normalize relations with all major powers in the region.
The most pressing dilemma for Aliyev was to decide what to do in the war over Nagorno-Karabakh and adjacent regions. As Thomas de Waal noted, the confusion which marked the period from Elchibey's ouster to Aliyev's election had serious consequences:
"During four months of confusion, after one president lost office and before another gained it, Azerbaijan lost a huge swath of territory to Armenians east and south of Nagorny Karabakh. This was effectively when the war was lost."
Initially, Aliyev did try to regain Nagorno-Karabakh. To minimize the political threat to himself, he disbanded battalions loyal to the Popular Front. He was then faced with the challenge of creating a real army. In December 1993, Aliyev launched a new military campaign in Nagorno-Karabakh, which lasted throughout the spring of 1994. It failed. These months saw a high number of Azerbaijani casualties far exceeding Armenian ones. In May 1994, Russia negotiated a ceasefire agreement, and the war was technically over.
Aliyev decided to mend relations with Russia and Iran and to create better ties with the West. He pursued a foreign policy described as balanslastirilmis (balanced). This, he argued, was essential for Azerbaijan to survive in a difficult neighborhood and rebuild its economy. As Thomas Goltz described it, Aliyev "played the Iranian card well" and "was not above groveling before the Russians as part of his policy of realpolitik." Azerbaijan agreed to join the Russian-led Commonwealth of Independent States in 1993 (the step which was sharply criticized by many Popular Front supporters and Elchibey himself).
Aliyev acknowledged Moscow's interests in the South Caucasus region. He signed protocols referring to the common defense and economic space. Goltz noted that Aliyev paid "lip service" to Moscow and "obstinately refused to implement any of the agreements in a meaningful way." The ties between the two countries improved in and resulted in a number of strategic commercial and economic agreements, most notably a lease of Qabala (Gabala) radar station and the delimitation of the Caspian Sea.
Aliyev maintained friendly ties to Turkey through his personal relationship with Demirel. Aliyev saw Turkey as a strategic partner and a "gateway to the West for the Azerbaijani economy", and faithfully cultivated this friendship Turkey was also the safest way for Azeri oil to reach Western markets.
Baku-Tbilisi-Ceyhan pipeline. Photo: earthmagazine.com
Aliyev used his country's hydrocarbon resources to give the West a stake in Azerbaijan's stability. Oil exports and the construction of the Baku-Tbilisi-Ceyhan oil pipeline (completed in 2006) became important factors of Azerbaijan's foreign policy. High levels of investment and technological expertise were necessary to develop Azerbaijan's hydrocarbon resources.The decision was made to invite foreign companies into Azerbaijan. As Hoşbakt Yusufzade, a leading Azerbaijani oil expert and the author of the energy strategy of Azerbaijan, recalls:
"When the USSR collapsed, technology and money were needed to drill the Azerbaijani oil. We could either wait 30 years until we had the money and means or invite foreign companies. Aliyev chose the latter.
There were people who objected. They said "we can do it ourselves." But it was a risky and expensive work and having international partners was a very good idea." 
When Heydar Aliyev came to power, he halted the ongoing negotiations Elchibey had begun with western oil companies, which excluded Russia and Iran. The first thing Aliyev did was to grant a 10-percent share to Russia's Lukoil in a forthcoming contract.
Heydar Aliyev created a team to negotiate the terms of the contract with a British Petroleum-led consortium of international companies for development rights of the Azeri oil fields. Heydar Aliyev also sent his son, Ilham Aliyev, "to be his eyes and ears at the talks". The negotiation focused on the distribution of profits and the oil fields that would be included. By all accounts, the negotiations produced a good deal for Azerbaijan. The contract negotiated entails investment of 7.4 billion USD over 30 years in three offshore oil fields (Guneshli, Chiraq, Azeri). In addition, the consortium had to pay the Azerbaijani side a 300 million USD "signing bonus."
The consortium consisted of ten major companies from six countries. As oil expert Levine explains in his book on Caspian oil politics, the process of distributing shares of the fields to the consortium partners was "a strategy of building a diplomatic shield". The final contract, which was soon referred to by Azerbaijanis as "contract of the century", was signed on 20 September 1994 in Baku's Gulistan Palace. Looking back today, Yusufzade highlights its importance not only in terms of investment but also as a signal to other investors that Azerbaijan was stable enough for large-scale investments.
Having secured some gains in foreign policy and in oil diplomacy, Heydar Aliyev set out to consolidate his domestic power. In October 1994, Aliyev removed Suret Huseynov, the commander who had staged a rebellion against Elchibey in June 1993, from his position of Prime Minister on charges of treason. The same charge was put against the representatives of other "power ministries", i.e. the Ministers of Defense and the Interior. Huseynov fled to Russia.
Alikram Hummatov. Photo: qwiki.com
Aliyev's position remained contested. As Charles King, an expert on the South Caucasus, noted that in the first years of his presidency Aliyev's rule was challenged by regional clan strongholds and loyalists of different Russian, Turkish and Iranian networks. National separatism had resurfaced in the country's south populated by Talysh, an ethnic group, speaking an Iranian language. In June 1993, Talysh nationalist colonel Alikram Hummatov had proclaimed "Talysh Mughan Autonomous Republic" during an uprising in the city of Lenkaran in the south of Azerbaijan. While Aliyev had no army at his disposal at the time, he instead provoked a local rebellion against Hummatov, who ended up fleeing. As King described it,
"Unlike his many opponents, Aliyev stood at the center of a vast network of friends and colleagues from his days as Communist Party leader and, even more crucially, from his earlier career as head of the Baku branch of the KGB."
Other armed uprisings and assassination attempts against Aliyev followed, yet he was able to consolidate his grip on power after each one.
The pro-presidential New Azerbaijan Party (YAP) was established and a new constitution was adopted in 1995. The first parliamentary elections of independent Azerbaijan were held on 12 November 1995. The YAP party came in victorious.
 Thomas de Waal, The Caucasus: An Introduction (Oxford University Presss, 2010), p. 122.
 Thomas de Waal, The Caucasus: An Introduction (Oxford University Presss, 2010), pp. 123-124.
 Goltz, p. 371
 Abulfaz Elchibey, "Independence: A Second Attempt".
 Goltz, p. 446.
 Margelov, Michael , Russian interests….(also see Nuriyev article). Nasib Nassibli, "Azerbaijan: Oil and Politics in the Country's future," in Michael P. Croissant and Bulent Aras (ed.), Oil and Geopolitics in the Caspian Sea Region (Westport and London: Praeger, 1999), p. 107
 Angeliki Spatharou, The Political Role of Oil in Azerbaijan, 1989-1994, 33
 5 July 2010 Yusufzade presentation at ADA Summer School
 Steve Levine, The Oil and the Glory:The Pursuit of Empire and Fortune on the Caspian Sea, Steve LeVine, Random House.
 5 July 2010 Yusufzade presentation at ADA Summer School
 Charles King, p. 227 (Thomas Goltz provides a somewhat different take on this saying that Aliyev gave Huseynov powers that he clearly would fail to carry out so he also was seen by the public as for example responsible for continued losses on the Karabakh front., see p. 414, pp. 447-448).
 Described in detail by Goltz, p. 413.
 Charles King, The Ghost of Freedom, pg 226
 Two coup attempts involved Interior Ministry troops, and one of the attempts (1995) was linked to the Turkish deep state. This is explained by Thomas Goltz and confirmed by the Susurluk report, as well as by Ozdem Sanberk who flew to inform Heydar Aliyev of the plot. Charles King in his 2008 book The Ghost of Freedom also wrote of numerous coup attempts: "Actual or alleged coup d'etat became an almost annual occurrence – in 1994, 1995, 1996 and 1998 – but in the aftermath of each one, Aliyev was able to augment his power over both rivals and past associates" (p. 277).
Generation Facebook on trial
The country Heydar Aliyev built
Oil and Democracy under Ilham Aliyev