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

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


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 ec2-user@ec2-34-201-49-170.compute-1.amazonaws.com:/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


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.


  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.