Jenkins agents in AWS EC2 instances with SSM crashing

I’m managing multiple Jenkins agents using the following setup: Each agent is an EC2 instance equipped with a Docker agent. A setup script is scheduled in the crontab to execute agent.jar, which then connects the agent with JNLP to the Jenkins controller. Finally, shell scripts are executed on the Jenkins controller. My EC2 instances don’t have public IP addresses and cannot use SSH, so I employ AWS Systems Manager (SSM) to configure the agents.

However, there’s an issue where the SSM agent occasionally crashes, causing the instance to return 504 or 503 errors and lose connection. In such cases, I have to restart or stop and start the instance. Any suggestions on how to prevent these crashes or disconnections? (The agents go offline, and the SSM checker usually remains at 1/2.)

Jenkins setup: Jenkins 2.387.1 Running on AWS EC2