Immunity and impunity are used in different situations. One can differentiate them based on legal usage, however in common usage the line between the words is often blurred. A doctor would not say that a virus acted with impunity, for a variety of reasons, the least of which is that viruses aren't typically thought of as living beings with a will to act on their own. In the medical sense, immunity refers to the ability of an organism to resist diseases or changes to it's integrity because of an outside agent.
Let's examine immunity in it's other sense. Let's say that prosecutors have captured a leader in a crime syndicate. Prosecutors know that this leader has a lot of information that can help them capture other criminals but they know they defense will be worried that their defendant will also have to admit to committing crimes. The prosectors will offer the defendant immunity from prosecution so that he will freely talk. So, during a trial, he can freely admit to committing crimes without worrying that he will be punished. In this case, he is not offered impunity and he is not acting with impunity, although if you just go by the dictionary definition, you might think "why not use impunity?" Immunity also has a sense of exemption from duties. For example, a king is immune to income tax, or a diplomat is immune to local laws.
Let's take a look at impunity. A feudal prince has a powerful and wealthy father. He knows that he can wander about the countryside with his henchmen, and demand money, property, favors, and other improper things not accorded to him by law or custom. He acts with impunity toward his subjects. Impunity is usually used for these kinds of situations. A person does something knowing that the likelihood of consequences is small. Authority figures are people that act with impunity. In my opinion, it has a negative connotation. In current debate about policing practices in the US, impunity is a word that could be used, "Police acted with impunity." In my opinion, this has a negative connotation.
In the above scenario with the prince, it is possible to use the word "immune" because the word has drifted from it's origins and now has a broader sense of resistance. So one could describe the above scenario by saying "The prince acted as if he was immune to prosecution." It's is my opinion that when using immune in this way, one should say what one is immune to, i.e. prosecution. One could also say "Police forces acted as if they were immune to prosecution." To me, it doesn't quite have the negative feel that impunity does. Other users may disagree on this.
It's worth noting that the word impunity is built up of the prefix im and the root pun or impunis in Latin which means "free of punishment." For etymology of Immunity see this link. This link says that the use of immune in a medical sense was first recorded in 1879, however a google ngram search shows that the word was used in a medical sense earlier than that. I am not an immunologist or medical historian so perhaps my interpretation of the ngram results is incorrect. I looked up immunity in ngram to get a sense of how the word was used before the widespread use of it in a medical sense, however I could not find anything that was helpful to my understanding.