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

前端 未结 4 1975
-上瘾入骨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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-04 07:34

    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

提交回复
热议问题