How to use the AWS Python SDK while connecting via SSO credentials
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
To use the AWS Python SDK (boto3) with SSO credentials, configure an SSO profile in ~/.aws/config, run aws sso login --profile your-profile to authenticate, then create a boto3 session with boto3.Session(profile_name='your-profile'). Boto3 automatically reads the cached SSO token and exchanges it for temporary AWS credentials. This avoids storing long-lived access keys and integrates with your organization's identity provider (Okta, Azure AD, etc.) through AWS IAM Identity Center (formerly AWS SSO).
Step 1: Configure the SSO Profile
For the legacy (pre-session) format:
Step 2: Log In via CLI
Step 3: Use boto3 with the SSO Profile
Using Environment Variables
Multiple Accounts and Roles
Handling Token Expiration
Programmatic SSO Token Refresh
Common Pitfalls
- Forgetting to run
aws sso loginbefore using boto3: SSO credentials require an active login session. Without it, boto3 raisesUnauthorizedSSOTokenErrororTokenRetrievalError. Runaws sso login --profile your-profileand complete the browser authentication before running Python scripts. - SSO token expiration during long-running scripts: SSO tokens typically expire after 1-8 hours (configurable by the admin). Long-running scripts fail midway when the token expires. Wrap API calls in try-except for token errors and re-authenticate, or use
sso_registration_scopeswith longer session durations. - Using
boto3.client()instead ofboto3.Session().client():boto3.client('s3')uses the default session, which may not pick up your SSO profile. Always create an explicit session:session = boto3.Session(profile_name='dev-admin')thensession.client('s3'). - Mixing SSO config formats (legacy vs session): The legacy format (all SSO fields in the profile) and the session format (
sso_sessionreference) are not interchangeable. Mixing them in the same profile causesInvalidConfigError. Use one format consistently — the session format is recommended for token portability. - Running in CI/CD without interactive login: SSO login requires a browser. In CI/CD environments, use IAM roles (instance profiles, OIDC federation) instead of SSO, or use
aws sso loginwith device authorization in a pre-step that caches the token for subsequent jobs.
Summary
- Configure an SSO profile in
~/.aws/configwithsso_start_url,sso_account_id, andsso_role_name - Run
aws sso login --profile nameto authenticate, then useboto3.Session(profile_name='name') - Handle token expiration with try-except and re-authentication for long-running scripts
- Use multiple profiles in
~/.aws/configto switch between accounts and roles - Set
AWS_PROFILEenvironment variable to avoid specifyingprofile_namein every session
Related reading
- How to use the code returned from Cognito to get AWS credentials?
- How to verify accessToken in node/express using aws-amplify?
- How to view all the services running on AWS?
- how to view aws log real time like tail -f
- How to use the old value and the new value of a Variable in Tensorflow?
- How to use the timeit module?
- How to wait for async actions inside AWS Lambda?
- How to workaround custom domain for private API gateway?
System design interview problems on this topic
- Design Dropbox: solution and walkthrough
- Design a Global Content Distribution Network: solution and walkthrough
- Design a Serverless Architecture Framework: solution and walkthrough
- Design Youtube or Netflix: solution and walkthrough

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseSystem Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.