Home > Blockchain >  Return results from awscli python
Return results from awscli python

Time:08-26

Hi I have a aws command which when run in the terminal outputs a list of datasets in AWS quicksight with their storage to a file:

aws quicksight list-data-sets --aws-account-id <foo-id> --region <foo-region> | jq " . | .DataSetSummaries[] | .DataSetId " | while read line; do echo "aws quicksight describe-data-set --aws-account-id <foo-id> --data-set-id ${line} | jq \". |.DataSet | {\"Name\": .Name , \"ConsumedSpiceCapacityInBytes\": .ConsumedSpiceCapacityInBytes}\" "; done > describe-data-set.sh

I then want to run this in python and store the results in a variable. I have imported awscli and have run a subprocess command but it just returns the command and the return status:

import awscli
import subprocess
push=subprocess.stdout(['aws', 'quicksight', 'list-data-sets', '--aws-account-id', '<foo-id>', '--region', '<foo-region>', '|', 'jq', '"', '.', '|', '.DataSetSummaries[]', '|', '.DataSetId', '"', '|', 'while', 'read', 'line;', 'do', 'echo', '"aws', 'quicksight', 'describe-data-set', '--aws-account-id', '<foo-id>', '--data-set-id', '${line}', '|', 'jq', '".', '|.DataSet', '|', '{"Name":', '.Name', ',', '"ConsumedSpiceCapacityInBytes":', '.ConsumedSpiceCapacityInBytes}"', '";', 'done'])

print(push)

CodePudding user response:

As John R pointed out in the comments, boto3 is the way to go if at all possible. It's an AWS supported wrapper for awscli operations in python. Show autocomplete hints...

If you're wondering what the heck a "ListDataSetsResponseTypeDef" is, you can access that (and all the other TypedDict defs you're sure to encounter) via from mypy_boto3_quicksight.type_defs import ListDataSetsResponseTypeDef.

I realize I haven't answered your question directly, but hopefully this helps. Good luck!

  • Related