Would like to run Python scripts once every 50mins (Take around 2mins to run), best way to setup?

I know they now charge per min, how can I automate once every 50mins I open an instance for a few mins, up to 5mins lets say of EC2, and then it computes my scripts, then closes and repeat.


Related discussions:

Would running on a Lambda solve your problem? Just schedule it for every 50 minutes. Seems like the exact use-case for one.

This sounds like a use case for Lambda. If you can get your script to work entirely within the Lambda framework, you’ll find it’s extremely inexpensive.

Otherwise, you can still trigger Lambda with a scheduled event (cron) and have a simple script launch your EC2 instance when it’s time. Make your script run at boot, and when it finishes, have it shut down the instance.

This might work for bring up. Shutdown, better go with “resource usage (or) Idle instance” based approach by using CloudWatch Alarms.

sure I’ve not done much with AWS other than S3 website hosting.

Is it hard to make script work entirely in lambda framework? Where do I get started learning about this?

Maybe I have no idea I am new to AWS

Not sure what you are running but if its simple script, lambda does support python 2.7 so it could be simple to migrate your code.

If you add additional modules to import you can package it via virtualenv

https://docs.aws.amazon.com/lambda/latest/dg/lambda-python-how-to-create-deployment-package.html

yep. this is exactly what i use lambda for.

https://aws.amazon.com/lambda/

I am novice as well and got 3 services tied together with Lambda working in a few hours.

My use case was deploying a SageMaker endpoint which is required to be called from a Lambda function for security. The Lambda outputs JSON and then I exposed the Lambda function to web request with API Gateway.

During my implementation of this I discovered nothing but comments of peoples minds blown at how powerful Lambda could be for replacing various infrastructure including even hosting an entire web app.

https://docs.aws.amazon.com/lambda/latest/dg/with-scheduled-events.html

Link to doc about how to schedule events on Lambda. Ping if you need help :slight_smile:

it supports 3.6 now

I think the idea is that a script that takes 2 minutes to run should just be a lambda function and not run on ec2 at all.

yea and if needed i can strip the function down. on my 50mb line my functions can be done in 1min with 350mb of ram. I could try make them more efficient

Don’t worry about memory too much as the Lambda performance (CPU and network) is tied to the memory limit you choose on the function. Running once per hour even at a full 5 minutes with 1024mb memory should still be extremely cheap.

I’m trying to stay in the free tier if possible haha. but I definitely don’t need a gig of memory it just requests and parses some pages

There are a lot of tools to make it easier or more efficient to setup/test/use Lambda functions, and I’ve personally used this one (mainly for quick local testing): https://github.com/nficano/python-lambda

what does your python script (need to) do? and who does it (need to) do it to?

it’s a bot for a text based video game from 2006. it’s an assignment basically.

it uses beautifulsoup, robobrowser, re.

it goes on a site, logs in, goes to a bunch of pages, parses the html, reads it with beautiful soup then a bunch of loops and if statements decide what actions to take.

so it takes around 30 seconds per action on my 50MiB/s internet with 350mb of ram.

there are 2 actions is needs to complete every 50mins.

if you would like to see my code I can send snippets later.

Either Lambda as people have suggested or Celery

419.4Mbps? Seems like an odd speed for an ISP to provide.