The eikonal equation for inhomogeneous anisotropic metamaterials with equal relative permittivity and permeability tensors (ε(r) =μ(r)) is derived from a free boundary variational principle. An original approach is proposed considering the wavefront as a moving discontinuity surface in an extended continuous media described by the Lagrangian density of electromagnetic fields. The eikonal equation arises as natural (non prescribed) boundary conditions for variational problems.