I’m using aws sso login, but I can’t found out how to discover if I’m already logged in or if I need to login again, the only way I found to do that is to run a command I know I have permission and check that no errors happen.
aws sso logout
aws sqs list-queues # error
aws sso login # brower accept
aws sqs list-queues # success
My goal with that is to automate some scripts and only ask for login if needed.
2
Answers
You can check for the sts caller identity call
https://docs.aws.amazon.com/cli/latest/reference/sts/get-caller-identity.html
If the session is still valid, it will return
If the session is not valid, it will return
Or you can use this utility which is designed for this purpose
https://github.com/benkehoe/aws-sso-util
I ran into the same issue recently, but opted for a solution using exit codes: