The author/s had several purposes in writing "The Epic of Gilgamesh." One was to express the power of friendship, as Gilgamesh is changed from a brute to a caring leader by his friendship with Enkidu. Similarly, Enkidu learns to become less wild from befriending Gilgamesh. The other moral is that mortals do not have the luxury of everlasting life. While Gilgamesh tries to find the secret to living forever by visiting Utnapushtim, Gilgamesh loses the plant that is supposed to confer everlasting life. Resigning himself to being mortal, Gilgamesh instead turns to being a great king and to achieving what he can on earth, precisely because he knows he is not immortal and that his time on earth is limited. The audience for these lessons were people in ancient Mesopotamia (the later epic was composed around 1300 to 1000 BCE by Sîn-lēqi-unninni, a priest).
Gilgamesh does not have a known author, but it is the earliest recorded human epic. Only pieces of the story survive and have been cobbled together to form the story of the warrior and Enkindu (the first "sidekick" in recorded history). The intended audience is unknown, but it seems likely that it was an oral tale heard by people of all socio-economic and political backgrounds.
The writer or writers of the epic seems to have several purposes in recounting the tale. It is instructive in explaining events in history: like the Bible, Gilgamesh explains how the world was created (a very interesting comparison can be made between the epic and Genesis 1-3).
Like The Odyssey and Beowulf, texts that come far later, the heroes of each epic battle faces incredible foes (Gilgamesh, Humbaba; Odysseus, the Cyclops; Beowulf, Grendel). The tales of super-human feats seem to be designed to keep the audience in suspense. Each has a hero that is larger-than-life.
There is also an implicit moral in Gilgamesh, a warning about the lust for immortality that will go unsatisfied. Gilgamesh, and those who read of the hero's exploits, must come to terms with their limited selves, strive for acceptance, and hope for remembrance by future generations.