0

I am trying to use boto3 to run a python file in my windows ec2 instance. I am trying to use AWS-RunPowerShellScript to run the following commands which both work when I rdp into my ec2 instance.

The code I am using: DoStuffOnEc2.py

import boto3
resp = client.send_command(
        DocumentName="AWS-RunPowerShellScript", # One of AWS' preconfigured documents
        Parameters={'commands': ['md test1', 'py -3 make_test_dir.py']},
        InstanceIds=XXXXXXXXXX,
    )

make_test_dir.py

import os
os.system('md test2')

When I run DoStuffOnEc2.py, only the test1 directory is created. When I run md test1 and py -3 make_test_dir.py directly on Powershell while using an rdp, both test1 and test2 are created.

How would I go about resolving this. I generally use linux and have no experience with Powershell but I have to use it or CMD because the instance is Windows.

I also plan on moving this to AWS Lambda once it starts working but call a different python script. If there would be any issues with that, let me know.

1 Answer 1

1

I am guessing that when you login, py -3 ... works b/c python is in your PATH, but when SSM client is running it, it is not. You could try specifying the full path for both 'py' executable and for your python file to rule that out.

To locate py.exe and to troubleshoot, you could use get-command (gcm for short) like this:

(gcm py).source

that should print the full path of py.exe.

You can get fancier by auto-detecting the location of the py as above and invoke the output (string) using '&', for example:

& (gcm py.exe).source -3 c:/temp/make_test_dir.py

Note: it's OK to use '/' instead of '\'. Windows will be fine with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.