I wanted to share this experience so anyone reading this - especially beginner - can take a lesson from my mistake.
Recently I decided to create a project for my portfolio with AWS and Next JS . The project is using the new Media Stream API where users can take photos or videos with their device camera. The technology is fairly new so it doesn't support all browsers yet.
Since I'm dealing with images and videos, I'm using AWS S3 and I also want to compress the images before storing them on S3. So I choose to use AWS Lambda function where when I upload the image, S3 will triggered the Lambda function and resize the image if it's larger than a certain pixel in width. But the problem when taking an image with a camera that facing the user (front-camera) is that the result is flipping. Meaning that your left will be on the right and your right on the left so I need to re-flipping it again.
...And This Is Where The Loop Hell Begin
I might write the wrong code in the function, but honestly, I didn't know that it will create an infinite loop. What I write is if the image width is bigger than a certain pixel, then the image will be resized, flip and save... else the image will just be flip and save.
Nothing fancy, but this code is going to do an infinite loop. Until today I still can not fully understand how it will trigger the loop over and over again and cost me thousands of dollars. I thought that it will only run once when the image was uploaded to S3, triggered the Lambda function, and saved it. That's it. But boy I was wrong.
How To Avoid it?
There are a couple of ways to avoid this. I'm not an experienced developer regarding AWS services, but I found these options might avoid the infinite loop.
- Always Check your Lambda invocation . Whenever you change your function, always check its invocation and look at any irregularities. When the function invokes when you doing nothing then that is a sign that something may be off with your code.
- Set a budget alarm. You can set a budget alarm with your AWS account. I do set my budget to 10USD and they did email me when it gets to 80% of my budget. But unfortunately, I didn't check my email on the weekend and it costs me a thousand dollars.
- If you put S3 as a trigger, make sure you activate the bucket versioning for the S3 bucket . It can prevent the function to write over and called itself and write it over again.
- Another way is to test it by mocking it . I try my code at my local computer and the code runs just fine, so in my case, this is not very helpful at all. I do like how firebase functions have emulators so a developer can test their functions locally before pushing them to the cloud.
- Last option is to entirely delete your Lambda function/s or other services that triggered it. I do this since I don't have any clue what's wrong. Forum thread here .
Please be careful when you ' trying ' something new. Always check your bill everyday when you subscribed to a new services, even if you choose free tier and already submit your credit card with them, and lastly don't forget to check you email everyday too.
For everyone reading this, don't be afraid to keep exploring new technology, because there is a bunch of great technology out there that you should explore, but just keep in mind to always be careful and keep track in what you are doing.
My case is still in negotiation and investigation. I do receive some credits adjustment but it is still too much for me to pay. I'm still negotiating with the team. For now, I'm thinking of creating a tutorial in Udemy or maybe back to my old job, I don't really know what I'm going to do, just wish me luck.
Shameless Plug... ;)
HIRED ME... I need the money to pay AWS.
Why you should hire me? Well, I'm old, I'm switching career to become web wrench (yeah... that's what I called myself), I'm not graduated from a fancy Bootcamp, I don't have a CS degree, I've been rejected more than 100 times, and I'm not really self-taught, many people have taught me indirectly via free and paid courses.
So what's good? honestly, I still don't know much about this vast technology industry. I graduated with a Master's degree in Management (majoring in finance), I was a Project Manager in the manufacturing and poultry industry for 12 years. Built my own travel company and got shut down by COVID-19, I know a little bit of React, Svelte, React Native, I love using Styled-Components with Tailwind using Twin Macro, I know Firebase and AWS Amplify to build a full-stack application (I hate building everything from scratch with Node JS, PostgreSQL, ORM and else), and I'm on GMT+7, so I can code while others were asleep.
For those who still reading this, Thank you and go back to work! :)
That's it folks. Stay safe, keep on learning!