At 120 years old, Moses begins to worry about his legacy.
Moses prepares to die - In a man's waning years, he becomes worried, perhaps even paranoid, that all of the things that he worked for in this life will go to hell in a handbasket. This is the image I get of the author of Deuteronomy, Chapter 31.
Moses, whom aside from the Bible there appears to be no evidence of being a real person, symbolizes the worries of the ancients who wrote collaborated in the writing of Deuteronomy. They wrote it during a time of competition with other religions. They wrote it as their new Yahwist religion was just getting afoot. The religion had a strong base among the Israelite people, but many people still worshiped the gods of old Sumeria. And so, as the founders of the religion began to get old, they worried. And, they used Moses as the mouthpiece of this worry.
As his strength and vision was leaving him, Moses said he is no longer able to move around. But he told the Israelite people he had a vision, and Yahweh told him that they will conquer the nations around them with the help of Joshua. He brought Joshua before them and told him of the vision.
Yahweh had also wrote a song for Moses. That was rather kind for an all-knowing, all-powerful deity to do, wasn't it? The song further reflected the worries of these old men. Apparently, to set up the need for the song, Yahweh told Moses that after he dies, his people will begin to worship the other gods in droves; and that He (Yahweh) will no longer bless them but curse them.
So, He made a song to teach the Israelites so that they will be ever reminded of Yahweh's love of the Israelites, what He did for them, and what they must do for Him in return.
So, in a nutshell. Whatever this song was (it was unclear in the chapter what the song was), it was made because the Levites (or priesthood) were worried that people were on the verge of losing their religion, and taking on other ones.
What I want to know is if Yahweh was so prevalent back then, showing himself as a great pillar of fire and smoke upon the Tabernacle, making these great shows of display, destroying all their enemies, and even destroying a few Israelites in the process - why is it that so many Israelites had so much trouble believing in Yahweh? Why was it such a prevalent problem that people were converting to other religions?