Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 463
humalayi Avatar asked May 17 '11 11:05

humalayi


People also ask

How do I run a command in AWS?

Open the AWS Systems Manager console at https://console.aws.amazon.com/systems-manager/ . In the navigation pane, choose Run Command. If the AWS Systems Manager home page opens first, choose the menu icon ( ) to open the navigation pane, and then choose Run Command. Choose Run command.


1 Answers

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
like image 181
MLhacker Avatar answered Oct 17 '22 11:10

MLhacker