The wiki article on the Einstein-Hilbert action for General Relativity says that the stress-energy tensor $T_{\mu\nu}$ is related to the Lagrangian of matter, $\mathcal{L}_M$, by $$T_{\mu\nu}=-2\frac{\delta\mathcal{L}_M}{\delta g^{\mu\nu}}+g_{\mu\nu}\mathcal{L}_M.$$ In FRW cosmology, in the comoving frame, the stress-energy tensor $T^\mu{}_\nu$ for a perfect fluid is given by $$T^\mu{}_\nu={\rm diag}(-\rho,p,p,p)$$ with equation of state $$p=w\ \rho$$ and $$\rho \propto a^{-3(1+w)},$$ where $w$ is a constant.
What is the Lagrangian $\mathcal{L}_M$ that leads to the FRW perfect fluid stress-energy tensor $T_{\mu\nu}$?