The Gemara (Sanhedrin 74a) derives that one must give up his life to avoid idolatry from the passuk in Va'Eschanan (Devarim 6:5):
ואהבת את ה׳ אלקיך ... בכל נפשך
You should love HaShem your G-d ... with all your soul
One is enjoined to love HaShem - i.e. not to replace Him with another (Rashi) - to the point of giving up one's soul.
According to the way Rashi seems to understand the Gemara, the derasha seems flawed, though: if one serves under duress, he's not "replacing" HaShem. He still loves Him; he's just doing what he can to live. His allegiance is still to the True G-d. Should V'chai Bahem not still apply in this case?