Skip to content

Latest commit

 

History

History
177 lines (152 loc) · 17.8 KB

File metadata and controls

177 lines (152 loc) · 17.8 KB

Deploy Python Project on AWS

Steps

  1. Signin to AWS usning rootuser credentials: https://aws.amazon.com/ after login screen image
  2. We will use EC2 Serive Amazon Elastic Compute Cloud (Amazon EC2) provides scalable computing capacity in the Amazon Web Services (AWS) Cloud. Using Amazon EC2 eliminates your need to invest in hardware up front, so you can develop and deploy applications faster. EC2 is kind of Virtual Machine where we can install any operating system and perform any activity on top of it. Other alternatives in AWS such as Lambda, Elastic Beanstalk and there are some separate platform also available in AWS for machine learning projects as well
  3. Go to EC2 (Services > EC2 or search EC2 in search bar) image Clikc on Instances and check running instances image One already running instance in my account image

Create New Instance

  1. Click on Launch Instances image
  2. Give Name image
  3. Select OS, here we can install any OS in our instance, we will use Ubuntu image image
  4. Select Instane Type: very important step, if you wan to use for free, then only select free tires eligible type else you will have to pay for your service image
  5. Create a Key Pair for access this instance outside image
  • Click on Create new Key pair, give relatable key any name and click Create key pair a .pem file will be downlaoded keep it safe and private, without this key cannot be loggedin into this instance. image image
  1. Network Settings: very import step, click on Edit button image
  • Come down and click Add Security Group Rule button image
  • Select Type : All Traffic and Source Type : Anywhere, do not touch any other fields
  • Note: Without this security group we will not able to see webapps image
  1. Configure Storage: this is the C drive storage, select as per required, free account only get 30gb per account here we selecting 8GB C drive storage. image

  2. Done with basic configuration, now click on Launch Instance image

  3. Launching Status: image image

  4. Check Created Instance Click on Instances, remove filter if any applies, all instaces will display image Click on Instance id to check the full details image

Communicate with EC2 Instance

  • We can communicate using any terminal like windows cms, linux terminal, gitbash terminal, putty etc...

Now, we are ready to communicate our Machine

  1. Click on Instance Id image
  2. Click on Connect image
  3. Go to SSH Client and copy the example command image
  4. Now, go to local system where login key has been downloaded or move that key to any specific folder then open CMD at the specific folder

Connect to instance

1. Connect Our Instance using Windows CMD(SSH client supported)

  • now open cmd in key folder image
  • now paste the copied command in cmd and press enter image
  • Now, type yes and press enter.... bingooo!! we are not connected to our instanceimage

2. We cannot connect our instance using windows cmd if does not support ssh communication, for that we can use below methods

A. Connect Our Instace using GitBash, download and install gitbash for windows

  • open gitbash terminal and change directory where key is present image
  • Paste the same command and press enter image connected to instance image

B. We can connect using Putty https://www.putty.org/

Let's Run some command to check our AWS Machine (below cmds will work windows as well as linux)

  1. Check the OS, cmd: cat /etc/os-release, is that the same as we configured while creating instance image
  2. Let's Check RAM details cmd: free -m we have selected 1gb image
  3. Check CPU details cmd: lscpu selected 1 CPU image
  4. Check Storage Capacity cmd: df -h image image
  5. Check Ip details cmd: ip a image image

We establised communication with EC2 instance using terminals, now we need to upload our program files to AWS machine

Setup WinSCP

  1. Download and install WinSCP
  2. Open WinSCP image
  3. To connect we need some details Hostname, username and password of our EC2 machine
    • Find Hostname of EC2 instance, go to instance page > connect > SSH clientimage
    • to Find Uername : Instance > EC2 Instance Connect image
    • Password we have already .pem file as password
  4. Enter the Hostname and Username image
  5. To enter the password winscp required .ppk format instead of .pem, click on Advancedimage
  6. Select SSH > Authentication and then select private key file .pemimage
  7. if .pem file not visible while selecting then change the file type to All File (.) in file explorer window then select that image
  8. It will ask to change file type to putty, click yes and then will ask to save new .ppk file, allow that image image image
  9. Now, we are ready to connectimage
  10. Click ok and Click on Login to connect EC2image
Bingooooooooooo!! we are connected now image

image

  • We don't have any files in our EC2 machine
  • using Winscp we can upload any file from our local machine to ec2 by jusy drag and drop
  • uploading a python file to EC2, just select the file you want to upload > right click> upload image
  • File uploaded to EC2 image
  • Also we can upload file and folders using drag and drop image
  • Check all files uisng terminal cmd: ls image

If we have connected with WinSCP then we donn't need to configure Putty, it's automatically configured

  • To launch Putty click on image
  • Connected easly image

Install Python and packages on EC2 using Putty or any other terminals

  • Lets check python comes with ubuntu or not, type python and press enter image

  • Let's check for python3, so python3 is installed in our ubuntu os image

  • we can install python using sudo apt install python3 command if python not installed image

  • update sedu command sudo apt update

  • install pip for installing python packages using command sudo apt install python3-pip

  • installing numpy using pip install numpy image

  • installing multiple python package using requirements.txt file at once pip install -r requirements.txt image

app.py file closing line should look like as below, host=0.0.0.0, and port can be anything in this case port=8080

  • this 8080 port our this program will deploy image

Deploye the WebApp

  • to run the app.py file, run command python3 app.pyimage

  • As mentioned above app running on this http://172.31.90.153:8080 server, but when you visit this link it will not work

  • to see the deployment, use private DNS address along with port no: image

  • our app is running at ec2-52-91-185-213.compute-1.amazonaws.com:8080 open this link in browser image

  • To press Ctrl+C in terminal to quite/terminate the app

  • Once the app is terminated this link will not work! image

  • when you close the terminal then also this app will not work, to keep running the app we need to run the app in specific way

Keep running/alive the App even after closing Terminal

  • to do so, first the terminate the code if running then, run this command nohup python3 app.py &image
  • even after closing terminal app will visible

Check running process in Linux

  • ps -aux command will give the all running process listimage

  • python script running image

  • Check any specific port running status sudo lsof -i:8080 image

  • Kill/stop running process on any port kill -9 processId, process will get terminated image

  • or sudo kill -9 sudo lsof -t -i:9001 image

  • check port status after kill, there will no record image

Access EC2 Ubuntu machine in Remote Desktop

  1. Install XRDP in Ubuntu: sudp apt install xrdp image
  2. Now, Enable XRDP: sudo systemctl enable xrdp image
  3. sudo add-apt-repository ppa:gnome3-team/gnome3 image
  4. sudo apt-get install gnome-shell ubuntu-gnome-desktop this step will take 10-15 minutes image
  5. Setup Ubuntu Password: sudo passwd ubuntu here ubuntu is username (type and confirm password) image
  6. Reboot or (Stop and Start) before connecting to Remote Desktop the EC2 Ubuntu instance image
  7. Now, Open Remote Desktop Connection in windows image
  8. Copy the Enter the Public DNS IPv4 image
  9. image
  10. Confirm Yes image
  11. image
  12. Enter the Username ubuntu and password that setup earlier 12345 and click Ok image
  13. Bingoooo!!! Connected to our Ubuntu GUI, although it might work very slow or get stuck below we are running this machine at very low configuration image
  14. image image