If you need to run custom scripts/commands on EC2 instances and you want to do this without logging in with SSH you can do that by using AWS Systems Manager. Adding also Resource Groups to the mix you can execute the commands on a high number of instances simultaneously without having to do this instance by instance.
For the YAML document used in Systems Manager documents go to Github here: https://github.com/majestic-cloud/systems-manager-scripts/blob/main/install-apache.yaml
See the full demo here: