
Usually 502 error happens when Cloudfront unable to reach origin endpoint. Some of the common reasons are:
Backend is busy: Origin endpoint ( ELB -> EC2) is busy or load is higher than given threshold. If this is happening reguarly, its better to get more instance in auto scaling group or increase existing instance for larger capacity.
WAF rule: Some request might get blocked by WAF rule. So check cloudFront logs to verfiy, if needed modify your application or disable WAF rule ( not recommended).
DNS: If Endpoint has custom DNS, make sure DNS is reachable.
EC2 instances: All EC2 instances in origin endpoint are down or unhealthy.