なり means "the moment", "as soon as": someone does one thing and straight away does another, usually something you would not expect. It follows the dictionary form of a verb, even when the whole sentence is in the past.
After a た-form, なり is a different pattern meaning "and stayed that way" (でかけた出かけたなりかえってこない帰ってこない, "went out and never came back").
Both halves have the same subject, and the second half is what that person did. It reports something that happened, so it is in the past, and it can't be a request, an order or a plan.
なり is mostly used about other people. For your own actions, たとたん or とすぐ sounds more natural.
All three mean "the moment". たとたん takes the た-form, is the everyday choice, and usually has something happen to the subject that they didn't intend, with a different subject in the second half if needed. やいなや takes the dictionary form like なり and is the most literary of the three; the subjects can differ.
なり belongs to written narrative: novels, essays, news stories. In conversation, たとたん is the usual choice.