Alava served as a naval aide-de-camp during the time of Spain's alliance with France but switched sides in 1808 when Napoleon invaded Spain. The Spanish Cortes appointed him commissary (military attaché) at the British Army Headquarters, and the Duke of Wellington, who regarded him with great favour, made him one of his aides de camp. Before the close of the campaign he had risen to the rank of brigadier-general. Later he joined the headquarters of the British Peninsular Army as a military attaché and became a close friend of the Duke of Wellington. During the Waterloo Campaign in 1815, Alava was the Spanish ambassador to The Hague at the court of King William I of the Netherlands, which allowed him to attend the Duchess of Richmond's ball and to be at Wellington's side during the Battle of Waterloo.