Should I move away from serverless (Lambda)?

Right now I have a web app that I want users to be able to use without signing up for an account and having to use an API key for authorization to call my backend in order for a better UE.

The only authorization I am using is recaptcha so that malicious users can’t flood requests to my backend and make limitless calls to the 3rd party apis that I am using (so they can’t run my bill up).

But, with my current approach of using lambda functions, technically anyone could maliciously flood requests to my backend and run my AWS lambda bill up because my recpatcha validation script is present within the lambda itself, so even if they don’t get access to my 3rd party apis (will be denied without captcha validation), I would still be billed since the lambda is being invoked.

So, with all of this being said, would ec2 or even digitalocean be a better implementation and route even if the pricing is a bit higher due to the security risks presented by serverless?

You’re referring to a Denial of Wallet attack, the normal mitigation is putting your Lambda behind WAF via API gateway, which you should do regardless if it’s on an instance or Lambda.

You should use the built-in throttling and quota capabilities of API Gateway: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html

You can set a global limit, a per user limit, etc. The idea is that the API Gateway rejects the request before your Lambda is even invoked. You will still have to pay a small fee for the API Gateway usage.

If your web app is low volume, you could set up a concurrency limit on the function to reduce the risk. That would throttle the function. Or set up something with WAF to blocklist bad actors.

The risk on the EC2 side is either someone floods your instance with auth requests and it runs out of resources , or causes your autoscaling to scale out costing you money.

Changing solutions will just bring you other risks IMO. I’d look at a way to throttle or better Auth solution.

You have a few options

Build some lambda authorizer with your custom logic (limit by ip and etc)

Use AWS Waf with link to your front end (on fronted you put script on api gateway side choose Waf) and activate antibot rules.

Also you can put your Dns as proxy let’s say on Cloudflare and use their anti bot functions.

I addition to WAF that was recommended, please set up Budgets and alarms for when budgets are exceeded.

All these options generate costs when under high load, you pay an unbounded fee for not getting downtime.

Probably the only way to limit the costs (but probably still paying more, and at the cost of a partial outage) would be using a classic ELB in front of an ASG.

They both just charge for data out and a fixed hourly fee, so after the ASG behind the load balancer is brought at capacity by the traffic increase, the data out stops growing and so do your costs, but then some legitimate requests will be dropped.

This is what Shield and WAF are for. Shield for DDoS and WAF for DoS. You can chain rate limit rules in WAF together to limit how many requests per second a single IP can make. If you get a bad actor you can just block that IP with a WAF rule too.

AWS WAF. A firewall for your application specifically to prevent attacks and DDOS.

Put api gateway with rate limiting. Also use hCaptcha. Google reCaptcha is not great.

You could use api gateway or appsync with cognito authorization required to host your api and have your app dish out the required guest access from the unauthenticated role. It’s probably not fool proof, but for all intents and purposes it will cut out any nefarious spam API usage or ddos etc. It has in my experience.

I guess just don’t set an auto scaling mechanism

While several mitigations have been posted here, they all reduce the risk and the bill amounts, they don’t solve the problem. This is one big reason why AWS is great for large organizations but makes a terrible discount hosting provider. EC2 has many of the same problems.

Digital Ocean IS a reasonable choice for a discount provider that won’t leave you with a massive surprise bill.

Never make a lambda publicly invokable. Put an api gateway in front of it with some form of authentication.

There is no such thing as a security risk on what you are describing. You can host such a “Web” app anywhere. If you are concerned about getting billed for flood requests then you should start evaluating rate-limiting for some metric on access.

This is the way

You need to set up a WAF and you’ll be fine.

We have had a large DDoS/Denial of Wallet attack and managed it via lambda via:

  • First layer is Cloudflare using bot protection. This cut 75% of the illegitimate traffic
  • AWS WAF via API Gateway - this cut the other 25%. I’d imagine using only WAF would also have worked just as well instead of CF for the first layer but it’s more expensive.
  • Moved all partner requests to using an API Gateway Usage Plan + API Key with generous but affordable (for us) limits

The attack cost us ~$30,000 which AWS gave us back once we proved we’d taken appropriate steps to mitigate future attacks.

Truthfully using a server vs lambda isn’t really going to protect you. Attack mitigation is a multilayer problem generally speaking, some of which can be handled at your infrastructure layer and some can be, but probably shouldn’t be, handled in your app.

Wow. Thank you for this! It’s good to know these things and I’m sorry this happened to you.

Thanks!