Skip to content

Instantly share code, notes, and snippets.

@shitana
Created December 10, 2023 10:30
Show Gist options
  • Save shitana/dc24aea55d0f6bc1227e182185231595 to your computer and use it in GitHub Desktop.
Save shitana/dc24aea55d0f6bc1227e182185231595 to your computer and use it in GitHub Desktop.
Get AWS Cred from SSO login
"""
Prerequistes
- AWS SSO login
- sso configure sso --profile <PROFILE_NAME>
- sso sso login --profile <PROFILE_NAME>
- SSO Cred json file cat .aws/sso/cache/d54a50b27ab3580ec5477511f9d40eb8f92b5649.json | jq
{
"startUrl": "https://d-90678c01fa.awsapps.com/start#",
"region": "us-east-1",
"accessToken": "aoaA..................SLRA",
"expiresAt": "2023-12-10T10:04:10Z",
"clientId": "urDhf0..............tMQ",
"clientSecret": "eyJra...................A5-Qat",
"registrationExpiresAt": "2024-03-05T15:12:28Z",
"refreshToken": "aorA...................OH5eFthkUCBBtQQ/1M"
}
"""
import json
import os
# Path to the AWS CLI cache directory
cli_cache_path = os.path.expanduser('~/.aws/cli/cache')
# Find the most recently modified file in the CLI cache directory
files = [os.path.join(cli_cache_path, f) for f in os.listdir(cli_cache_path)]
latest_file = max(files, key=os.path.getmtime)
# Extract the credentials from the file
with open(latest_file) as f:
data = json.load(f)
credentials = data['Credentials']
print(f"export AWS_ACCESS_KEY_ID={credentials['AccessKeyId']}")
print(f"export AWS_SECRET_ACCESS_KEY={credentials['SecretAccessKey']}")
print(f"export AWS_SESSION_TOKEN={credentials['SessionToken']}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment