There are a number of sources. If you are comfortable using differential forms, then one source that you may find useful is
L. Hsu, Calculus of variations via the Griffiths formalism, J. Differential Geom. 36, 551–589 (1992).
He explains how to do this calculation very explicitly there.
Another source is
R. Montgomery, A Tour of Subriemannian Geometries, Their Geodesics and Applications, AMS Mathematical Surveys and Monographs, Volume 91. 2002. MathSciNet review: MR1867362
You can get a pdf download from the American Mathematical Societ web page https://www.ams.org/books/surv/091/