Thursday, July 30, 2015

Getting Started with Boto and AWS

In this article, I’ll demonstrate the simplest way to managed AWS using Boto – a Python SDK for AWS. It is very necessary to have some kind of scripting in order to manage infrastructure on AWS. Boto is the best SDK for Systems Administrators. You can use packages and libraries of Python to take additional advantages. Boto supports most of the frequently used AWS services.
First thing you need to do is to configure Boto Config file for AWS Access keys and Secret keys, otherwise you’ll have to provide these keys every time you try to connect with AWS.
If you are using Linux, then you can configure Boto Config file at any of the below locations.
1. At /etc/boto.cfg – For site-wide or system wide settings
2. At ~/.boto ( ~ means home directory) – For user-specific settings
In Windows OS, you can place boto.config file user home directory of your Windows account.
A sample Boto config file looks like this:
[Boto]
ec2_region_name = us-east-1
ec2_region_endpoint = ec2.us-east-1.amazonaws.com
[Credentials]
aws_access_key_id=YOUR AWS ACCESS KEY
aws_secret_access_key=YOUR AWS SECRET ACCESS KEY
Now we can start working with Boto and get connected with AWS
1. Establishing a connection with AWS (Multiple ways to do it)
It can be done in more than one way. There is no impact of using one or the other method. Only thing to consider is what you are importing in Python. If you import complete Boto module, it means it needs more resources since every component Boto supports will be imported in Python. The other way is to import only specific module, e.g. only EC2 module if you are working with only EC2 instances.
a. Import Boto in Python (Importing all modules)
>>> import boto
>>> boto.Version
‘2.38.0’
b. Connect with AWS default region (us-east-1)
>>> ec2_conn=boto.connect_ec2()
>>> print ec2_conn
EC2Connection:ec2.us-east-1.amazonaws.com
c. Importing only EC2 module in Boto (Specific module only)
>>> from boto.ec2 import EC2Connection
>>> conn=EC2Connection()
>>> print conn
EC2Connection:ec2.us-east-1.amazonaws.com
As you can see, there is no change in the end result using both ways. However, it is preferred to import only specific module if you are going to work only on one particular AWS service.
2. List all servers in your account.
Now we’ll talk about listing all the servers configured in your account along with state. By default, there is no direct way to get list of all the servers. Although there is a function called ‘get_all_instances’ but it does not provide information about all the configured instances. Instead it provides reservation ids of all servers which can further help in getting instances ids and health state.
Let’s see how it works.
a. Get reservation ids of all servers.
>>> res=conn.get_all_instances()
>>> print res
[Reservation:r-0ad52ce3]
Since I have only one server running, it returned only one reservation id.
b. Get instance ID and instance state manually. Just to understand how it works.
>>> res[0].instances[0].id
u’i-98d45571′
>>> res[0].instances[0].state
u’running’
c. As you can see, returned reservation output is an array in itself, which further contains sub-array that contains information of instances.
d. Let’s automate it so that instance information can be obtained for all the instances.
>>> for r in res:
for i in r.instances:
print r.id, i.id, i.state
r-0ad52ce3 i-98d45571 running
The last line is the output which provides information on reservation ID, instance ID and instance state. If you have multiple instances configured, then all the instances will be listed in the output.

28 comments:

  1. Your good knowledge and kindness in playing with all the pieces were
    very useful. I don’t know what I would have done if I had not
    encountered such a step like this.


    AWS Training in Chennai


    AWS Training in Bangalore


    AWS Training in Bangalore

    ReplyDelete
    Replies
    1. IEEE Final Year projects Project Center in Chennai are consistently sought after. Final Year Students Projects take a shot at them to improve their aptitudes. Final Year Project Domains for IT

      JavaScript Training in Chennai

      JavaScript Training in Chennai

      The Angular Training covers a wide range of topics including Components, project projects for cse. Angular Training

      Delete
  2. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in.
    Android Training in Chennai
    Android Training Institute in Chennai
    Android Classes in Chennai
    App Development Course in Chennai
    Best Android Training institute in Chennai

    ReplyDelete
  3. I am really enjoying reading your well written articles.
    It looks like you spend a lot of effort and time on your blog.
    I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..
    Java Training in Bangalore
    Best Java Training Institutes in Bangalore
    Java Course in Bangalore
    Java Training Institutes in Bangalore
    hadoop course in bangalore
    best hadoop training institutes in bangalore
    best big data training in bangalore

    ReplyDelete
  4. I have really enjoyed sharing your website. thank you so much for your sharing this document. this document more useful and improve our knowledge.
    AWS training in chennai | AWS training in anna nagar | AWS training in omr | AWS training in porur | AWS training in tambaram | AWS training in velachery

    ReplyDelete
  5. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in.
    angular js training in chennai

    angular js training in omr

    full stack training in chennai

    full stack training in omr

    php training in chennai

    php training in omr

    photoshop training in chennai

    photoshop training in omr

    ReplyDelete
  6. Nice Topics.. Read My Review Of An Online Cfd Trading Broker,GATE IOWith A Focus On Their Platform And Customer Support Services. I Have Been Using Them For Over A Year And This Is My Honest, Unbiased Review.

    ReplyDelete
  7. NoteZilla 9.0.27 Crack is a great option. The application works like a notepad for Windows and mobile phones, sometimes people can use it .Notezilla Free Download Full Version

    ReplyDelete
  8. Free Download Adobe Master Collection 2022 v11 (29 Aug 2022) for Win & macOS Multilingual with CRACK & Keygen..Adobe Master Collection Price

    ReplyDelete
  9. May this Christmas season bring you closer to all those that you treasure in your heart. Have a Merry Christmas and a Happy New year! Best wishes for a joyous .Inspirational Christmas Messages

    ReplyDelete