For (my) simplicity, I will keep $8\pi G=1$. Taking inspiration from this paper, I think it's possible to show what you ask only if you include explicitly also the lapse function $N$ in the FRW metric, like this:
\begin{equation}\label{met}
ds^2 = - N^2\, dt^2 + a^2(t)\, \frac{dr^2}{1-k\, r^2}+ r^2\, d\Omega^2\, ,
\end{equation}
The associated Lagrangian would be
\begin{equation}\label{lagr}
\mathcal{L} = 3\, \left( N\, a\, k - \frac{a\, \dot{a}^2}{N} \right) + a^3 \left( \frac{\dot{\phi}^2}{2\, N} -N\, \mathcal{V}(\phi) \right)\, ,
\end{equation}
with generalized coordinates $\{a,\phi,N\}$. The canonical momentum associated to $N$ is $p_N=0$, which means that actually $N$ is not a dynamical degree of freedom, and it equals a generic constant: that is why usually one writes the FRW metric without $N$, because the case $N=1$ can be achieved by a simple, and always allowed, redefinition of the metric time $N\, dt \rightarrow dt$ (one can say that it's a gauge choice).
Now, what happens if you want to try and get anyway the dynamics of the non-dynamical degree of freedom $N$? You can use Euler-Lagrange equations with the Lagrangian above and see that the result is
\begin{equation}\label{fried}
\frac{\partial\, \mathcal{L}}{\partial\, N} - \frac{d}{dt}\, \frac{\partial\, \mathcal{L}}{\partial\, \dot{N}} = 0\quad \Rightarrow\quad 3\, \frac{1}{N^2}\frac{\dot{a}^2}{a^2} + 3\, \frac{k}{a^2} -\frac{1}{2\, N^2}\dot{\phi}^2 - V(\phi) = 0\, .
\end{equation}
This looks already similar to (the Hamiltonian that you found) $\, =0$, the difference being, of course, the presence of $N$. But, as we saw, $N$ is a constant and we still have the gauge freedom to redefine the time variable: so you can simply redefine the time derivatives in the equation above with $N\, dt \rightarrow dt$ (which amounts to the gauge choice $N=1$) and find in this way the usual Friedmann equation.
The point is that, in some cases, it is good to have $N$ explicit because it makes evident the presence of a constraint: the trajectories in the phase space of this model are constrained on the energy surface $\mathcal{H}=0$, and Friedmann equation is an expression of such energy balance.
Best Answer
This is almost certainly answered elsewhere, but the Hilbert Action, from which Einstein's equation can be derived, is:
$$S = \int d^{4}x\;\left(\sqrt{|g|}\frac{1}{16\pi G}R + \mathcal{L}_{m}\right)$$
taking the variation is pretty complicated (there are second derivatives of the metric in the action, and you have to deal with gauge invariance) and best looked up in a textbook, though. But note, that by this definition, we define $T_{ab} = \frac{\delta \mathcal{L_m}}{\delta g^{ab}}$