2

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 ?

Sakshi Gupta
  • 153
  • 8
  • Focus on questions about an actual problem you have faced. Include details about what you have tried and exactly what you are trying to do. – Nasser Ali Karimi Nov 27 '18 at 05:38
  • I am trying to execute aws cli using python, While executing it using above method its returning the output of describe instance on CLI, I am not able to store the retrieved output in variable, whereas I am able to store exit code of executed cli. – Sakshi Gupta Nov 27 '18 at 05:42

1 Answers1

2

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.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97