Based on the discussion in the comments I'm now adding a complete answer to this, as this is not really suitable for a comment.
You mentioned that your current service is already running on EC2 and you'd like to move that over to a Serverless solution. Furthermore you mention the options of "Lambda or Serverless exposed via API-Gateway". Then you add some additional information about expecting a response time of 500ms and the Lambda doing 2 DynamoDB calls.
I'll address these points in order:
EC2 vs Serverless Solution:
You seem to have already decided on trying the Serverless route, which works quite well in principle for a Microservice-Type architecture you're describing. I'm not going to focus too much on the merits of the EC2 solution here. Going serverless can have the following benefits (among others):
- Cost effectiveness: You pay only for the resources your code consumes while it's running and not for idle times
- Scalability: Lambda scales horizontally, fast and effortlessly - you basically don't worry about it (up to 1000 parallel "instances")
- Lower operational overhead: No need to patch operating systems - AWS takes care of that for you
- Focus on your business logic, leave the heavy lifting of managing the infrastructure to AWS
Lambda or Serverless exposed via API-Gateway
Serverless isn't really an AWS Service but a paradigm or architectural pattern so these options don't completely make sense - you'd use the API Gateway to trigger Lambda functions whenever an Event (read: HTTP-Request) occurs. This means you'll setup a fully-managed REST-Endpoint (API-Gateway) to call your code (Lambda) on demand.
On Performance
A response time of 500ms is realistic for the use case you're describing - DynamoDB advertises single-digit-millisecond latency, so two calls to it within 500ms shouldn't be a problem. Unfortunately Lambda cold-start is a thing. Lambda scales out with parallel requests, meaning a new Micro-VM gets provisioned if there aren't enough warm instances of your function available to serve your request. This takes time, but in your use-case this shouldn't be an issue, since you don't need access to a VPC (in that case it would take multiple seconds).
Lambda is limited in performance compared to EC2 instances, you scale the amount of performance Lambda provides by specifying the amount of RAM the function gets allocated (CPU resources are provided based on the RAM). For a simple Login-Service this shouldn't be an issue as well.
I suggest you read up on the points I mentioned in the Lambda documentation (which is quite good).
Answer from Maurice on Stack OverflowBased on the discussion in the comments I'm now adding a complete answer to this, as this is not really suitable for a comment.
You mentioned that your current service is already running on EC2 and you'd like to move that over to a Serverless solution. Furthermore you mention the options of "Lambda or Serverless exposed via API-Gateway". Then you add some additional information about expecting a response time of 500ms and the Lambda doing 2 DynamoDB calls.
I'll address these points in order:
EC2 vs Serverless Solution:
You seem to have already decided on trying the Serverless route, which works quite well in principle for a Microservice-Type architecture you're describing. I'm not going to focus too much on the merits of the EC2 solution here. Going serverless can have the following benefits (among others):
- Cost effectiveness: You pay only for the resources your code consumes while it's running and not for idle times
- Scalability: Lambda scales horizontally, fast and effortlessly - you basically don't worry about it (up to 1000 parallel "instances")
- Lower operational overhead: No need to patch operating systems - AWS takes care of that for you
- Focus on your business logic, leave the heavy lifting of managing the infrastructure to AWS
Lambda or Serverless exposed via API-Gateway
Serverless isn't really an AWS Service but a paradigm or architectural pattern so these options don't completely make sense - you'd use the API Gateway to trigger Lambda functions whenever an Event (read: HTTP-Request) occurs. This means you'll setup a fully-managed REST-Endpoint (API-Gateway) to call your code (Lambda) on demand.
On Performance
A response time of 500ms is realistic for the use case you're describing - DynamoDB advertises single-digit-millisecond latency, so two calls to it within 500ms shouldn't be a problem. Unfortunately Lambda cold-start is a thing. Lambda scales out with parallel requests, meaning a new Micro-VM gets provisioned if there aren't enough warm instances of your function available to serve your request. This takes time, but in your use-case this shouldn't be an issue, since you don't need access to a VPC (in that case it would take multiple seconds).
Lambda is limited in performance compared to EC2 instances, you scale the amount of performance Lambda provides by specifying the amount of RAM the function gets allocated (CPU resources are provided based on the RAM). For a simple Login-Service this shouldn't be an issue as well.
I suggest you read up on the points I mentioned in the Lambda documentation (which is quite good).
If you want your events driven service managed use AWS Lambda, you just provide the code in the required language, and Amazon AWS does the rest. If you want to customise for your own needs and use whatever coding language you prefer Amazon EC2 offers flexibility and a whole range of EC2 Instance types to choose from, in conjunction with Elastic Beanstalk services for deploying onto Amazon EC2.
AWS Lambda is a service for running code in response to events, such as changes to data in an Amazon S3 bucket and Amazon DynamoDB tables, or as compute services to run your code in response to HTTP requests using Amazon API gateway or API calls made by using AWS SDKs. This is an ideal computing platform for applications when running within the standard runtime environment.
Lambda should be your best bet.
Why EC2, some points:
- More control
EC2 allows for more control on what is running on the hardware like specific tech versioning etc. EC2 allows for custom provisioning. Lambda is more hands off or automatic, fewer choices but less to manage, provision and scale.
You want some sort of data recovery. EC2 allow you to recover data on the box.
You have a need for VPC or other security access restrictions and want to take advantage of built in firewalls for EC2.
In general Lambda's are an event driven architecture built on top of the AWS SDK. For example, I drop a message to a SQS queue and then want to do something with it. Some systems are more complex than firing and responding to events so an EC2 may be a better fit.
It depends on what you are trying to achieve. If you have a long running process or complex processing, EC2 may be a better fit. If I recall the process timeout for a lambda is 5 or 15 minutes. Not that a typical restful API call would take that long, but hopefully that illustrates some of the differences between the two approaches.
In a lot of cases, the EC2 infrastructure was migrated from an on-prem infrastructure, and EC2 maps better to their existing/previous server setup.
Hi everyone I have a question regarding cost between lambda and ec2 I am building a simple node application using puppeteer that will only act as an api. Obviously don’t expect heavy usage for now but overall Iv read that lambda will be cheaper to run up until heavy usage? Just wanted to get your thoughts if ec2 is better than lambda both in performance and cost. Again this will not be heavily used in either case. Also which ones performance will be better?
Hello everyone, I was studying for my AWS CSAA exam and while I’ve sightly used both services and played around with them in lab environments, I can’t wrap my head around one question. When would I use a EC2 over a Lambda when Lambda is much cheaper and still provides scalability for a backend solution. For most simple web apps, a REST API served thru Lambda seems like a great option (esp when utilizing AWS API Gateway). When would I directly use EC2 (as well as ELB, ASG,etc)? I guess I’m trying to understand stand when a “serverless” web app makes sense. Thanks everyone for your time
EDIT: added serverless clarification
Say that I have 5000 requests hitting an application every 5 seconds and I need to consume an object and write it to a database. Would a lambda with an http trigger that does an insert be more cost effective than having an ec2 instance that does the same thing?
Edit: Just want to say you all are awesome! I am coming from MSFT world and never experienced so many folks being so helpful. Makes me think I should have switched to AWS ages ago. Thanks everybody.
I'm trying to determine whether using lambdas or an EC2 instance is right for my use case. Here are the details:
We fetch our data from an external source, process it, then store it in our database. For the external source, they allow a maximum of one request every 20ms, or 50 requests per second. Most of the processing that calls this system can be done asynchronously, and there are small bursts every 15 minutes along with larger bursts when we trigger a data fetch.
Option 1 - EC2: I could set this up as a persistent server with Express or the like routing the requests to fetch the correct data from the provider. On the plus side, it would be very easy to throttle my requests to a max of one every 20ms. On the downside, it's a single instance that needs to handle burst traffic, and may slow down. In addition, I'd be paying for the instance to always be up, and if there were ever an uncaught exception, fetching our source data would be stopped until I brought the server back up.
Option 2 - Lambda: I could set this up as a lambda function with a maximum scaling number of something like 5 to 10. No matter how I throttled each lambda function, I still couldn't ensure that two functions wouldn't fire a request within a 20ms period, as they're not aware of each other. When this happens, I'd receive a spike arrest error from the provider, and would need to add handling in my code to retry the call. On the plus side, I could save money with lambdas since the traffic bursts before falling off to nearly zero. On the downside, it would add a little complexity to my code and potentially upset the provider if they need to send me regular spike arrest errors.
I'm new to AWS and have a feeling there are some things I'm not considering. Based on the above information, would you choose one of the above options or recommend something else?