<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Levi</title>
    <description>The latest articles on Forem by Levi (@adminl3).</description>
    <link>https://forem.com/adminl3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2326998%2F6b201a7f-393a-4289-ba81-df2899f3bcb6.jpeg</url>
      <title>Forem: Levi</title>
      <link>https://forem.com/adminl3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/adminl3"/>
    <language>en</language>
    <item>
      <title>How to run Python in the Cloud</title>
      <dc:creator>Levi</dc:creator>
      <pubDate>Sat, 04 Jan 2025 11:59:06 +0000</pubDate>
      <link>https://forem.com/adminl3/how-to-run-python-in-the-cloud-mkh</link>
      <guid>https://forem.com/adminl3/how-to-run-python-in-the-cloud-mkh</guid>
      <description>&lt;p&gt;To do this, I’m going to use Amazon Web Services (AWS) to create a virtual machine and run the Python script on it!&lt;/p&gt;




&lt;h2&gt;
  
  
  Step 1: Launch an EC2 Instance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Login to AWS Console:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Go to the AWS Management Console.&lt;/li&gt;
&lt;li&gt;Select &lt;strong&gt;EC2&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  2. Launch a New EC2 Instance:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Click &lt;strong&gt;Launch Instance&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Choose an Amazon Machine Image → &lt;strong&gt;Ubuntu Server&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select the instance type, e.g., &lt;strong&gt;t2.micro&lt;/strong&gt; (for free tier).&lt;/li&gt;
&lt;li&gt;Configure all the settings (accept defaults or customize).&lt;/li&gt;
&lt;li&gt;Under &lt;strong&gt;Key Pair&lt;/strong&gt;, either create a new key pair or select an existing one. Download the &lt;code&gt;.pem&lt;/code&gt; file (important for accessing later!).&lt;/li&gt;
&lt;li&gt;Launch the instance.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  3. Get Public DNS of the Instance:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;In the EC2 Dashboard, go to &lt;strong&gt;Instances&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Select your instance and find the &lt;strong&gt;Public DNS (IPv4)&lt;/strong&gt; address.

&lt;ul&gt;
&lt;li&gt;Should look like this: &lt;code&gt;ec2-XX-XX-XXX-XXX.compute-1.amazonaws.com&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 2: Connect to EC2 Instance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Open Terminal on Your Local Machine:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to your AWS folder:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;cd &lt;/span&gt;C:&lt;span class="se"&gt;\U&lt;/span&gt;sers&lt;span class="se"&gt;\P&lt;/span&gt;ath&lt;span class="se"&gt;\t&lt;/span&gt;o&lt;span class="se"&gt;\A&lt;/span&gt;WS
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your &lt;code&gt;key.pem&lt;/code&gt; file and other related files should be here.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. SSH into EC2 Instance:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use the public DNS or IP address from your EC2 instance:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  ssh &lt;span class="nt"&gt;-i&lt;/span&gt; key.pem ubuntu@ec2-XX-XX-XXX-XXX.compute-1.amazonaws.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;When asked if you trust the connection, type &lt;code&gt;yes&lt;/code&gt; to continue.&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 3: Install Dependencies
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. Update Package Lists:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Run the following to ensure your package lists are up to date:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Install Python and Pip on EC2 Instance:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Install Python 3 and the necessary packages:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;python3 python3-pip
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Install Other Packages (Optional):
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;If you want to install other packages or use a virtual environment, you can do that now.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Installing Selenium:
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;selenium
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Installing Chromium and ChromeDriver (for Selenium):
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;chromium-browser
&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;chromedriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Create a symlink to make ChromeDriver accessible globally:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  &lt;span class="nb"&gt;sudo ln&lt;/span&gt; &lt;span class="nt"&gt;-s&lt;/span&gt; /usr/lib/chromium-browser/chromedriver /usr/bin/chromedriver
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 4: Transfer Files from Local Machine to EC2
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Use SCP to Transfer Files:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;On your local machine, navigate to the directory where your &lt;code&gt;main.py&lt;/code&gt; or code is located.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;scp&lt;/code&gt; (SecureCopy) to copy files to your EC2 instance:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;  scp &lt;span class="nt"&gt;-i&lt;/span&gt; key.pem main.py ec2-XX-XX-XXX-XXX.compute-1.amazonaws.com:/home/ubuntu/your_project/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Ensure that you are in the correct directory where your files are located (see step 2.1).&lt;/li&gt;
&lt;/ul&gt;




&lt;h2&gt;
  
  
  Step 5: Run the Script on EC2
&lt;/h2&gt;

&lt;h3&gt;
  
  
  1. SSH Into Your EC2 Instance (if not already connected):
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ssh &lt;span class="nt"&gt;-i&lt;/span&gt; key.pem ubuntu@ec2-XX-XX-XXX-XXX.compute-1.amazonaws.com
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Navigate to the Project Directory:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd&lt;/span&gt; /home/ubuntu/your_project
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Run the Python Script:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python3 main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Step 6: Stop EC2 Instance
&lt;/h2&gt;

&lt;p&gt;Once you’re done with your EC2 instance, stop it to avoid unnecessary charges:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Go to &lt;strong&gt;EC2 Dashboard&lt;/strong&gt; in AWS.&lt;/li&gt;
&lt;li&gt;Select your instance.&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Actions&lt;/strong&gt; → &lt;strong&gt;Instance State&lt;/strong&gt; → &lt;strong&gt;Terminate Instance&lt;/strong&gt;.&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>python</category>
      <category>aws</category>
      <category>cloud</category>
    </item>
  </channel>
</rss>
