How to check if AWS CLI SSO is logged in
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
With AWS CLI SSO, a user is not really "logged in" just because a cache directory exists. The useful question is whether the configured profile can still obtain valid credentials and successfully call AWS APIs.
Use a Real AWS Call as the Check
The most reliable check is to run a harmless command that requires credentials. sts get-caller-identity is ideal because it does not modify resources and works in almost every account.
If the SSO session is still valid, the command returns the caller ARN, account ID, and user or role identity. If the session is expired, missing, or misconfigured, the CLI exits with an error. That makes it better than inspecting local cache files.
Check Exit Status in Scripts
For shell scripts, the exit code matters more than the printed JSON. Redirect the output away and branch on success or failure.
This is the simplest way to gate a deployment script, local helper, or preflight check.
Distinguish Authentication from Configuration
An SSO profile can fail even when the cached session exists, because the profile itself may be wrong. Before assuming the user needs to log in again, inspect the configured profile values.
If sso_start_url, sso_region, account ID, or role name do not match the intended access path, the identity command will fail for configuration reasons rather than for session-expiration reasons.
Trigger Login Only in Interactive Flows
When you are working locally, it can be reasonable to run aws sso login after a failed check. In a non-interactive environment, that pattern is usually wrong because browser-based login is not appropriate for automation.
Use this pattern for developer tooling, not for headless CI. For automation, prefer IAM roles, workload identity, or another non-interactive credential source.
Cache Inspection Is Only a Debug Aid
You can inspect the SSO cache if you are debugging odd behavior.
That can tell you whether cached entries exist and whether they were refreshed recently, but it does not prove the target profile is usable right now. Cache files can be stale, unrelated to the profile you care about, or present even when an API call still fails.
Build a Reusable Helper
If this check is part of your daily workflow, wrap it in a small shell function. Keeping the logic in one place avoids subtle differences between projects.
This helper works well in local scripts because it exposes a clean boolean contract through the shell exit code.
Common Pitfalls
The most common mistake is treating the presence of files in ~/.aws/sso/cache as proof of a valid session. That directory is useful for debugging, but it is not the source of truth.
Another problem is mixing AWS_PROFILE and --profile inconsistently. If one part of a script uses the environment variable and another uses a flag, you can easily test one profile while assuming you are testing another.
It is also easy to forget that a failing profile may be misconfigured rather than expired. Re-running login repeatedly will not fix a wrong account ID or role name.
Summary
- Use
aws sts get-caller-identity --profile ...as the actual SSO readiness check. - In scripts, rely on the command exit status rather than parsing cache files.
- Treat
aws sso loginas an interactive recovery step, not a CI pattern. - Verify the SSO profile configuration before assuming the session is expired.
- Use cache inspection only for troubleshooting, not for the main decision.
Related reading
- How to check if pod security policy is enabled?
- How to check if Python app is running within AWS lambda function?
- How to check if specific resource already exists in CloudFormation script
- How to check whether my user data passing to EC2 instance is working
- how to check whether RBAC is enabled, using kubectl
- How to clear out session on log out
- How to choose an AWS profile when using boto3 to connect to CloudFront
- How to choose an AWS profile when using boto3 to connect to CloudFront

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.