Recommendations for how to achieve something on AWS

I have one EC2 box, which is a webserver and also does a few other things too.

SoI have a script that runs ffmpeg to analyse podcasts for loudness and stuff. It’s a slow process (10x speed of a podcast, so if it’s 30 minutes it takes 3 minutes to run), and it is quite processor heavy. (I’m running ffmpeg with “nice”, which I think gives it a lower priority than everything else). I have a script which runs this every hour (choosing a different podcast every time). It puts the results into a database.

I’d like to run it on demand rather than on a slow Cron job. But I also would like to ensure it isn’t expensive to run. And while it runs, my box is running at 100% CPU (which isn’t an issue, but I’m getting plenty of alarms!)

Hour would you architect this? Would Lambda be very expensive?


Related discussions:

Are the podcasts for broadcasting/downloading, or collected podcasts for _you_ to listen to?

Don’t use Lambda for this, the costs could be astronomical.

look into aws batch

Lambda would only become unfeasibly expensive if it ran frequently enough - there is a tipping point where EC2 becomes cheaper. Also, duration would be a limiting factor - with a maximum execution time, your process has to fit in that, or risk being cut off.

Lambda’s can be triggered with “cron” (technically Cloud Watch scheduled Events), and depending on what your database is, can be run inside a VPC (for RDS), or outside (where it doesn’t matter, say, DynamoDB).

To keep it cheap, I would have an Autoscaling Group (ASG) built from a spot fleet.
Set a startup script that pulls an item from an SQS queue, processes the job, updates the DB, then deletes the item from the
SQS queue and sets the ASG desired size to 0. Make an AMI from that system and build your autoscaling group from it.

Your cron job could then run hourly, sending the next podcast to SQS and setting the Desired Size of the ASG to 1.

IAM instance roles on the webserver to allow sending the SQS message and setting the ASG size, and an IAM instance role on the ASG allowing receive and delete SQS messages, writing to your DB, and setting the ASG size.

ffmpeg appears to be multithreaded too, so you could experiment with larger instances in your spot fleet, and potentially accelerate the processing time, if that matters, and it would also reduce the billable time.

Have a look at spot instances to run this if you stay with EC2.
You could trigger the jobs with cloudwatch (start the instance etc) rather than cron and shutdown the machine afterwards.

if your instance is c or m, you can do processing in the background, it should not matter much. but you should also rethink this choice, because a t instance is probably cheaper.

if you use t, putting processing there might be suboptimal cost-wise.

if you are willing to do some extra work, aws batch is for this exact task. it will launch a c or m instance for you on demand, whenever you submit a job. it will even nicely parallelize if there are more than one files to process. but you need to pack your processing environment into a docker image, which has some learning curve, and also extra setup, like accessing the file itself.

Have you entertained SQS or SNS to trigger the jobs? Are you on a framework like Ruby on Rails to leverage existing implementations?

AWS Batch solves this problem well. It manages the queue and the ec2 resources on spot. The only cost is the cost of the ec2 instances, so it comes out cheaper than managing SQS and ec2 autoscaling yourself.

The only thing you have to do is make a docker image of the application.

This is the right idea. Consider using a Spot Fleet that automatically scales based on the depth of messages in an SQS queue.

Store the podcasts in an S3 bucket, and configure a bucket notification such that every time a new podcast is added to the bucket, a message is added to the SQS queue.

The Spot Instances in the Spot Fleet are running workers that check for messages in the SQS queue. If they find work, they check out the message, run the work, write the results to the DB, and then remove the message from the queue.

The Spot Fleet has an automatic scaling policy that scales based on the depth of the message queue to make sure you are only paying for the compute instances when you need them.

You can find an example of this here:
https://github.com/awslabs/ec2-spot-labs/tree/master/sqs-ec2-spot-fleet-autoscaling

I also gave a webinar on this subject here:
https://pages.awscloud.com/Running-Cost-Effective-Batch-Workloads-with-AWS-Batch-and-Amazon-EC2-Spot-Instances_1022-CMP_OD.html

Please reach out if you have questions - happy to help.

Edit: One more example of this- check out Step 7:
https://github.com/aws-samples/immersive-media-refarch

Reuters puts videos into an S3 bucket, which triggers a Lambda function that splits it up into 0.5 s chunks, putting them into a second S3 bucket. For every fragment a new lambda is invoked to do encoding on only that 0.5 s chunk, which is very fast. This allows them to do this work in parallel. Then they stitch it back together.

Interesting. They probably don’t stitch it back together fully - HLS uses chunks anyway.

Yeah, I think queues would work well. I could certainly batch them together, and perhaps run a cron once a day to whisk through them. That might be simpler.

This is great information, thank you. The only reason I’m running the cron job once an hour is just to trickle through the podcasts (though I think I’ve identified that the script is probably timing out too often). AWS Batch looks like my reading for the day.

I guess on the other hand that as long as I’m running that script at the right priority, it’s fine - running at 100% CPU just means I’m getting value out of my server, right? :wink:

Kubernetes could work too. Have a pod that regularly scrapes s3 or listens to SQS, and then can trigger a k8s job. (container / group of containers that run until job completion). Amazon EKS is the hosted solution. Would have to do a cost analysis though.

(I use PHP, since you asked)

Neither.

https://podnews.net/article/podcast-analysis is some of the output.

Yep - absolutely nothing wrong with using all of your CPU as long as it doesn’t interfere with the primary workload. If you did want to scale beyond the CPU available in your existing instance, please definitely consider using Spot Instances and an architecture described as above. If you have questions, you can reach me at schmutze@amazon.com.

I think I might do that. What I ought to do is to stop the alarm about “CPU over 90%” that goes off… :wink:

Thanks, that’s helpful.