Skip to content

Instantly share code, notes, and snippets.

Forked from ottokruse/aws-console
Last active May 18, 2022 18:56
Show Gist options
  • Save davideicardi/48dfc3b664b4c39cd48708ea7f2899dd to your computer and use it in GitHub Desktop.
Save davideicardi/48dfc3b664b4c39cd48708ea7f2899dd to your computer and use it in GitHub Desktop.
Python script to launch the AWS console in your webbrowser, using a presigned URL generated from your AWS CLI credentials
#!/usr/bin/env python3
Based on:
- Save this script somewhere on your path (e.g. `vi /usr/local/bin/aws-console && chmod +x /usr/local/bin/aws-console`)
- Install dependencies: pip install boto3
- Make AWS credentials available in one of the usual places where boto3 can find them (~/.aws/credentials, env var, etc.)
- Excute the script: `AWS_PROFILE=your-profile aws-console`
- :tada: Your browser opens and you are signed in into the AWS console
import json
from urllib import parse, request
import webbrowser
import argparse
import boto3
def open_console(echo_to_stdout=False):
session = boto3.Session()
sts = boto3.client('sts')
federation_token = sts.get_federation_token(
'arn': 'arn:aws:iam::aws:policy/AdministratorAccess' # TODO limit your policy depending on your use case
url_credentials = dict(sessionId=federation_token["Credentials"]["AccessKeyId"],sessionKey=federation_token["Credentials"]["SecretAccessKey"], sessionToken=federation_token["Credentials"]["SessionToken"])
request_parameters = "?Action=getSigninToken"
request_parameters += f"&DurationSeconds={DURATION}"
request_parameters += "&Session=" + parse.quote_plus(json.dumps(url_credentials))
request_url = "" + request_parameters
with request.urlopen(request_url) as response:
if not response.status == 200:
raise Exception("Failed to get federation token")
signin_token = json.loads(
request_parameters = "?Action=login"
request_parameters += "&Destination=" + parse.quote_plus("")
request_parameters += "&SigninToken=" + signin_token["SigninToken"]
request_url = "" + request_parameters
if echo_to_stdout:
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Open the AWS console in your web browser, using your AWS CLI credentials')
parser.add_argument('--stdout', action='store_true', help='don\'t open the webbrowser, but echo the signin URL to stdout')
args = parser.parse_args()
Copy link

danbtl commented May 18, 2022

This is really helpful script, thanks for sharing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment