Preheat oven to 350 degrees. Butter a baking dish.
In a saucepan, melt 2 tablespoons butter over medium high heat. Stir in flour and cook for 1 minute. Remove saucepan from heat and whisk in milk. Return pan to heat and bring to a simmer while stirring. When sauce has thickened remove from heat, season with salt and pepper and set aside.
In a skillet, cook onions in melted butter until golden brown. Season with salt and pepper.
Spread 1/3 of the white sauce in bottom of baking dish and top with half of the potatoes. Spread out half of the onions, ham, cheese and another third of the sauce. Season with salt and pepper. Continue layering ingredients, ending with the remaining cheese on top. Bake for 45 minutes or until golden and bubbly.