Yesodei HaTorah 5:5
If a gang of non-Jews tells a group of Jewish women, “Give us one of you to violate, otherwise we will violate all of you,” they should allow themselves to be violated rather than hand over one of them. Similarly, if they told a group of Jews, “Give us one of you to kill, otherwise we will kill all of you,” the group should allow themselves to be killed rather than hand over one of them.
However, if the attackers request a specific person, saying “Give us this individual or we will kill all of you,” then if the person is liable to the death penalty like Sheva ben Bichri (in II Samuel chapter 20), then they may hand him over. If he is not liable to the death penalty, they should allow themselves to be killed rather than hand someone over.
Yesodei HaTorah 5:6
The same principles that apply to being coerced also apply to illness. If a person is so sick that he is in danger of dying, if the doctors say that violating a Torah law as part of the remedy can save him, their advice should be followed. When a life is at stake, one may violate any Torah prohibition as part of the cure, except for idolatry, forbidden sexual relations and murder; these may not be violated to save a life. If a person violated them as part of a cure, he may be liable for the usual court-imposed penalty.