Carrera was commander of artillery in the Valley of Mexico at the time of the United States invasion. He fought in the battles of Padierna (20 August 1847), Molino del Rey (6 September), and Battle of Casa Mata (11 September). After the war he was military governor of the Federal District (1853–55).