How to find out about 504 errors and the reasons?

It’s unlikely Cloudflare is the cause of this problem, the error you’re seeing is because it can’t connect to your AWS server upstream:

https://support.cloudflare.com/hc/en-us/articles/218378978-What-should-I-do-after-seeing-a-502-or-504-gateway-error-on-my-site-

Themes don’t have any significant performance impact AFAIK, but plugins can do, depending on which ones you have installed.