CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

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

We have created EC2 Instance in the 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 the 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 the 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 the Python program.

Read this Article “AWS CLI : AWS Configuration and Connect to EC2 Instance” to know to configure AWS. This is a 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 the “boto3” library to access AWS Services or Resources. So, we must import the boto3 library into our program:

import boto3

Step 4. We are going to access, Ec2 resources from AWS. We need to create a session for this to connect to AWS resources. 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, for example: “S3”, “EC2” etc., So, our statement to get the resource service client is: This gives a list of available EC2 services.

ec2Instances = boto3.resource('ec2')

Step 5. Now we have a 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 the 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 instance, 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 issues.

You may see below the 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 the 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 the Authentication failure error. This will occur, when we pass incorrect 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

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

One thought on “AWS : Connect to EC2 Instance (Virtual Machine) – Python

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top