How to configure commands to get executed automatically when AWS ec2 instance cpu utilization goes above certain percentage

The commands on the instance that has high CPU like taking java thread dump, checking processes running and upload to s3


You can use Cloudwatch to monitor the CPU and run command via SSM when the specific threshold is reached

Follow this document to set this up: https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/EC2_Run_Command.html