简体   繁体   English

定期在 AWS 服务上运行 python 代码

[英]Run python code on AWS service periodically

I need to run some python code on aws platform periodically(probably once a day).我需要定期在 aws 平台上运行一些 python 代码(可能每天一次)。 Program job is to connect to S3, download some files from bucket, do some calculations, upload results back to S3.程序工作是连接到 S3,从存储桶下载一些文件,做一些计算,将结果上传回 S3。 This program runs for about 1 hour so I cannot make use of Lambda function as it has a maximum execution time of 900s(15mins).该程序运行约 1 小时,因此我无法使用 Lambda 函数,因为它的最长执行时间为 900 秒(15 分钟)。

I am considering to use EC2 for this task.我正在考虑使用 EC2 来完成这项任务。 I am planning to setup python code into a startup and execute it as soon as the EC2 instance is powered on.我计划将 python 代码设置为启动并在 EC2 实例启动后立即执行它。 It also shuts down the instance once the task is complete.一旦任务完成,它还会关闭实例。 The periodic restart of this EC2 will be handled by lambda function.此 EC2 的定期重启将由 lambda 函数处理。

Though this a not a best approach, I want to know any alternatives within aws platform(services other than EC2) that can be best of this job.虽然这不是最好的方法,但我想知道 aws 平台(EC2 以外的服务)中的任何替代方案,可以最好地完成这项工作。

Since自从

If you are looking for other solutions other than lambda and EC2 (which depending on the scenario it fits) you could use ECS (Fargate) .如果您正在寻找除 lambda 和 EC2(取决于它适合的场景)之外的其他解决方案,您可以使用ECS (Fargate)

It's a great choice for microservices or small tasks.它是微服务或小任务的绝佳选择。 You build a Docker image with your code (Python, node, etc...), tag it and then you push the image to AWS ECR.您使用代码(Python、节点等)构建 Docker 映像,标记它,然后将映像推送到 AWS ECR。 Then you build a cluster for that and use the cloudwatch to schedule the task with Cloudwatch or you can call a task directly either using the CLI or another AWS resource.然后,您为此构建一个集群并使用 cloudwatch 通过 Cloudwatch 安排任务,或者您可以使用 CLI 或其他 AWS 资源直接调用任务。

  • You don't have time limitations like lambda你没有像 lambda 这样的时间限制
  • You don't also have to setup the instance, because your dependencies are managed by Dockerfile您也不必设置实例,因为您的依赖项由 Dockerfile 管理
  • And, if needed, you can take advantage of the EBS volume attached to ECS (20-30GB root) and increase from that, with the possibility of working with EFS for tasks as well.并且,如果需要,您可以利用附加到 ECS(20-30GB 根)的 EBS 卷并从中增加,并且还可以使用 EFS 处理任务。

I could point to other solutions, but they are way too complex for the task that you are planning and the goal is always to use the right service for the job我可以指出其他解决方案,但它们对于您计划的任务来说太复杂了,目标始终是为工作使用正确的服务

Hopefully this could help!希望这会有所帮助!

Using EC2 or Fargate may be significant overkill.使用 EC2 或 Fargate 可能会显得有些矫枉过正。 Creating a simple AWS Glue job triggered by a Lambda function (running once per day) to do this (pull from S3, open selected files (if required), do some calculations on the files contents, then push results back to S3) using Python and the AWS boto3 library (and other standard Python file-reading libs if necessary) is most likely your easiest route.创建一个由 Lambda 函数(每天运行一次)触发的简单AWS Glue作业来执行此操作(从 S3 拉取,打开选定的文件(如果需要),对文件内容进行一些计算,然后使用 Python 将结果推回 S3)而 AWS boto3库(以及其他标准的 Python 文件读取库,如有必要)很可能是您最简单的途径。

See this SO question for an example and solution . 有关示例和解决方案,请参阅此 SO 问题

Good luck!祝你好运!

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

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