Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run a command remotely on ec2

I have and Ubuntu 14.04 ec2 instance with my scripts uploaded there. When I want to run a script remotely using my mac's terminal I do ssh myaws "python MyFolder/myscript.py" which runs perfectly. However, I want the script to run on ec2 even after I close the terminal window on my mac or switch off my mac. Once I figure that out I will move onto using cron to schedule my scripts to run on ec2 without me ever ssh-ing in. Is it possible and if it is how would I go about it? Let me now if you need more clarification. Thanks.

like image 224
Koba Avatar asked Oct 31 '22 18:10

Koba


1 Answers

Yes, you can do this with AWS Systems manager. AWS Systems Manager Run Command allows you to remotely and securely run set of commands on EC2 as well on-premise server. Below are high-level steps to achieve this.

  1. Attach Instance IAM role:

The ec2 instance must have IAM role with policy AmazonSSMFullAccess. This role enables the instance to communicate with the Systems Manager API.

  1. Install SSM Agent:

The EC2 instance must have SSM agent installed on it. The SSM Agent process the run command requests & configure the instance as per command.

  1. Execute command :

Example usage via AWS CLI:

Execute the following command to retrieve the services running on the instance. Replace Instance-ID with ec2 instance id.

aws ssm send-command --document-name "AWS-RunShellScript" --comment "listing services" --instance-ids "Instance-ID" --parameters commands="service --status-all" --region us-west-2 --output text

More detailed information: https://www.justdocloud.com/2018/04/01/run-commands-remotely-ec2-instances/

like image 107
ExploringApple Avatar answered Nov 09 '22 15:11

ExploringApple