Are you tired of configuring an EC2 machine while struggling to maintain, scale, monitor it? Are you also trying to optimise the price on your API server? If yes, then you are on the right page. Here we focus on two alternative solutions offered by AWS. In this blog, we introduce the two services and discuss how they are different from each other.
Lambda is an event-driven, serverless computing offering from AWS. Lambdas will run only when required, with little or no maintenance, automatic scaling, and so on. These characteristics make Lambda a Function as a Service ( FaaS ).
Elastic Beanstalk ( EB )
EB is another computing service provided by AWS that works as a wrapper over multiple AWS services to host long-running tasks like web servers, workers, etc. EB comes as a Platform as a Service ( PaaS ) wherein you have control over a platform.
Hosting an API
When it comes to hosting an API, it can be done on both Lambda ( Using API Gateway ) as well as EB with a server running 24/7.
Advantages of Elastic Beanstalk
- Easy Server Configuration
With a few clicks of a button, a full-fledged server can be deployed on EB. Basically, it will create and manage all the underlying AWS services that are required to run a web server. All the created resources can be accessed in the same AWS account where EB is hosted.
- Auto Scaling
Based on the set of rules, instances will be managed ( scaled up or scaled down). And these rules are mostly triggered through the traffic received.
- Application Monitoring
Continued monitoring will ensure that the server is up and running. If there is an issue, then appropriate alarms will be raised.
- Control on Platform
Users have the flexibility to tweak the platform settings. Be it in terms of resource allotment, access, security, or more.
Using Lambda Over Elastic Beanstalk
Lambda provides scaling, monitoring, and a few key good features that you cannot ignore.
Lambda is only charged when used. If the traffic on the API side is less or if it’s not continuous then you will pay less when compared to running EC2 instances. ( As EB creates EC2 instances under the hood ).
On an average, there will be 0.25% reduction in the pricing for Lambda as compared to EB. This number is subject to the traffic received.
Lambdas don’t require any maintenance when it comes to setting up the environment since it is taken care of by AWS.
- CPU Bound Tasks
If there are tasks that require more CPU resources, Lambdas are your best bet. Adding more memory proportionally increases the amount of CPU, increasing the overall computational power available. In the case of EB, since you must choose a larger instance, it will be an overkill for the rest of the application.
- API Gateway
APIs are hosted on an API gateway and they in turn trigger Lambda for processing. With API Gateway, users get some useful features like Requests, Response Validations, Authentication, Rate limiting, etc.
Limitations around Lambda
Here are some of the limitations in using Lambda along with the possible workarounds.
- Cold Start
This is the time taken by AWS to spin up the Lambda with the environment and the code. When freshly invoked, it will take some time. This adds latency,, if you are using Lambda for your APIs.
Cold start can be avoided in two ways:
- By having provisioned concurrency. To learn how to create low-latency, high-volume APIs with Provisioned Concurrency, go here.
- By pinging the Lambda continuously so that it stays hot for most of the time. Visit this link to know how it works.
- API Gateway timeout
Gateway expects Lambda to finish processing and send back the response to the client within 30 sec. If you need an API that consumes more than 30 sec, then you might have to break the logic into multiple Lambdas or you can try the approach discussed in this article.
If multiple lambdas are running in parallel and if they are creating new connections to the database, then the database may choke with too many connections. In such a case, you might have to restrict the number of concurrent Lambdas running. This will affect scaling. So you must find the right balance that lets you to not only scale but also have no issues while scaling.
- Payload Size
Since Lambda payloads have an upper limit of 10MB, you must make sure your HTTP requests are small. Here is a workaround for uploading large files for processing.
The most common problem of Lambda is that there is only 512 MB of storage available for processing. At the time of writing this blog, the storage limit has been increased to 10GB.
As we can see, while Elastic Beanstalk is good, Lambdas are even better in few of the areas as mentioned above.
Serverless is the future and Lambdas will keep improving, which in turn means little or no limitations.
It’s hard to conclude which is better off the two services. You must choose based on the features you require and their importance. If you don’t want any maintenance and want to pay only when the resources are used, then Lambda must be your first choice. However, if you need control over the platform and have a requirement for a long-running process, then Elastic Beanstalk is the better choice.