Automate AWS Console Login
Are you also a person who works with multiple AWS Account and fed up of managing numerous AWS alias and respective username and password.
We will be using a small python code snippet which will automate your login process and open as many AWS account in a go, simultaneously.
The solution:
- We will use webbot(Web browser automation Tool) to perform the required automation. It works well with Google Chrome.
- Each Session is opened by webbot , it will have a completely different instance of browser for each login. It will enable us to open as many AWS login in one go without using any chrome plugin to distinguish session between them. Some people use a plugin like SessionBox to achieve multi-login functionality to the website.
- Config Management of Alias and respective username and password.
Config File for AWS Login Credentials
Let’s create a console.conf file in .aws directory on your local system. .aws is already being used by AWS CLI to store secrets. Content of console.conf file should look like
$ cat ~/.aws/console.conf
[aws-alias1]
username=user@gmail.com
password=#zh7&5nsh[aws-alias2]
username=user@gmail.com
password=#zh7&5nsh
The above content is following the ConfigParser module.
Requirement
- Python > 3.7
- webbot ≥ 0.33
- configparser ≥ 5.0.0
pip install configparser==5.0.0
pip install webbot==0.33
Python Code
Below code, the snippet is responsible for automation and taking alias name as argument.
#!/usr/bin/env python3from webbot import Browser
from configparser import ConfigParser
from os.path import expanduser
import sysaccount_name = sys.argv[1]#Consider using ConfigParser instead which checks types of the #values to be stored internally. If you don’t want interpolation, #you can use ConfigParser(interpolation=None).#Using this will not interpolate if you have special character in #your password, like '%'config = ConfigParser(interpolation=None)config.read("{}/.aws/console.conf".format(expanduser("~")))
if account_name in config:
username=config[account_name]['username']
password=config[account_name]['password']
print("Opening AWS Account {} using {}".format(account_name,username))
web=Browser()
web.go_to('https://{}.signin.aws.amazon.com/console'.format(account_name))
web.type('{}'.format(username) , into='Email')
web.type('{}'.format(password) , into='Password' , id='password')
web.click('Sign in')
web.click('NEXT' , tag='span')
else:
print("No Config for {}".format(account_name))
Save this file in ~/.aws directory as openaws.py and make it executable. Here interpolation=None being used with ConfigParser to turn off interpolation, so that it caters the special character in your password. Otherwise, it will land up in InterpolationSyntaxError
#Make file executable
chmod 774 openaws.py
#Verify the change
ls -ltr ~/.aws/openaws.py
Create an alias of this file so that it will be handy
$ alias openaws="~/.aws/openaws.py"
Now give a try ^_^
$ openaws aws-alias1
It will open up a new chrome suite, something like this