After a crime has happened, Apple can be compelled to produce information that is useful to solve the crime, and if writing software was necessary to do so, they would have to write that software. I'm not aware whether Apple would have to be compensated to do so. I also don't know what would happen if Apple's software engineers who are capable of writing that software would refuse to do so. I don't think the state can force an employee, and I don't think the state can force Apple to put pressure onto such an employee.
However, one of Apple's design goals for iPhones is that no hacker should be able to extract data from a customer's iPhone. And Apple has quite reasonably decided that in order to achieve this goal, Apple itself should be completely incapable of extracting data from a customer's iPhone. And because of that, Apple is not capable of writing software to extract data from a criminal's phone. Since Apple is not capable of writing such software, Apple cannot be compelled.
What the government wants is to create software today so that Apple can break into future iPhones. That would be quite possible. But since no crimes have been committed (yet) using future iPhones, Apple cannot be compelled to do so either.
Practically, the NSA has twice had attack software stolen from them that caused immense problems for example to the UK health service. And compared to the NSA, the FBI are amateurs. So there is no way that Apple could produce such software, give it to the FBI, and it doesn't end up in the hand of criminals who use it to rob ordinary citizens, or in the end of state actors who would use it to rob confidential information from politicians, law enforcement, military, important companies etc.