0

I am using Boto3 SSM to run commands in my instance.

When I run simple command such as echo hello world or mkdir abc it works fine and gives me expected output. but when I am trying to run python -V or any other python command. The output is python not found. But python is in my EC2 Instance which is running Ubuntu. When I manually check it works fine.

Code:

ssm = boto3.client('ssm' ) 
commands = ['python -V']

ssm.send_command(DocumentName="AWS-RunShellScript", Parameters={'commands': commands}, InstanceIds=['i-xxxxxxxxxxx'])
sjishan
  • 3,392
  • 9
  • 29
  • 53

3 Answers3

1

The correct answer to this question depends on the OS AMI installed.

For this question there are several options to solve this problem:

  • Use python3 because python (python 2.x) is not installed.
  • Install python 2.x (Ubuntu): sudo apt-get install python
  • Execute the command: sudo ln /usr/bin/python3 /usr/bin/python to link python to python3 in the /usr/bin directory.

Amazon Linux 2 has Python 2.7.14 installed. The commands "python", "python2" and "python2.7" are supported. Python 3 is not installed by default.

Ubuntu 16.04 64-bit has Python 3.5.2 installed. The commands "python3" and "python3.5" are supported. Note that there is no link to "python". Python 2 is not installed by default.

One item that I noticed with Amazon Linux 2 and Python 2.7. The command python -V writes the version string to stderr, which will make you think that the command failed as the SSM Run Command will display the output as follows when the command actually succeeded. I discovered that the SSM Run Command is just display the stderr after the stdout separated by ----------ERROR-------

----------ERROR-------
Python 2.7.14

Python 3 writes the version string to stdout. This took me hours to figure out as I never noticed the stderr issue with Python 2.x until today.

Information on the AMIs that I tested with.

Region: us-west-2 (Oregon)

Amazon Linux 2: amzn2-ami-hvm-2.0.20180810-x86_64-gp2 (ami-6cd6f714)

Ubuntu 16.04 LTS: ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20180814 (ami-51537029)

John Hanley
  • 74,467
  • 6
  • 95
  • 159
0

boto3.ssm is not mean to be used as your quick shell injection tools. You need to install ssm-agent to your linux instance to perform more advanced triggering, vice-versa to windows.

mootmoot
  • 12,845
  • 5
  • 47
  • 44
0

Using python3 worked. Another thing to mention, it uses the core python in the system and does not recognise any other version installed.

sjishan
  • 3,392
  • 9
  • 29
  • 53