简体   繁体   English

从AWS Lambda读取EC2上的文件

[英]Reading files on EC2 from AWS Lambda

I have some files uploaded on an EC2 instance. 我在EC2实例上上传了一些文件。 I would like to read and process them from a lambda function. 我想从lambda函数读取和处理它们。

Is there a short snippet (Python preferred) on how to do it? 是否有一个简短的代码段(首选Python)? Or just a short tutorial? 还是只是一个简短的教程?

I've been through ton of tutorials but I can't figure it out yet 我已经看过很多教程,但我还不能弄清楚

If you decide to use a EC2 instance to serve files, make sure: 如果您决定使用EC2实例来提供文件,请确保:

  • You can reach the instance from outside which means it needs to have a public IP and the server has to listen for connections from anywhere (0.0.0.0) 您可以从外部访问该实例,这意味着它需要一个公共IP,并且服务器必须侦听来自任何地方的连接(0.0.0.0)
  • EC2 security group has to allow 0.0.0.0 for 80 or 443 (or custom port) EC2安全组必须为80或443(或自定义端口)允许0.0.0.0

In Python, there is SimpleHTTPServer . 在Python中,有SimpleHTTPServer You can use it as a starting point and harden it to suit to your needs. 您可以将其用作起点并根据需要进行硬化。

import SimpleHTTPServer
import SocketServer

PORT = 8000

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

You would have to serve the files somehow from the EC2 server using a web server or something. 您将必须使用Web服务器或其他方式以某种方式从EC2服务器提供文件。 Or you could try using an SCP or SFTP library in your Lambda function to connect to the EC2 server and copy the files over. 或者,您可以尝试在Lambda函数中使用SCP或SFTP库来连接到EC2服务器并复制文件。 The normal way you would share files between these services is to have a process on the EC2 server copy the files to S3 instead of leaving them on the EC2 server. 您将在这些服务之间共享文件的通常方法是在EC2服务器上进行一个将文件复制到S3的过程,而不是将文件留在EC2服务器上。

You must understand that, as per default, your EC2 volumes are not available to anyone (or anything) outside your instance. 您必须了解,默认情况下,实例之外的任何人(或任何人)都无法使用EC2卷。 They are, for the sake of understand, "local volumes". 为了理解,它们是“本地卷”。

Your lambda code runs outside your EC2 instances. 您的lambda代码在EC2实例外部运行。 You can configure it to run like it is in your VPC, but even in this case it will not have access to your "local" volumes inside your instance. 您可以将其配置为像在VPC中一样运行,但是即使在这种情况下,它也无法访问实例内的“本地”卷。

To give access to the files inside your EC2 to lambda functions you'll have a few options: 要将访问EC2内部文件的权限授予lambda函数,您将有几种选择:

  • You can install a file sharing service like FTP in yout instance and connnect to it in your lambda; 您可以在实例中安装FTP之类的文件共享服务,并在其lambda中连接该服务。
  • You can install a endpoint service to execute tasks on files inside your EC2 and connecto to this service from your lambda. 您可以安装终结点服务以对EC2内的文件执行任务,并从lambda连接到该服务。

But the point is: Maybe this design is not the solution for your need, considering all other AWS services available. 但是重点是:考虑到所有其他可用的AWS服务,也许这种设计不是您需要的解决方案。 As told in the previous response, maybe you'll be better served using S3. 如先前的回复所述,使用S3可能会更好地为您服务。 Or maybe lambda is not the best compute solution. 也许lambda不是最好的计算解决方案。 Keep in mind that Lambda has a few time limits to be run and using a disk operation by network will put you in a dangerous zone considering Lambda limitations. 请记住,Lambda有一些运行时间限制,考虑到Lambda限制,通过网络使用磁盘操作将使您处于危险区域。

If you can give us more information about your use case we can help you better. 如果您可以向我们提供有关您的用例的更多信息,我们可以为您提供更好的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM