Sunday, August 4, 2013

Manage AWS S3 buckets with Python - Part 1

There are many ways to manage your AWS account. For ease of administration, AWS provides a very decent web management console to administer most of the services. However, if you are a systems administrator then you must be looking for something which automates most of your admin tasks and makes your life easy.
There are various APIs released by AWS for most of the widely used programming languages. This article will focus on Python integration with AWS.
In this series of articles, I will walk you through most of the services that you can manage using Python.

Before we start, here is a list of pre-requisites:
1. You must have AWS account for practice. Don't worry, you can Free Web Tier for couple of months. Just don't exceed the limits. For more details, please visit AWS official website.
2. Basic understanding of Python. There are many free tutorials on YouTube, Pyton official website, etc.
3. Basic understanding of how AWS services work. Here is an online video tutorial covering most of the AWS services. It will give you good understanding of how stuff works in AWS. You can get it for ONLY $10

Let's get to work now.

There is a standard Python SDK for AWS, which is called boto. You need to install it on your system before you start working on any of the AWS services. Here is the command to install boto.
pip install boto
 Once boto installation will be completed, follow these steps to start working with AWS S3.

1. Create /etc/boto.cfg file and add AWS AccessKeyId and SecretAccessKeyId
2. If you don't want to add credentials in a file, then you have to explicitly define them in the python code (not recommended).

3. Connect with S3 and display existing buckets:
import boto
 Above code connects with your S3 account and display list of all the existing buckets in default region. The default region is us-east-1.

4. Now, lets create a new bucket. I will first check if the bucket already exists or not.
if bucket:
      print "Bucket %s exists" % bucket_name
 5. Finally, delete the bucket.
if bucket:
     print "Deleting bucket %s" % bucket_name
     print "Bucket does not exist"

No comments:

Post a Comment