1

I am running a command in aws-cli to get the logs and i need to perform some opertaion in python with the output of this command.

I am doing something like this:


aws logs filter-log-events --log-group-name "something" --log-stream-names my-log-stream --filter-pattern "status=FAIL" | python3 -c "import sys, json; print(json.load(sys.stdin))" 

The above command gets the aws logs and runs the python code snippet to print the json returned.

I want to access the log-group-name and log-stream-name i.e "something" & "my-log-stream" (in this case) in my python code snippet. I am not sure how this can be done.

I have tried using sys.argv but it returns me the arguments passed in the python command i.e ['-c'] and not the aws command.

3
  • 1
    I'm afraid this can't be done because these are arguments to a different command, not Python. You could execute the whole command from within Python using the subprocess module Commented Sep 22, 2020 at 10:01
  • Actually that was the second option that i have thoudh of but i would have to make a lot of changes in my bash script. Just wanted to know if it was doable this way. Commented Sep 22, 2020 at 10:03
  • 1
    @Mohan: A general practice in programming is that, if you need one piece of information in two places, you bind it to a variable and use the variable in every place. It makes maintenance easier. In your case, you could use environment variables to hold the log group and the log stream, and use these variables in both your aws command and within Python. Commented Sep 22, 2020 at 13:44

1 Answer 1

3

That is not how pipe works. It just sends the output of the command to the next command. You could set the log-group-name and log-stream-name as environment variables

export LGN=something
...

and then access it in python with os.environ["LGN"] ... similar to this

export LGN="my-log-group"; <your code here> | python3 -c "import os; print(os.environ['LGN'])"
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.