The phrase "Sword of Damocles" alludes to the legend of Damocles, who was a servant to King Dionysius I of Syracuse. The king, weary of Damocles' obsequious flattery, invited him to a banquet and seated him under a sword hung by a single horse hair. This was done to point out to Damocles the precariousness of his position. His was a situation of impending death. He was in fear of the imminent danger that the sword caused. He learned a great lesson.

Also, it is believed that this idiom was first recorded in writing in 1747. Next, Damocles' story is used as a point of reference for the expression "hanging by a thread".

More Info: en.wikipedia.org