In this article, I’ll try to cover some of the most widely used EC2 parameters to be configured using Boto. Undoubtedly, AWS provides a web console which is very user friendly and supports almost all the features. But having Boto in your armour is a real edge. Being a system or AWS administrator, you won’t rely on the console very much. Instead, it will be all scripting and scripting. That’s where Boto comes into picture.
Ok, so let’s get started. First things first, let’s launch a new EC2 instance using Boto.
In order to create and managed EC2 instance, you’ll need following minimum information with you
- AMI ID (Image ID which will be used to launch a new instance)
- Security Group (Security group name which the new instance will be part of)
- Key Pair (Key pair name which will be used to access the instance)
- Region (Leave blank for default region)
- Instance Type (There are many types to select from, like t1.micro, m1.small, m1.medium, etc.)
Let’s create a new instance using Boto by providing above details:
1. Create a connection between Boto and EC2
2. Create new instance
3. The output returns the reservation id for that particular instance.
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.instances.id u’i-98d45571′ >>> res.instances.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.