Both the ids are generated when UE makes radio connection with eNodeB. Once the RRC connection setup completes between UE and eNodeB i.e when UE moves from RRC-IDLE to RRC-CONNECTED state, eNodeB prepares and sends the "Initial UE message" to MME. Initial UE message carries the eNodeb UE S1AP ID identifier for an UE at the eNodeB. MME also generates an identifier for the UE named as "mme UE S1AP ID" and informs to eNodeB in Initial context setup request message.
As long as UE has radio connection with eNodeB i.e UE is in RRC-Connected mode, both the identifiers will exist and used to identify an UE over S1-C interface. When UE moves to RR-IDLE state or in other words when radio connection is released between UE and eNodeB, UE context is deleted at eNodeB. Afterwards, both the identifiers become invalid.