It was possible that Sauron knew of her presence and saw her as an ally. At the end of the Age of the Trees, Sauron's master Morgoth allied himself with Shelob's ancestor Ungoliant to invade Valinor and kill the Two Trees. Her presence in the Tunnels also served to protect Mordor's western flank.