Is there any option to prevent this? My intent when doing the app was as a showcase for my portfolio (trying to get my first job as a dev) so the link will be “out there” in public for everyone to see and I would like to have some sort of control regarding how much my users write data in the AWS services I´m using (my particular concern is S3, since photos nowdays are so heavy).
Lambda could be setup to read cloudwatch. I’m guessing all users are in the same bucket but under different prefixes. So if a certain prefix has more than the allowed volume of objects, the lambda reading from cloudwatch could trigger a second lambda to delete the oldest objects.
As mentioned previously, this should be handled by the application. Here’s a thought:
A user has one account. The account has a column that tracks total_user_data. When an image is uploaded/deleted by the user, add or subtract the photo size to/from the account’s total_user_data column:
This is going to be expensive. It’s either going require n S3 buckets which you shouldn’t do, the paid metrics for each prefix, or scanning the bucket each time.
Its better to calculate the size as you ingest new images.
First and foremost – how are users uploading images/objects to S3? Do they have to login? Do they upload directly to S3 from their browsers or are you using a proxy (e.g. Api Gateway, ALB) that triggers some backend code that process the file and saves it to S3?
One way I can think of from top of my head at the moment is presigned url with separate lambda function to generate presigned url with custom policy and record stored meta data such as client IpAddress and request counter in DynamoDB.
Your application can invoke or callback to alb endpoint target group attached to labmda and pass request with predefined parameters so that helper function can query DynamoDB for client IpAddress and counter for number of request made so far from that IpAddress).
Another method is to use API Gateway instead of attaching the lambda function to ALB. API Gateway can be adjusted to limit throttle as well as lambda invocation rate.
it’s pretty crude idea based on rate limiting by number of presigned url generation, but you get the gist of it.
But at the end of the day, it would be either doing all in your stack or aws resource do it for you. Regardless it would all come down to which would be most cost effective. $0.023 per GB upload to S3 or $0.0225 per hour ALB with $0.20 per million lambda requests and however-much total data-ingress payload.
What are you trying to showcase in your portfolio with your app? Is it the uploader itself? The UX? You can set you bucket to delete objects after a certain amount of time. If it is not a production app then no need to keep them forever. And simply set limits on what you are allowed to upload.
I’d recommend setting up a lifecycle policy on the bucket to delete objects after x days if you don’t need to store the object for long term. It’ll keep your cost down and simple to implement.
If you do this based on submitted upload size/decoupled from S3, you might get a little bit off sometimes but you’ll probably save some money by not needing to enable the deeper logging via cloudwatch etc. that will enable you to make decisions based of that. +/- a few megs per user is almost certainly cheaper than logging/infra to enable deeper S3 inspection.