How to run a code in an Amazone's EC2 instance?

PythonAmazon Ec2

Python Problem Overview


I understand nearly nothing to the functioning of EC2. I created an Amazon Web Service (AWS) account. Then I launched an EC2 instance.

And now I would like to execute a Python code in this instance, and I don't know how to proceed. Is it necessary to load the code somewhere in the instance? Or in Amazon's S3 and to link it to the instance?

Where is there a guide that explain the usages of instance that are possible? I feel like a man before a flying saucer's dashboard without user's guide.

Python Solutions


Solution 1 - Python

Here's a very simple procedure to move your Python script from local to EC2 Instance and run it.

> 1. scp -i <filepath to Pem> <filepath to Py File> ec2-user@<Public DNS>.compute-1.amazonaws.com:<filepath in EC2 instance where you want
> your file to be>
> 2. Cd to to the directory in EC2 containing the file. Type Python <Filename.py> There it executed.

Here's a concrete examples for those who likes things shown step-by-step:

  1. In your local directory, create a python script with the following code: print("Hello AWS")
  2. Assuming you already have AWS already set up and you want to run this script in EC2, you need to SCP (Secure Copy Protocol) your file to a directory in EC2. So here's an example:

    • My filepath to pem is ~/Desktop/random.pem.

    • My filepath to py file is ~/Desktop/hello_aws.py

    • My public DNS is ec22-34-12-888

    • The ec2 directory where I want my script to be is in /home/ec2-user

    • So the full command I run in my local terminal is:

> scp -i ~/Desktop/random.pem ~/Desktop/hello_aws.py [email protected]:/home/ec2-user

  1. Now ssh to your ec2 instance, cd to /home/ec2-user (Or wherever you put your file) and Python hello_aws.py

Solution 2 - Python

You have a variety of options. You can browse through a large library of AMIs here.

You can import a vm, instructions are here.

This is a general article about AWS and python.

And in this article, the author takes you through a more advanced system with a combination of datastores in python using the highly recommend django framework.

Solution 3 - Python

  1. Launch your instance through Amazon's Management Console -> Instance Actions -> Connect (More details in the getting started guide)

  2. Launch the Java based SSH CLient

  3. Plugins-> SCFTP File Transfer

  4. Upload your files

  5. run your files in the background (with '&' at the end or use nohup)

Be sure to select an AMI with python included, you can check by typing 'python' in the shell. If your app require any unorthodox packages you'll have to install them.

Solution 4 - Python

Running scripts on Linux ec2 instances


I had to run a script on Amazon ec2 and learned how to do it. Even though the question was asked years back, I thought I would share how easy it is today.

Setting up EC2 and ssh-ing to ec2 host

  • Sign up and launch an ec2 instance(Do not forget to save the certificate file that will be generated while launching ec2) with default settings.
  • Once the ec2 is up and running, provide required permissions to the certificate file chmod 400 /path/my-key-pair.pem (or .cer file)
  • Run the command: ssh -i /path/my-key-pair.pem(.cer) USER@Public DNS(USER data changes based on the operating system you have launched, refer to the below paragraph for more details && Public DNS can be obtained on ec2 instance page)

> Use the ssh command to connect to the instance. You specify the private key (.pem) file and user_name@public_dns_name. For Amazon Linux, the user name is ec2-user. For RHEL, the user name is ec2-user or root. For Ubuntu, the user name is ubuntu or root. For Centos, the user name is centos. For Fedora, the user name is ec2-user. For SUSE, the user name is ec2-user or root. Otherwise, if ec2-user and root don't work, check with your AMI provider.

Clone the script to EC2

In order to run the scripts on ec2, I would prefer storing the code on Github as a repo or as a gist(if you need to keep code private) and clone into ec2.

Above mention is very easy and is not error-prone.

Running the python script

I have worked with RHEL Linux instance and python was already installed. So, I could run python script after ssh-ing to host directly. It depends on your operating system you choose. Refer to aws manuals if it's not installed already.

Reference: AWS Doc

Solution 5 - Python

simply add your code to Github and take clone on EC2 instance and run that code.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhumalayiView Question on Stackoverflow
Solution 1 - PythonMLhackerView Answer on Stackoverflow
Solution 2 - Pythonmarr75View Answer on Stackoverflow
Solution 3 - PythonCristianView Answer on Stackoverflow
Solution 4 - PythonunknownerrorView Answer on Stackoverflow
Solution 5 - PythonRajesh JethwaView Answer on Stackoverflow