Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto Execute shell command on ec2 instance

I am newbie to EC2 and boto. I have an EC2 running instance and I want to execute a shell command like e.g. apt-get update through boto.

I searched a lot and found a solution using user_data in the run_instances command, but what if the instance is already launched?

I don't even know if it is possible. Any clue in this reference will be a great help.

like image 381
vibhor Avatar asked Mar 19 '13 14:03

vibhor


2 Answers

The easies way is to use kitten python utility which is just a wrapper around boto3.

e.g

kitten run "apt-get update" ubuntu 18.105.107.20
like image 89
Neo Avatar answered Sep 17 '22 20:09

Neo


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.

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.

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.

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 44
ExploringApple Avatar answered Sep 18 '22 20:09

ExploringApple