简体   繁体   English

EC2实例中的.py文件可在S3存储桶上的事件上执行

[英].py file in EC2 instance to execute on event from S3 Bucket

i have a .py file thats on an EC2 instance. 我在EC2实例上有一个.py文件。 Im trying to have the .py file run when an event(file uploaded to S3 Bucket) occurs. 我试图在事件(文件上传到S3存储桶)发生时运行.py文件。

I currently have an event notification that is sent to a AWS Lambda function that starts the EC2 instance, here is that code from the AWS console: 我目前有一个事件通知已发送到启动EC2实例的AWS Lambda函数,这是来自AWS控制台的代码:

import boto3

id = [ec2-ID]

def lambda_handler(event, context):
    ec2 = boto3.client('ec2')
    ec2.start_instances(InstanceIds=id)

i can manually go into PuTTY and type in "python test.py" to run my program and it works, but i want to get rid of the "having to do it manually part" and have it just run itself whenever there is an event. 我可以手动进入PuTTY,然后输入“ python test.py”运行我的程序,它可以正常工作,但是我想摆脱“必须手动完成”的部分,并使其在发生事件时自行运行。

I am stumped as to how to progress. 我对如何进步感到困惑。

I thought by "starting" my EC2 instance it would run that .py file and get to work processing whats in the S3 bucket 我认为通过“启动” EC2实例,它将运行该.py文件并开始处理S3存储桶中的内容

no error messages...it just doesnt do anything at all. 没有错误信息...它什么都不做。 Its suppose to work once a file is uploaded to the S3 bucket it should send a notification to the lambda to have the EC2 start processing the file with the .py file that is on it. 它假定一旦将文件上传到S3存储桶即可工作,它应该向lambda发送通知,以使EC2开始使用其中的.py文件处理文件。

Kind regards 亲切的问候

This is a nice trick you can try - https://aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/ 这是一个不错的技巧,您可以尝试-https: //aws.amazon.com/premiumsupport/knowledge-center/execute-user-data-ec2/

This should override the fact User Data is executed only on instance first creation. 这应该覆盖用户数据仅在实例首次创建时执行的事实。 This method will allow you to execute User Data scripts on every boot. 此方法将允许您在每次引导时执行用户数据脚本。 Just update the bash from: 只需从以下位置更新bash:

/bin/echo "Hello World" >> /tmp/testfile.txt

to: 至:

python /file_path/python_file.py &

Use Cron: 使用Cron:

$ sudo apt-get install cron
$ crontab -e

# option 3 vim

#Type "i" to insert text below

@reboot python /path_directory/python_test.py &

#Type ":wq" to save and exit

To find the .py file, run: 要找到.py文件,请运行:

sudo find / -type f -iname "python_test.py"

Then add the path to Cron. 然后将路径添加到Cron。

Ttake a look at AWS Systems Manager Run Command as a way to run arbitrary scripts on EC2. 看一下AWS Systems Manager Run Command作为在EC2上运行任意脚本的一种方式。 You can do that from your boto3 client, but you'll probably have to use a boto3 waiter to wait for the EC2 instance to restart. 您可以从boto3客户端执行此操作,但是您可能必须使用boto3服务员来等待EC2实例重新启动。

Note that if you're only starting the EC2 instance and running this script infrequently then it might be more cost-effective to simply launch a new EC2 instance, run your script, then terminate EC2. 请注意,如果仅启动EC2实例并很少运行该脚本,则简单地启动一个新的EC2实例,运行脚本然后终止EC2可能更具成本效益。 While the EC2 instance is stopped, you are charged for EBS storage associated with the instance and any unused Elastic IP addresses. EC2实例停止时,将向您收取与该实例相关联的EBS存储以及任何未使用的弹性IP地址的费用。

If all you need is to run some python code and the main limitation is running time, it might be a better idea to use lambda to listen to the S3 event, and Fargate to execute the task. 如果您只需要运行一些python代码并且主要限制是运行时间,则最好使用lambda来监听S3事件,然后使用Fargate来执行任务。 The main advantage is you don't have to worry about starting/stopping your instance, and scaling out would be easier. 主要优点是您不必担心启动/停止实例,并且扩展会更容易。

There is a nice write-up of a working use case at the serverless blog 无服务器博客中有一个很好的工作用例写法

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

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