Reputation: 163
I want to run a py script in my ec2 instance automatically whenever a new file is added to an s3 bucket. As of now, I have connected my bucket to a lambda function which is triggered with event notification whenever a file is added to the bucket. How do i further use this to trigger my ec2 instance to start executing the python file? I want to send that file from the bucket to the instance and the python program will use that file as input. I had read that i can trigger the lambda function when object is added. Now how do i use it or SQS to start my instance automatically and start running the python script inside it after the file has been sent from bucket to the instance?
Upvotes: 1
Views: 2234
Reputation: 269101
There are several options:
/var/lib/cloud/scripts/per-boot/
, which will be run automatically when the instance starts. The script should trigger the app, the same as the 2nd option above, which reads from the SQS queue. Once it has emptied the queue, the script could stop the instance to minimize costs. For more details, see: Auto-Stop EC2 instances when they finish a task - DEV CommunityUpvotes: 2
Reputation: 1968
S3 can generate an event, when a new object is created https://docs.aws.amazon.com/AmazonS3/latest/userguide/NotificationHowTo.html
One type of event destination may be an SNS Topic https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-how-to-event-types-and-destinations.html
One type of SNS subscription type is an HTTP/S POST https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html
So if you script can implement a simple HTTP listener/api, it will be triggered by SNS nearly instantly.
Upvotes: 0
Reputation: 106
uploading file to s3 bucket in an event which can trigger lambda function, as you wrote. The lambda may be python code itself - in this case there is no need to run it on ec2 instance. However you have time limit (15 mins) for execution. If this is not sufficient and the ec2 instance is normally stopped, you could start your ec2 instance using boto3 (you could use lambda code described here to do this and have the instance configured to run the python script on start (for example @reboot entry in /etc/crontab)
Upvotes: 0