I spanked my 2.5 year old for the first time

Please no critical comments as it already happened and I feel horrible. He woke up at 12:59 am screaming of anger (not crying) and hitting me out of nowhere. I’m trying to console him for 10 minutes and he was getting worse then he was screaming and pushing me for another 5 minutes for no reason and that’s when I spanked him. To be honest he cried for a good 3 minutes after but then he started to calm down. Once he calmed down I talked to him nicely that we don’t act this way in the middle of the night. He doesn’t speak in full sentences yet but he understands us most of the time. I looked up what that was all about and it could’ve been a nightmare or night terror but either way I felt I had to get him out of it with a spank. I don’t yell or spank ever and I’ve always talked to him nicely because he’s a baby and I never want him to hate me but for some reason I lost it at that moment. Anyone go through something similar? What could’ve been done next time?

Emily: He has woke up scared many times and it was nothing similar to this. This was a full on tantrum of anger, rage, and no way of consoling him no matter what we do