Certifications / Cloud

AWS Lambda vs EC2: Which to Use and When

AWS Lambda vs EC2: Which to Use and When picture: A
Follow us
Published on September 14, 2020

Amazon Web Services (AWS) is the most important development in computer technology since the microprocessor. Over 83% of workloads are now in the cloud as of 2020, and even the federal government is hopping on the bandwagon.

AWS comprises well over 1,000different services, each one specifically tailored to optimize your cloud experience. With a toolset that large, it's nearly impossible to know exactly what to use when and where.

Staring at the AWS console can be overwhelming for any user, especially those new to cloud development. However, in the same way you build a house (one brick at a time), you have to learn AWS one service at a time.

There are two AWS servicesthat are consistently ranked as the most used: Lambda and EC2. Both offer the user elastic computational resources, but it may not be immediately apparent which service best suits your given need.

Lambda specializes in code execution based on certain triggers, while EC2 is Amazon's core platform for cloud development. But, there is a bit more to it than that. That's what we'll explore in this post.

Why Do You Use AWS Lambdas?

It's important to first understand what problem AWS Lambda solves, not simply what it does.

Say you own an image sharing website. Any user is free to upload whatever image they want. More importantly, they can upload the images in any file format: .png, .jpeg, .gif, etc.

Your site has been up for a couple months so far, and traffic is increasing and more images are being uploaded to your server. Now all you have to do is convert them to JPEGs and store them on your database. Code is written by you (or your developer) to do just that. It's then deployed to your dedicated server.

If your dedicated server is in your basement, bedroom, or server room, then its RAM is finite. It may even be enough RAM…on slow days; but as traffic increases, your file conversion code will stall and eventually give users the dreaded 500 Error Code.

Now your site's down at the busiest time of the week… the worst time possible. All because some lines of code received far more requests than they could process. Thanks to server limitations, users are leaving your site, along with the revenue they bring.

So, what to do? More RAM installed on the server might help for awhile, until the site increases in popularity. Then it's back to square one. Maybe just buy a whole new server as a backup in case those few lines of code break. None of these solutions are efficient, and cause a lot of stress and guesswork. That is the problem Lambda has fixed.

Lambda allows you to deploy code to the cloud, without worrying about the inherent unpredictabilities of server management. Gone are the days of staring anxiously at your server, praying it won't crash. With Lambda, you simply deploy the code and let the cloud take care of the heavy lifting.

What is an AWS Lambda?

AWS Lambda is the perfect solution for any event-driven situation. Event driven means that the code is executed based on some external trigger. For instance, maybe you want a lambda to trigger when a file is uploaded to the cloud. Or maybe you want a particular lambda to run every time an API is called. Lastly, the lambda could be triggered by a cron job that runs every Sunday at midnight for example.

All three of these situations are examples of event-driven programming, and consequently perfect candidates for Lambda. The key thing to remember for Lambda: All Lambda does is upload code to the cloud, and have that code trigger on a given event.

Lambda is an excellent tool with regards to event driven programming. Just deploy the code, and you're all set. However, there are situations that call for broader requirements.

For example, what if you need to have your own server to house a website, a database, a load balancer, and more? Well that is a different, albeit similar tool called EC2, which stands for Elastic Cloud Computing.

Everyone Uses EC2 (Literally)

It's no secret that EC2 is the foundational piece of AWS. It is used by 100% of AWS users, and is a critical component of cloud computing. But what problem does EC2 solve?

EC2 allows users to move all their computations to the cloud. On an EC2 instance, memory scales out to meet the demand, and scales back in once the demand lowers.

In addition to predictable and reliable resources, companies that do not specialize in database management (which is pretty much every company) no longer have to hire employees to take care of these legacy systems.

What is EC2?

EC2 scales out resources when demand increases, and scales in as demand decreases. That way, you are only paying for resources you use, not for the resources you might use. This is the exact same principle as Lambda.

EC2 gives you the option to pick the operating system and which AWS services will be installed onto it. Amazon has made it easy however, by allowing the user to pick from AMI's—or Amazon Machine Images. These are pre-installed EC2 instances that come with different choices of operating systems, memory allocations, and services.

Maybe you want an EC2 solely dedicated to running a SQL database, there is an AMI for that. If your company wants to focus on machine learning, there is an AMI for that too. Amazon provides a wide variety of preconfigured EC2 instances to meet virtually every business need on the market.

Lambda vs. EC2: How to Choose

As you can see, Lambda and EC2 have clear differences. Neither one is better than the other, but there are situations where one is more suited than the other. Here are a couple more examples of when to use one over the other.

Lambda is most often used when data manipulation needs to occur on a consistent basis. Let's say that you need to upload customer data into the cloud every Monday night, but you need to obfuscate sensitive material when it lands in the S3 bucket. A lambda function would be a perfect solution.

As soon as the record is placed in the S3 bucket, a lambda function will recognize that event as something called a trigger. Once that trigger is pulled, the lambda will loop through the bucket and mask the data. If the program fails for some reason, the file will be removed from the S3 bucket.

If you find yourself with a similar scenario as the one described above, use Lambda. Don't spin up a new EC2 instance. Lambda charges the customer per request and duration.

EC2, on the other hand, charges the customer for every second the instance is running. If the function triggers every Monday night, then lambda will ensure you are only charged every Monday night.

EC2 is ideal for creating websites. (If it is a WordPress site though, I'd recommend AWS LightSail.) You would spin up an EC2 instance, and on that instance there could be a NoSQL database such as DynamoDB to store user data. Load balancers and gateways could also be placed onto the instance to grant access to the internet.

The best part is that an AMI can be created so you can reuse these resources and configurations later. This is something Lambda simply cannot do. Unless you were creating the most basic web application, an EC2 instance is going to be your best bet for web development. Having an AMI that emulates an existing website is also a great strategy for disaster recovery.

EC2 instances have even been used exclusively to play PC games, all you would have to do is increase the GPU on your instance, assign it Windows OS, and you would be all set. In short, EC2 allows you to create an entire ecosystem dedicated to some particular task.

Wrapping Up

One of the most challenging aspects of using AWS is figuring out which tools to useand where. Lambda is perfect for event driven programming, while EC2 is ideal when you need a more tailored solution to fit your needs.

EC2 will give you an actual OS to work with, such as Linux or Windows, while Lambda will simply perform a function as a service. EC2 can also provide you with AMI that can be spun up in disaster recovery situations. EC2 and Lambda are both vital tools in your toolset, and now you should have a better understanding of when to use one over the other.


Download

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.


Don't miss out!Get great content
delivered to your inbox.

By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.

Recommended Articles

Get CBT Nuggets IT training news and resources

I have read and understood the privacy policy and am able to consent to it.

© 2024 CBT Nuggets. All rights reserved.Terms | Privacy Policy | Accessibility | Sitemap | 2850 Crescent Avenue, Eugene, OR 97408 | 541-284-5522