AWS : Connect to EC2 Instance (Virtual Machine) – Python

We have created EC2 Instance in previous Article “AWS : Creating a Virtual Machine with EC2“. And we have successfully connected to the EC2 Instance using SSH Client (PuTTY). In this Article, I am going to explain, how we connect to EC2 Instance through Python Program.

Step 1. We use Boto3 library to connect to AWS resources using Python. If you are not already installed, boto3, install it to execute this program.

Step 2. Do you remember, we have created a User through IAM Management console, in our previous article.? You must be noted down, the AccessKey Id & Secret access key generated by IAM. We need those details now, to configure AWS; to enable to connect using Python program.

Read this Article “AWS CLI : AWS Configuration and Connect to EC2 Instance” to know to configure AWS. This is Very Important step, before we continue to write our Python program.

Step 3. Once AWS is configured, start writing the Python program. As I mentioned before, we are going to use “boto3” library to access AWS Services or Resources. So, we must import boto3 library into our program:

import boto3

Step 4. We are going to access, Ec2 resource from AWS. We need to a create a session for this to connect to AWS resource. boto3 provides a function, named “resource” for us. “resource” function creates a resource service client by name. We need to pass the AWS resource name as an argument, example: “S3”, “EC2” etc.,. So, our statement to get the resource service client is: This gives list of available EC2 services.

ec2Instances = boto3.resource('ec2')

Step 5. Now we have list of EC2 services, return by boto3’s “resource” function. Now we will walk-through the list of EC2 instances, and display them which of those instances are available. We will display the Id of the instance by using its’ attribute.

for instance in ec2Instances:
    print(instance.id)

Step 6. Lets’ put all together & run the Python program.

# ec2_instances.py
import boto3

ec2Instances = boto3.resource('ec2')

for instance in ec2Instances.instances.all():
    print(instance.id)

To run the program, type below command at the command prompt:

python ec2_instances.py

And you will see the list of EC2 Instances which are available are displayed on the console window. As we have created only one EC2 instances, this programs displays one entry.

i-0xx1xxxx7xxxxxxxx

Are you seeing some issues while running the program.? Below are the common mistakes most of us do: let’s fix those issue.

You may see below Error, due to the missing credentials. This is mainly, due to the missing AWS configuration. You must run the AWS configuration; with proper details, to fix this issue.

  File "/usr/local/lib/python2.7/site-packages/botocore/auth.py", line 352, in add_auth
    raise NoCredentialsError
botocore.exceptions.NoCredentialsError: Unable to locate credentials

You may see below error if you missed to mentioned the region name while doing the AWS configuration. The solution is, run the AWS configuration and provide the right region name.

  File "/usr/local/lib/python2.7/site-packages/botocore/regions.py", line 135, in _endpoint_for_partition
    raise NoRegionError()
botocore.exceptions.NoRegionError: You must specify a region.

Another common error most of us see is, Authentication failure error. This is by passing in correct AccessKey Id OR Secret access key while doing the AWS configuration. To fix this, again, run the AWS configuration and provide the proper details.

  File "/usr/local/lib/python2.7/site-packages/botocore/client.py", line 612, in _make_api_call
    raise error_class(parsed_response, operation_name)
botocore.exceptions.ClientError: An error occurred (AuthFailure) when calling the DescribeInstances operation: AWS was not able to validate the provided access credentials

I have explained clearly, how to do the AWS configuration in my previous Article “AWS CLI : AWS Configuration and Connect to EC2 Instance“.

We will go through more programs through my coming Articles.

[..] David

Add a Comment

Your email address will not be published.