Sakshi Gupta
Sakshi Gupta

Reputation: 153

Executing AWS CLI using python

While Executing AWS CLI using python, Please find code for reference.

import awscli.clidriver
driver = awscli.clidriver.create_clidriver()
driver.main(['ec2','describe-instances','--instance-ids','i-12345678'])

Is it possible to store the output of driver.main in a variable ?

Upvotes: 2

Views: 1948

Answers (1)

Alex Harvey
Alex Harvey

Reputation: 15492

I don't think this is supported by the AWS CLI but you can do this:

import awscli.clidriver
from cStringIO import StringIO
import sys

driver = awscli.clidriver.create_clidriver()

old_stdout = sys.stdout
sys.stdout = mystdout = StringIO()

driver.main(['ec2','describe-instances','--instance-ids','i-12345678'])

sys.stdout = old_stdout

myvar = mystdout.getvalue()

Note that this is based on another Stack Overflow answer here.

Upvotes: 2

Related Questions