Moving video processing from ECS to Lambda

I was discussing a bit of a roadmap shift with my team today.

Right now, we have a platform ( flask API ) where users post content, we write to the DB, and then trigger an SNS workflow. The catch is the video/podcast processing. Currently, it’s all running in the same Flask code on ECS Fargate (0.5 vCPU / 1GB RAM).

It works fine for small things, but whenever a bigger video hits, the API lags or crashes. So I do quick fix like bumping the task limits, but that’s obviously not a long-term solution ( like how Instagram people use to add server manually on weekend )

The plan I am pitching to team is let’s decouple the video logic and move it to Lambda. The logic is simple. It keeps the main API light only CRUD, we get “scale-to-zero” while we’re in early stages, and we only pay when someone actually uploads.

One of my devs raised a great point: What if we hit scale? If we get uploads every second, is Lambda still the best?

My take was that it probably won’t be. At that point, we would be paying a “convenience tax” for serverless. If the demand is 24/7 and predictable, I would actually want to move it back to a dedicated Fargate or even ECS on EC2 to keep the margins healthy.

My thought model for the team was:

- Start with Lambda for agility and stability (don’t crash the API).

- Once the “idle time” disappears and we’re running 24/7, pivot back to provisioned capacity to save costs.

Curious to hear from others who have gone through this. Did you find a specific “tipping point” (requests per second or cost-wise) where you decided to move off Lambda back to Fargate/EC2 or vice versa?


Related discussions:

Keeping AWS secrets secret with ECS – Remind Engineering
Amazon ECS Allows Containers to Directly Access Environmental Metadata
Whats the best way to scale ECS?
Introducing Cloud Native Networking for Amazon ECS Containers
Are there any publicly available benchmark utilities for AWS services?
Check out my latest visual notes on the Amazon Elastic Container Service
Advice please- Multi Tenant SaaS on ECS
1 big ECS cluster w/ multiple tasks, or smaller, separate clusters w/ single tasks?
Preventing an ECS instance from being killed during scaling down?
ECS - can i scale from 0 EC2's triggered just by adding a task to a service?
ECS Scheduled Task and logging run-task failure
I'm Confused about Fargate. Does it run continuously by default? Or only when a request comes in?
AWS Fargate from the Command-Line
Is there anything like Spotinst natively offered by AWS that helps you run production workloads 24/7 using spot instances and ECS?
replace auto scaling group instances
Kinesis producer in Lambda
Need to dockerize hundred of cronjobs, which service would be the most suitable? (Batch or Fargate?)
Best way to implement log sharing with customers for logs that are on EC2 instances? Kinesis? Cloudwatch with SNS?
Every AWS customer ever. . .
CloudFormation - Is it possible to use stack variables in TemplateURL parameter?
With Savings Plans, how about we include Load Balancers in there too?
Fargate price calculation check
Should I stop or terminate ec2 instances to save money overnight?
Automating your ECS container architecture deployments with ECS ComposeX | Amazon Web Services
Serverless GitLab CI/CD on AWS Fargate
ECS - Updating service with new image uploaded to ECR
creating python server in AWS without breaching free tier
ECS Container Deployments: Hands down the absolute best article I've found to explain ECS deployments. I wish more people read this article!
PostgREST on Fargate
AWS Lessons Learned from being DDOS'd (2021-02-13-#12394)
Using Amazon ECS Exec to access your containers on AWS Fargate and Amazon EC2
AWS Fargate for running a Django application
Push directly to ECS via Gitlab or with separate infrastructure pipeline?
CDK - ECS task can't access S3 bucket
[Long]: Fargate container errors when trying to upload image to S3: `Inaccessible host: `<bucket_name>.s3.'. This service may not be available in the `{"AWS_REGION":"eu-west-2"}' region.`
Automate ECS Service with ECR Push?
suggestions for deployment patterns to EC2 using Docker
Running Grafana Loki on AWS
Best Way to temporarily stop a Fargate Service?
AWS Fargate multiple tasks causing regional data transfer costs
How to get output for boto3 ecs execute_command. I have a bounty on this, but thought reddit would be a good place to ask.
Is there a way to get a notification when an IAM policy denies an action?
When using Amazon ECS and AWS Fargate, what is the difference between the task role and the task execution role?
Architecture for highly customized alerts - DynamoDB inserts to SNS topics/subscriptions
AWS Fargate: can it quickly scale up worker tasks in response to changes in a redis queue?
AWS CDK 2.25.0 -- Unable to set an SNS topic as the destination for S3 bucket event notifications.
Amazon ECS simplifies Capacity Provider integration with Auto Scaling groups
Large number of lambdas project
Do ECS clusters cost money when no tasks are running?
Confused with ECS and how it deploys
Why I recommended ECS instead of Kubernetes to my latest customer
Considerations for Express.js backend
What's the most you have accidentally spent on AWS?
How to find the IPv4:InUseAddresses?
What is the benefit o Fargate vs directly using ECS or EKS?
Implementing PubSub using AWS SNS and SQS
ECS Running Service Terraform vs Console
Best Practices Scaling Services in ECS on Business Hours
Best way to reduce IP range of AWS Fargate?
Secrets manager for Lambda
Debating EC2 vs Fargate for EKS
Using SES to send emails on behalf of customers, but I want to be able to send that customer a notification when that email is opened
What’s your workflow and infra for deploying a React app?
ECS Fargate, ELB Health checks failing
Lessons Learned Migrating a Startup from EC2 to ECS Fargate
Multi container Fargate task
EC2 vs ECS billing for low to medium usage.
Where are the interesting announcements?
What is a cluster trying to abstract exactly?
Scheduling GPUs for deep learning tasks on Amazon ECS
Worldwide SMS for SNS now available
Passed SAA-C02
Tips for starting a career in the Cloud
Recommended study material for AWS Cloud Practitioner Certification?
ECS vs EKS practice question help
Failed the SAA-C03 exam. Dammit!
Passed SAA-C03 wih 833! Need advice on which one next and effort needed for SAP-C02
Passed !!! SAA-C03 !!!
SAA-C03 - ECS vs. EKS vs EC2?
Extremely bad experience with Pearson Vue application
Just Passed SAA-C03!
Passed the Security Specialty Exam Yesterday
Limit 1 task per ECS instance?
Cleaning failed containers AWS ECS
Amazon ECS team will do an AMA tomorrow over at /r/IAmA
CloudWatch Logs agent on ECS instances
CloudHub (multi-remote sites) with pfSense applications

Look into Step Functions.

Be careful of time limits on lambdas when doing something heavy like video processing. Was doing the extraction part of an ETL workflow, had to move it to Batch, triggered by a lambda.

Currently it’s just video uploading. Moderation of video is separate flow which required frame evaluation.

By decoupling the video from your main workflow, you’re actually improving your ability to handle load, not making it more likely you will have disruptions. The important thing is to log out your workload to something like SQS, and have Lambda consume that. Then you are golden.

It seems unlikely you are going to run into 10 minute + video jobs. If you do, sure, you can run longer tasks on ECS consuming the same SQS queue. But that’s an elaboration of what you’re proposing, which will solve the problem you describe. Go for it.

The ingest process on a media archive is perfect for lambda - it’s what we did on the Paramount project. Working with CBS and later Viacom and Paramount on this project was one of the highlights of my time at AWS

https://youtu.be/6wPCHw1Pdxo?si=PaN01iV5ZZ0FDGWa

The big thing that really makes it work is building a queued platform for QC, normalization and especially video encoding / transcoding.

How long does the actual video processing take, on average, on a 0.5 vCPU container?

Lambda is great for event-driven, unpredictable workloads, and it’s cheap because you don’t pay for idle. But once you start to do CPU-heavy stuff, you need to know that Lambda, on a per-CPU-cycle basis, is about eight times as expensive as a comparable EC2 instance, and something like 3-4 times as expensive as a comparable Fargate container. Plus, with EC2 and Fargate you can possibly use the spot market for even bigger savings.

What I would do in your case is this:

Yes, do the decoupling. Any video that needs to be processed goes into an SQS queue, and you have a separate task that processes that video. Don’t process the video in your main 24/7 Flask container.

For the video processing task itself, you have a few choices. The quickest to implement would be to have a Lambda that’s triggered from the SQS queue that performs the processing. But it won’t be the cheapest solution.

A dedicated ECS container or EC2 instance that pulls the same work from the queue and processes the videos is slightly more difficult to setup, but will be cheaper in the long run.

And you can combine these approaches: Have a Lambda with limited concurrency read from the queue. This is your base solution. You then setup an ECS or EC2 Auto Scaling Group with minimum capacity zero, and triggered by the queue depth of the SQS queue: If more work gets into the queue that your limiited-concurrency Lambda can handle, you spin up one (or a few) ECS or EC2 instances for additional processing capacity. Using the spot market if possible. Scale the cluster back to zero if the queue depth is below the value that your Lambda can handle on its own.

Wrap all this in a Step Function or with the new Lambda Durable Functions so that whenever the task is finished, you can provide feedback to your users.

Oh, and for the video uploading, don’t let your users do this in a synchronous API call. That’s way too much data to handle. Your Flask container can easily generate an S3 Signed URL that you give to your users, that dumps the upload directly into an S3 bucket.

Look at Temporal as well for this whole workflow. They solved all the problems with this basically

You would also have to be careful about lambda memory limits. If you’re reading the video to memory for uploads, this is a problem. Where are you storing the videos? S3?

And uploading means the time is not under your control: a slow client on the far end of a satellite link could get failed uploads that they then blame on you.

overkill, tbh.

Ain’t lambda environments smaller for video transcoding and stuff?

Ideal option you suggested this in future radar. Thanks for details feedback.

But once you start to do CPU-heavy stuff, you need to know that Lambda, on a per-CPU-cycle basis, is about eight times as expensive as a comparable EC2 instance, and something like 3-4 times as expensive as a comparable Fargate container. Plus, with EC2 and Fargate you can possibly use the spot market for even bigger savings.

Yes thats plan. I think I am on same path but we will evolve as when demand grow. Lambda Durable functions one of option in list yet to do experiment with it.

Oh, and for the video uploading, don’t let your users do this in a synchronous API call. That’s way too much data to handle. Your Flask container can easily generate an S3 Signed URL that you give to your users, that dumps the upload directly into an S3 bucket.

This is good option. Earlier company we used to generate STS token dynamically and dynamic secrete to upload from UI. Now this signed URL option is correct way to handle. I haven’t done but will try this also instead of moving video upload part completely to backend. is that what you are saying ?