This question is exactly what I would like to ask. I know this is a duplicate, but I cannot comment and I do not find answers to that question.
So, let me state again the problem. Let us consider Classical Electrodynamics, which is described by Maxwell's Equations. Then we have Geometrical Optics, which is based on Fermat's Principle. Knowing that light is an e.m. field, one would be happy to derive Fermat's Principle from Maxwell's Equations. This is done in many treatises on the subject, but in the limiting case of very short wavelength AND very slow variation of the index of refraction in the medium in which the phenomenon takes place. Namely, one derives an approximate wave equation by neglecting variations of the index of refraction, and from there obtains the Eikonal Equation by a perturbative expansion in terms of the wavelength. Lastly, the Eikonal Equation leads to Fermat's Principle.
Now, the conclusions of Geometrical Optics (limited to the path of light rays) are exact in the case of a sharp flat boundary between two homogeneous media. I mean, Maxwell's Equations (and the corresponding continuity conditions) imply that a plane wave in this scenario follow a path described by Snell's law, and Geometrical Optics describes exactly the same path. So it seems that Fermat's Principle is correct even when there is a discontinuity in the index of refraction. But in this case we cannot neglect the variations of the latter when deriving the Eikonal Equation. So our derivation of Fermat's Principle from Maxwell's Equations is not valid.
In conclusion: Maxwell's Equations are valid from the outset; Fermat's Principle is valid for very short wavelengths, but for any variation of the refractive index; but we do not have a link from Maxwell to Fermat which is valid whatever be the refractive index. Do we miss something?