Saturday, December 16, 2017

Amazon AWS automation with python

I hate the fact that AWS GUI keeps changing (atleast now, may be not that much in future) so fast that you wonder where some stuff is which you used yesterday. It is time for me to get my hands dirty with aws via text, that is cli, python etc.,
you need the following though

  1. aws account (duhh...of course you should)
  2. python and pip installed and working
  3. aws cli
  4. boto3 (pip install boto3
    or if you are a not python aficionado or pythonista then you can choose your own diet her
Now do the following

aws configure

you will notice that it asks for a key. Go to AWS IAM and create a new user and do not use root user. give necessary permissions and rights to this user.
click on the user
security credentials tab
generate access key
download the csv file
use the details of the csv file when you run aws configure
leave the default region name empty
default output format, set this to json (yes I like json over others, blow me :p)
Note : you cannot recover a lost key. you can only generate a new one. delete the lost access key id and key pair and keep only the one which are in use and relevant.
Run the below now to list all the aws regions.

import boto3
client = boto3.client('ec2')
regions =[region['RegionName'] for region in client.describe_regions()['Regions']]
for region in regions:
    print (region)