AWS

How to use Python Code in AWS EC2

Amazon Web Services (AWS) provides you with the services of using code in any language using the virtual machine. You need to create and connect to the AWS virtual machine, and once you are connected to the AWS virtual machine, you have to use a series of commands this is a very simple process through which your code can be deployed in AWS. This post will cover the process of using python code in AWS.

Let’s start with how to use python code in AWS EC2:

Use Python Code in AWS

To use a python code in AWS, you have to create an EC2 instance for that, click on the “Launch Instance” button:

You need to give the name of your instance and then select the machine image for the virtual machine you will be using:

Then scrolling down a little and from there you have to select the instance type and create a key pair for the connection purposes and then click on the “Launch Instance” button to create an EC2 instance:

After the creation of the EC2 instance simply select the instance and click on the “Connect” button:

On this page select the “SSH client” and copy the command mentioned in the screenshot below:

Simply paste the command in the Command prompt or PowerShell and then change the path for the key pair from your PC:

After the connection to the virtual machine, simply use the following command to check if Python is installed or not:

python3

This command will show you if python can be used or not:

Now use the following command to install the “pip” for python:

yum install python3-pip

This command will install the pip for python:

After the installation, let us get inside the pip to check which directories are available and for that, use the following command:

python3 -m pip

This command will show you what is inside the python-pip:

Install the HTTP server by using the following command:

yum install httpd -y

This command will install the Apache server:

After that, use the following commands to start the HTTP server, which will allow us to deploy our code:

systemctl start httpd

systemctl enable httpd

These commands will activate the HTTP server:

After these services are activated, you can deploy python code using the following command:

vim helloworlds.py

This command will create a python file in which you can enter the python code and then type “:wq!” to save the file:

After creating a python file, you can use the following command to check what is the location of our python file:

whereis python

This will tell us the location for our python file:

You have successfully used the python code in AWS:

Conclusion

To use a python code in AWS simply create an EC2 instance and then connect with the virtual machine. Once the connection is established with the virtual machine, you can use commands to install and activate different services required for using a python code. After all the services are in the “Active” state, use the commands to create a python file and then write a code on it. This post has taught you the process of how to use a python code to AWS.

About the author

Abdul Mannan

I am curious about technology and writing and exploring it is my passion. I am interested in learning new skills and improving my knowledge and I hold a bachelor's degree in computer science.