Skip to content

Instantly share code, notes, and snippets.

@somu4git
Forked from ottokruse/aws-console
Created May 24, 2022 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somu4git/b21c1110b7b040518c0d5435961d36e5 to your computer and use it in GitHub Desktop.
Save somu4git/b21c1110b7b040518c0d5435961d36e5 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
"""
Usage:
- Save this script somewhere on your path (e.g. `vi /usr/local/bin/aws-console && chmod +x /usr/local/bin/aws-console`)
- Make AWS credentials available in one of the usual places where boto3 can find them (~/.aws/credentials, env var, etc.)
- Excute the script: `aws-console --profile myprofile`
- :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(profile_name=None, echo_to_stdout=False):
creds = boto3.Session(profile_name=profile_name).get_credentials()
url_credentials = dict(sessionId=creds.access_key,sessionKey=creds.secret_key, sessionToken=creds.token)
request_parameters = "?Action=getSigninToken"
request_parameters += "&DurationSeconds=43200"
request_parameters += "&Session=" + parse.quote_plus(json.dumps(url_credentials))
request_url = "https://signin.aws.amazon.com/federation" + 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(response.read())
request_parameters = "?Action=login"
request_parameters += "&Destination=" + parse.quote_plus("https://console.aws.amazon.com/")
request_parameters += "&SigninToken=" + signin_token["SigninToken"]
request_parameters += "&Issuer=" + parse.quote_plus("https://example.com")
request_url = "https://signin.aws.amazon.com/federation" + request_parameters
if echo_to_stdout:
print(request_url)
else:
webbrowser.open(request_url)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Open the AWS console in your web browser, using your AWS CLI credentials')
parser.add_argument('--profile', default=None, help='the AWS profile to create the presigned URL with')
parser.add_argument('--stdout', action='store_true', help='don\'t open the webbrowser, but echo the signin URL to stdout')
args = parser.parse_args()
open_console(args.profile, args.stdout)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment