如何在 Amazone 的 EC2 实例中运行代码?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是将 Python 脚本从本地移动到 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:
Here's a very simple procedure to move your Python script from local to EC2 Instance and run it.
Here's a concrete examples for those who likes things shown step-by-step:
- 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:
您有多种选择。您可以此处浏览大型 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.
通过 Amazon 的管理控制台启动您的实例 ->实例操作 ->连接
(更多详细信息请参阅入门指南)
启动基于 Java 的 SSH 客户端
插件 -> 插件SCFTP 文件传输
上传您的文件
在后台运行您的文件(末尾带有“&”或使用 nohup)
请务必选择包含 python 的 AMI,您可以通过在 shell 中输入 'python' 进行检查。
如果您的应用程序需要任何非正统的软件包,您就必须安装它们。
Launch your instance through Amazon's Management Console -> Instance Actions -> Connect
(More details in the getting started guide)
Launch the Java based SSH CLient
Plugins-> SCFTP File Transfer
Upload your files
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.
在 Linux ec2 实例上运行脚本
我必须在 Amazon ec2 上运行脚本并了解如何执行此操作。尽管这个问题是几年前提出的,但我想我应该分享一下今天它是多么容易。
设置 EC2 并通过 ssh 连接到 ec2 主机
将脚本克隆到 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
chmod 400 /path/my-key-pair.pem
(or .cer file)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)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