See e.g. https://en.m.wikipedia.org/wiki/Lagrange_multiplier and https://en.m.wikipedia.org/wiki/Karush%E2%80%93Kuhn%E2%80%93... for the justification.
It’s essentially an example of duality in numerical optimization (https://en.m.wikipedia.org/wiki/Duality_(optimization)#Dual_...).