Vatsal Rahul
Vatsal Rahul

Reputation: 367

How to run aws cli comands in python using subprocess module

I am trying to run a very basic AWS CLI command from python, instead of using boto3

So I found one answer from stack overflow I tried that but it didn't do any better, I don't want to use boto3, below is my code which I have tried

import subprocess

cmd='aws s3 ls'
push=subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE)
print push.returncode

If I run these commands into bash scripts it works perfectly fine. But I have a restriction that it has to be done using python scripts only.

Upvotes: 2

Views: 11663

Answers (2)

Venus713
Venus713

Reputation: 1591

Well, you can try following commands.

import subprocess

push=subprocess.call(['aws', 's3', 'ls', '--recursive', '--human-readable', '--summarize'])

or

import subprocess

push=subprocess.run(['aws', 's3', 'ls', '--recursive', '--human-readable', '--summarize'])

Wish help for you.

Upvotes: 0

arudzinska
arudzinska

Reputation: 3331

As the documentation for subprocess.Popen states, .returncode stores the child process return code. After running your code I am seeing this output:

ania@blabla:~$ python3 test.py 
None
ania@blabla:~$ 
[Errno 32] Broken pipe

Checking again the aforementioned docs:

A None value indicates that the process hasn’t terminated yet.

So let's modify your code to wait for the child process to end (I am using Python 3):

import subprocess

cmd='aws s3 ls'
push=subprocess.Popen(cmd, shell=True, stdout = subprocess.PIPE)
push.wait()   # the new line
print(push.returncode)

Now, in the output I get the exit status 0:

ania@blabla:~$ python3 test.py 
0

This is advice about an issue with the subprocess module, but what bothers me is why you don't want to use the boto3 module which is specifically written for sending API calls to AWS from Python. I discourage you from using subprocess to send these requests and switch to boto3, if possible.

Upvotes: 1

Related Questions