如何在 Amazone 的 EC2 实例中运行代码?

发布于 2024-11-07 13:47:53 字数 212 浏览 1 评论 0原文

我对 EC2 的功能几乎一无所知。我创建了一个 Amazon Web Service (AWS) 帐户。然后我启动了一个 EC2 实例。

现在我想在这个实例中执行Python代码,但我不知道如何继续。是否有必要在实例中的某个地方加载代码?或者在 Amazon 的 S3 中并将其链接到实例?

哪里有解释可能的实例用法的指南?我感觉自己就像一个站在飞碟仪表板前的人,没有用户指南。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅紫色的梦幻 2024-11-14 13:47:53

这是将 Python 脚本从本地移动到 EC2 实例并运行它的非常简单的过程。

> 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.

对于那些喜欢逐步显示内容的人来说,这里有一个具体示例:

  1. 在本地目录中,使用以下代码创建一个 python 脚本: print("Hello AWS")
  2. 假设您已经拥有 AWS设置完毕后,如果您想在 EC2 中运行此脚本,您需要将文件 SCP(安全复制协议)保存到 EC2 中的目录中。这是一个例子:

    - 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 [电子邮件受保护]:/home/ec2-user

  1. 现在 ssh 到您的 ec2 实例,cd 到 /home/ec2-user (或您放置文件的任何位置)和 Python hello_aws.py

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
惯饮孤独 2024-11-14 13:47:53

您有多种选择。您可以此处浏览大型 AMI 库。

您可以导入虚拟机,说明位于此处

这是一篇关于 AWS 和 python 的一般文章。

在本文中,作者将带您了解使用强烈推荐的 django 框架结合 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.

半葬歌 2024-11-14 13:47:53
  1. 通过 Amazon 的管理控制台启动您的实例 ->实例操作 ->连接
    (更多详细信息请参阅入门指南)

  2. 启动基于 Java 的 SSH 客户端

  3. 插件 -> 插件SCFTP 文件传输

  4. 上传您的文件

  5. 在后台运行您的文件(末尾带有“&”或使用 nohup)

请务必选择包含 python 的 AMI,您可以通过在 shell 中输入 '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.

烟酉 2024-11-14 13:47:53

在 Linux ec2 实例上运行脚本


我必须在 Amazon ec2 上运行脚本并了解如何执行此操作。尽管这个问题是几年前提出的,但我想我应该分享一下今天它是多么容易。

设置 EC2 并通过 ssh 连接到 ec2 主机

  • 使用默认设置注册并启动 ec2 实例(不要忘记保存启动 ec2 时将生成的证书文件)。
  • ec2 启动并运行后,为证书文件 chmod 400 /path/my-key-pair.pem(或 .cer 文件)提供所需的权限
  • 运行命令:ssh -i /path/my-key-pair.pem(.cer) USER@Public DNS(USER 数据根据您启动的操作系统而变化,请参阅以下段落了解更多详细信息&&公共DNS可以在ec2实例页面获取)

使用 ssh 命令连接到实例。您指定私钥 (.pem) 文件和 user_name@public_dns_name。对于 Amazon Linux,用户名为 ec2-user。对于 RHEL,用户名是 ec2-user 或 root。对于 Ubuntu,用户名是 ubuntu 或 root。对于 Centos,用户名为 centos。对于 Fedora,用户名为 ec2-user。对于 SUSE,用户名为 ec2-user 或 root。否则,如果 ec2-user 和 root 不起作用,请咨询您的 AMI 提供商。

将脚本克隆到 EC2

为了在 ec2 上运行脚本,我更愿意将代码作为存储库或要点(如果您需要保持代码私有)存储在 Github 上,然后克隆到 ec2 中。

上面提到的很简单并且不容易出错。

运行 python 脚本

我使用过 RHEL Linux 实例,并且已经安装了 python。因此,我可以在 ssh-ing 后直接运行 python 脚本来托管。这取决于您选择的操作系统。如果尚未安装,请参阅 aws 手册。

参考:AWS 文档

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文