简体   繁体   English

如何使用 AWS Lambda 函数将文件从 S3 存储桶复制到 EC2 Windows 实例

[英]How to copy a file from S3 bucket to an EC2 Windows instance using AWS Lambda function

I am trying to copy the file(s) uploaded to S3 to the EC2 windows instance via AWS Lambda function.我正在尝试通过 AWS Lambda 函数将上传到 S3 的文件复制到 EC2 Windows 实例。 My requirement is the AWS Lambda function should trigger when a file has been uploaded to a S3 bucket and the same file should be copied to the EC2 instance.我的要求是 AWS Lambda 函数应该在文件上传到 S3 存储桶时触发,并且应该将同一文件复制到 EC2 实例。

How the AWS Lambda function can be connected to an EC2 Windows instance ? AWS Lambda 函数如何连接到 EC2 Windows 实例? Please let me how can I achieve this scenario.请让我如何实现这个场景。

You can approach this problem in two ways:您可以通过两种方式解决此问题:

  1. Creating a Shared File System for your Lambdas and EC2.为您的 Lambda 和 EC2 创建共享文件系统。 https://aws.amazon.com/blogs/aws/new-a-shared-file-system-for-your-lambda-functions/ https://aws.amazon.com/blogs/aws/new-a-shared-file-system-for-your-lambda-functions/

  2. Creating an API service on your EC2 to receive Files.在您的 EC2 上创建 API 服务以接收文件。

The first approach is the most secure and probably the better way of doing it.第一种方法是最安全的,可能也是更好的方法。 The second approach is funnier.第二种方法更有趣。 You could create a Node+Express API that receives files and just sending it from your Lambda in a POST request.您可以创建一个 Node+Express API 来接收文件并在 POST 请求中从您的 Lambda 发送它。

An AWS Lambda function runs outside of EC2 instances. AWS Lambda 函数EC2 实例之外运行。 In fact, you can think of it as a very temporary EC2 instance.实际上,您可以将其视为非常临时的 EC2 实例。

Therefore, the answer to your question is "Well, how would you copy something from an EC2 Linux instance to a Windows instance?"因此,您的问题的答案是“好吧,您如何将某些内容从 EC2 Linux 实例复制到 Windows 实例?”

The biggest issue is that good security means that it's not easy to "push" things into an EC2 instance.最大的问题是良好的安全性意味着将东西“推”到 EC2 实例中并不容易。 For example, I can't push things into the computer you are currently using.例如,我无法将东西推送到您当前使用的计算机中。 Therefore, the best answer is that instead of "pushing" the file, it is better to "pull" the file.因此,最好的答案是与其“推”文件,不如“拉”文件。 Here's a few ways:这里有几个方法:

Systems Manager Run Command系统管理器运行命令

The AWS Lambda function could trigger the AWS Systems Manager Run Command , which can execute a script on the EC2 instance. AWS Lambda 函数可以触发AWS Systems Manager Run Command ,它可以在 EC2 实例上执行脚本。 This script could 'pull' the file from S3 onto the instance.此脚本可以将文件从 S3“拉”到实例上。

This is made possible because the EC2 instance has the Systems Manager Agent installed, which can receive the request and trigger the script.这是可能的,因为 EC2 实例安装了 Systems Manager 代理,它可以接收请求并触发脚本。

Polling an Amazon SQS queue轮询 Amazon SQS 队列

The AWS Lambda function could send a message to an Amazon SQS queue. AWS Lambda 函数可以向 Amazon SQS 队列发送消息。 A script on the instance could be continually polling the SQS queue looking for a message.实例上的脚本可能会不断轮询 SQS 队列以查找消息。 When it receives a message, it uses the information to 'pull' the file from S3 into the instance.当它收到一条消息时,它使用该信息将文件从 S3“拉”到实例中。 (Use Long Polling on the SQS queue to reduce the number of requests.) (对 SQS 队列使用长轮询以减少请求数。)

In fact, Amazon S3 can send the message directly to the Amazon SQS queue without requiring the Lambda function.事实上,Amazon S3 可以将消息直接发送到 Amazon SQS 队列,而无需 Lambda 函数。

Use a Web Server使用网络服务器

The AWS Lambda function could POST the file via a web server running on the EC2 instance that is 'listening' for such requests. AWS Lambda 函数可以通过运行在 EC2 实例上的 Web 服务器发布文件,该服务器正在“侦听”此类请求。

Shared filesystem共享文件系统

As suggested by @mimetist, an AWS Lambda function can be connected to an Amazon EFS filesystem.正如@mimetist 所建议的,AWS Lambda 函数可以连接到 Amazon EFS 文件系统。 Thus, it could store the file on the filesystem, which could instantly make it accessible to the EC2 instance if it also has the same filesystem mounted.因此,它可以将文件存储在文件系统上,如果 EC2 实例也安装了相同的文件系统,则可以立即使其可访问。

The issue is fixed now by using the AWS Lambda with S3 and Systems manager worked for me.现在通过使用 AWS Lambda 和 S3 解决了这个问题,系统管理器为我工作。 I have created a node.js script to get the auto event from s3 and push back the details to a systems manager run powershell script document and thereby copying to ec2 instance which was worked as expected.我创建了一个 node.js 脚本来从 s3 获取自动事件并将详细信息推回系统管理器运行 powershell 脚本文档,从而复制到按预期工作的 ec2 实例。

You need to chose the right instance type based on the file transfer from s3 to EC2您需要根据从 s3 到 EC2 的文件传输选择正确的实例类型

暂无
暂无

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

相关问题 如何使用 lambda function 将文件从 s3 存储桶复制到 ec2 实例? - How do I copy a file from s3 bucket to ec2 instance using lambda function? 使用 Lambda 将文件从 S3 存储桶复制到 EC2 Windows 实例 - Copy files from S3 bucket to EC2 Windows Instance using Lambda 如何使用 AWS Lambda 函数将文件从 AWS S3 存储桶复制到 EC2 linux 机器 - How to copy files from AWS S3 bucket to EC2 linux machine using AWS Lambda Functions 在AWS中如何使用lambda函数将文件从一个s3存储桶复制到另一个s3存储桶 - In AWS how to Copy file from one s3 bucket to another s3 bucket using lambda function 如何将Windows EC2实例复制到AWS中的S3存储桶? - How to copy Windows EC2 instances to S3 bucket in AWS? 将文件放在S3存储桶中时,如何在AWS EC2实例上触发python脚本? (没有Lambda) - How to trigger a python script on a AWS EC2 instance when a file is placed in S3 bucket? (No Lambda) aws 发送命令不将文件从 s3 存储桶复制到 windows 服务器 ec2 实例 - aws send command not copying file from s3 bucket to windows server ec2 instance 如何使用 cloudformation 模板中的用户数据将文件从 s3 存储桶复制到 ec2 实例中 - How can I copy a file from s3 bucket into an ec2 instance using userdata inside a cloudformation template 文件从 ec2 复制到 aws 中的 s3 存储桶端口 - files copy from ec2 to s3 bucket port in aws 如何配置通过 VPC 从 aws ec2 实例访问我的 aws s3 存储桶 - How to configure access to my aws s3 bucket from aws ec2 instance via VPC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM