Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How To Prevent AWS Lambda Abuse by 3rd-party apps

Very interested in getting hands-on with Serverless in 2018. Already looking to implement usage of AWS Lambda in several decentralized app projects. However, I don't yet understand how you can prevent abuse of your endpoint from a 3rd-party app (perhaps even a competitor), from driving up your usage costs.

I'm not talking about a DDoS, or where all the traffic is coming from a single IP, which can happen on any network, but specifically having a 3rd-party app's customers directly make the REST calls, which cause your usage costs to rise, because their app is piggy-backing on your "open" endpoints.

For example:

I wish to create an endpoint on AWS Lambda to give me the current price of Ethereum ETH/USD. What would prevent another (or every) dapp developer from using MY lambda endpoint and causing excessive billing charges to my account?

like image 299
nyusternie Avatar asked Jan 02 '18 01:01

nyusternie


People also ask

How do I stop Lambda throttling?

Lambda function throttling can be prevented by a few techniques, it can be done by requesting a higher limit of concurrent executions for your AWS account. You can increase your account's concurrency limit at no cost, this can prevent your application from lambda function throttling but cannot eliminate it.

Which tools are used for monitoring and troubleshooting Lambda applications?

AWS Lambda integrates with other AWS services to help you monitor and troubleshoot your Lambda functions. Lambda automatically monitors Lambda functions on your behalf and reports metrics through Amazon CloudWatch.


2 Answers

When you deploy an endpoint that is open to the world, you're opening it to be used, but also to be abused.

AWS provides services to avoid common abuse methods, such as AWS Shield, which mitigates against DDoS, etc., however, they do not know what is or is not abuse of your Lambda function, as you are asking.

If your Lambda function is private, then you should use one of the API gateway security mechanisms to prevent abuse:

  • IAM security
  • API key security
  • Custom security authorization

With one of these in place, your Lambda function can only by called by authorized users. Without one of these in place, there is no way to prevent the type of abuse you're concerned about.

like image 147
Matt Houser Avatar answered Nov 20 '22 14:11

Matt Houser


@Matt answer is correct, yet incomplete.

Adding a security layer is a necessary step towards security, but doesn't protect you from authenticated callers, as @Rodrigo's answer states.

I actually just encountered - and solved - this issue on one of my lambda, thanks to this article: https://itnext.io/the-everything-guide-to-lambda-throttling-reserved-concurrency-and-execution-limits-d64f144129e5

Basically, I added a single line on my serverless.yml file, in my function that gets called by the said authirized 3rd party:

reservedConcurrency: 1

And here goes the whole function:

refresh-cache:
    handler: src/functions/refresh-cache.refreshCache
    # XXX Ensures the lambda always has one slot available, and never use more than one lambda instance at once.
    #  Avoids GraphCMS webhooks to abuse our lambda (GCMS will trigger the webhook once per create/update/delete operation)
    #  This makes sure only one instance of that lambda can run at once, to avoid refreshing the cache with parallel runs
    #  Avoid spawning tons of API calls (most of them would timeout anyway, around 80%)
    #  See https://itnext.io/the-everything-guide-to-lambda-throttling-reserved-concurrency-and-execution-limits-d64f144129e5
    reservedConcurrency: 1
    events:
      - http:
          method: POST
          path: /refresh-cache
          cors: true

The refresh-cache lambda was invoked by a webhook triggered by a third party service when any data change. When importing a dataset, it would for instance trigger as much as 100 calls to refresh-cache. This behaviour was completely spamming my API, which in turn was running requests to other services in order to perform a cache invalidation.

Adding this single line improved the situation a lot, because only one instance of the lambda was running at once (no concurrent run), the number of calls was divided by ~10, instead of 50 calls to refresh-cache, it only triggered 3-4, and all those call worked (200 instead of 500 due to timeout issue).

Overall, pretty good. Not yet perfect for my workflow, but a step forward.


Not related, but I used https://epsagon.com/ which tremendously helped me figuring out what was happening on AWS Lambda. Here is what I got:

Before applying reservedConcurrency limit to the lambda: enter image description here

You can see that most calls fail with timeout (30000ms), only the few first succeed because the lambda isn't overloaded yet.

After applying reservedConcurrency limit to the lambda: enter image description here

You can see that all calls succeed, and they are much faster. No timeout. Saves both money, and time.


Using reservedConcurrency is not the only way to deal with this issue, there are many other, as @Rodrigo stated in his answer. But it's a working one, that may fit in your workflow. It's applied on the Lambda level, not on API Gateway (if I understand the docs correctly).

like image 23
Vadorequest Avatar answered Nov 20 '22 13:11

Vadorequest