Letter of recommendation from a professor with whom I took a class
Yes, it is appropriate to ask. There is never any issue about that. He may automatically remember you if you stood out as two years isn't that long. But you may need to remind him of the course and say some things about what you've been doing since. 

The best way to manage this is face to face. I mention this even though it may now be impossible with the pandemic, and may be hard in any case due to distance. But actually seeing you would help jog his memory. But perhaps you have photos online somewhere to help refresh his memory. 

You can also ask him whether he thinks a letter from him would be helpful or not. He might be able to say more than you think. The main reason for a letter is so that an independent person makes a prediction about your suitability for a program and your likelihood of success. Perhaps he can do that easily. If he has any hesitation, then you'd be better off asking someone else. 

I had a few standout students in my teaching career. It would be easy to write recommendations for them even though our interactions were brief. If you made an impact it can work out.