The deal will earn the 28-year-old around £10.5m per year following the expiration of his Bayern contract.
Alaba had been at Bayern since 2008 but turned down multiple offers to extend his contract, including a €15m-a-year deal, but talks ceased in November.
David Alaba is officially Real Madrid player.