How to Upload a Python File to an Ec2 Instance
Running Python scripts on an AWS EC2 Example
And then you have an EC2 case upward and running on AWS (If yous don't have it already, take a look at this post: https://medium.com/@praneeth.jm/launching-and-connecting-to-an-aws-ec2-case-6678f660bbe6).
At present let's meet how we can setup a python environment, transfer python scripts from your local machine to the remote example and run them.
To see everything that's installed in your instance, type in the following commands:
cd /usr/bin/
ls
- You will probably see python two.7 already installed which is the older version, so let's go ahead and install python 3.half dozen
sudo yum install python36
Even subsequently installing python 3.6, running python --version
in the Putty terminal still points to python 2.7, let'southward modify that.
alternatives --ready python /usr/bin/python3.6 python --version
What we did at that place is, change the default python to the newer version, it should at present point to python iii.half dozen.
2. Side by side we want to install pip3 that we will use to install python packages.
cd /tmp
curlicue -O https://bootstrap.pypa.io/go-pip.py
python3 get-pip.py --user
pip3 --version
Now we can utilize pip3 to install python packages. If you want to install the selenium package, run the following in your Putty last.
pip3 install selenium --user
Allow'southward transfer a python script from our local car onto the EC2 instance and try running information technology.
- First open a windows terminal on your auto using cmd.
- Use scp (Secure Copy Protocol) to transfer files between the local and the remote host. The scp command has iii arguments, the first statement is the file path to the private key ".pem" file, the 2d argument is the path to the location of the file you want to copy from your machine, and the third argument is the path to the location on the example where you desire to re-create it.
scp -i path/to/.pem_file path/to/file username@host_address.amazonaws.com:/path_to_copy
The username@host_address.amazonaws.com
in the to a higher place lawmaking is nil just the domain name of the instance that you lot can find on the "View case" folio on aws.
For Example, i have my individual primal file "test.pem" and a python script "test.py" in the location "D:/ec2/" and i want to re-create them from my local machine into the habitation binder of the ec2 instance.
scp -i D:/ec2/examination.pem D:/ec2/exam.py ec2-user@ec2-eighteen-191-31-0.us-eastward-2.compute.amazonaws.com:/dwelling house/ec2-user
Make sure y'all run this in a windows command line terminal and not the Putty last,. Note: If you go an fault saying " Permission denied " or " Permissions are too open ", then we need to modify the permissions for the ".pem" private key file such that just the owner has access to it.
3. Right Click on the ".pem" file > Select Properties > Click on the "Security" tab > Click "Avant-garde" > Click on "Disable Inheritance". This should clear upward all the Permission entries.
4. Y'all now need to add together permissions for the owner, So make a note of the username that shows up every bit possessor on the screen. So Click "Add" > Click on "Select a principal" > Type in the owner name into the text box that says "Enter the object name to select" > Click on "Check Names". Now the Owner name should appear in that box > Click OK > Bank check "Full Command" > Click OK
5. Run the scp command again now and you should see your files transferred to the EC2 instance.
At present connect to the EC2 instance in Putty and blazon in ls
, this gives you lot the listing of all files in the current working directory, you should now see the "examination.py" file which is a python script to print "Hello World". Run that script using python examination.py
and you should see the output.
If you want to open the python file, come across information technology's content and make changes to it y'all can use the post-obit Linux commands in the Putty terminal.
six exam.py
opens upwardly the python script for viewing. If you desire to edit the file hit i
then edit the file using your keyboard, afterward y'all are done making changes, hitting the escape cardinal so type in :wq
and striking the enter key, to write the changes to the file and quit.
You can too setup a cron chore to automatically run python scripts at a specified interval or time in your ec2 instance. To do this:
- Outset the cron service
sudo service crond showtime
- Edit the crontab file using
crontab -e
- Add the file path of the file you desire to run. Striking the " i " key to start editing the file and then add
0 */1 * * * python path/to/file
. Make sure you give the full path to the python script that you want to run.
The cron fourth dimension cord is five values separated by spaces each specifying the minute (0–59), hour(0–23), day of the month(ane–31), month(1–12) and twenty-four hours of the week (0–7, 0 and 7 both represent Lord's day).
If you append a slash ( / ) and an integer to a wildcard in any of the character positions, you tin can configure the cron job to run at a regular interval .
If y'all want to practice some spider web scraping using Selenium and ChromeDriver, check out my adjacent post: https://medium.com/@praneeth.jm/running-chromedriver-and-selenium-in-python-on-an-aws-ec2-case-2fb4ad633bb5
verbrugghentheacted.blogspot.com
Source: https://praneeth-kandula.medium.com/running-python-scripts-on-an-aws-ec2-instance-8c01f9ee7b2f