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

前端 未结 4 1974
-上瘾入骨i
-上瘾入骨i 2020-12-04 07:04

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 execut

4条回答
  •  醉酒成梦
    2020-12-04 07:56

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

    > 1. scp -i   ec2-user@.compute-1.amazonaws.com: your file to be>
    > 2. Cd to to the directory in EC2 containing the file. Type Python  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

提交回复
热议问题