AWS offers a mix of compute and storage resources. When combined, these resources create an efficient infrastructure to enable almost any type of enterprise application. Inevitably, this abundance of options leads to confusion.
Cloud developers can select Amazon Elastic Compute Cloud (EC2) virtual machines and event-driven AWS Lambda computing services. Each platform is suited to specific purposes, which makes the selection of the right one an essential part of cloud application design.
Let's compare Amazon EC2 vs. AWS Lambda.
What is Amazon EC2?
Amazon EC2 is a cloud computing service designed to provide VMs called instances. AWS users can select an instance with the desired amount of computing resources, such as the number and type of CPUs, memory and local storage.
AWS offers almost 400 instance types that use a mix of processors, including those from Intel and AMD. The main instance types are:
- general purpose
- compute optimized
- memory optimized
- accelerated computing
- storage optimized
Let's say a user needs simple, general-purpose EC2 instances. A t3.medium instance, for example, would provide two virtual CPUs (vCPUs), 4 GB of memory, access to AWS Elastic Block Storage (EBS) and up to 5 Gbps network capacity. More complex applications might require a c5.4xlarge instance with 16 vCPUs, 32 MB of memory and up to 10 Gbps of network capacity. Cloud engineers or architects decide which EC2 instances best match a particular workload. They'll also need to consider how to connect EC2 instances with other AWS services, such as storage and monitoring, to create a complete cloud infrastructure capable of hosting the intended workload.
EC2 instances are generally intended for consistent, long-term operation that can typically last from weeks to years. While admins can scale EC2 instances, the time required to build out an EC2 infrastructure is best for static or constant usage. Users pay for EC2 instances as a recurring monthly charge.
What is AWS Lambda?
AWS Lambda is an event-based service that delivers extremely short-term compute capability. It is designed to run code without the need to deliberately deploy, use or manage VM instances. An organization would use it to address specific triggers or events, such as database updates, storage changes or custom events generated from other applications.
In a typical process, a developer would write code and upload it to the Lambda service. When a trigger event occurs, AWS automatically loads and runs the code. More than 200 AWS services can trigger this code, which is called a Lambda function. Developers can also directly call or trigger the code. This model is often called serverless computing, or function-as-a-service.
AWS Lambda is ephemeral in nature; it only runs when triggered. The triggered task could involve, for example, processing data that was just uploaded to the cloud. The upload -- seen as storage activity -- directs the Lambda function to operate on the data. When the task is completed, the function stops and frees computing resources from the AWS infrastructure. When another event arrives, the process repeats.
While EC2 users must consider the resource needs of their code, Lambda users are constrained only by a set of restrictions, such as 50 MB file size limits and inbound/outbound network traffic restrictions.
EC2 vs. Lambda use cases
EC2 is often the default choice for straightforward application architectures or when migrating traditional monolithic applications from a local data center to the cloud. EC2 instances are generally intended for consistent, long-term operations that typically last from weeks to years.
Because it exists and runs only until it completes an associated task, a Lambda function is well-suited to handle specific responses to events.
EC2 vs. Lambda management
With EC2, users have complete control over the choice and configuration of OSes and network settings, as well as drivers and other supporting software. Users take full responsibility for infrastructure and application management, including capacity planning, provisioning, application health and performance, fault tolerance and scalability.
With Lambda, AWS handles the back-end provisioning, loading, execution, scaling and unloading of the user's code. This effectively relieves users of all infrastructure concerns. Users cannot access or manage the Lambda infrastructure.
Because the concept of serverless computing is fundamentally inconsistent with traditional software designs, organizations generally use Lambda in major software redesigns or for new, highly scalable cloud-native software architectures. These architectures can embrace such function-as-a-service concepts in the code's foundation.
The level of control in EC2 extends to security. Admins must implement satisfactory security settings for the EC2 instance and the application. This might include using a firewall to control traffic, creating IAM roles, specifying permissions and setting up security groups.
Lambda security is more straightforward than EC2. Admins can use IAM roles to define Lambda access and allow connections to other AWS services. They can also implement encryption between Lambda functions and S3 or an API gateway. However, AWS handles the security updates and patches for the infrastructure.
EC2 vs. Lambda pricing
EC2 pricing follows a pay-per-use model, and specific pricing depends on the instance type. Instances consume consistent resources and impose a regular cost, regardless of how much work a workload runs.
There are five options to pay for EC2 instances:
- On-Demand. A pay-as-you-go model with no long-term commitment
- Spot Instances. Users purchase unused EC2 capacity at a lower rate than on-demand pricing
- Savings Plans. Commitment of usage for a one- or three-year period at a discount
- Reserved Instances. A capacity reservation for a one- or three-year commitment at a discount
- Dedicated Hosts. Enterprises can use eligible software licenses, including those from Microsoft and Oracle, to pay for a dedicated EC2 server
Unlike EC2, Lambda charges only for the compute time consumed and the number of requests made. The cost of Lambda compute time represents measurable work from the Lambda service -- execution to termination. When code is not running, there is no cost to a customer.
For both services, users can estimate their costs through the AWS pricing calculator.
Use EC2 and Lambda together
Lambda is ideal for short-term tasks. EC2, in contrast, is intended for long-term, steady-state operations, such as running websites and applications or supporting resilient high-performance computing. Even so, both platforms can work together.
Developers can draw on the benefits and strengths of both platforms to architect a complete application. For example, Lambda can use events to trigger activities that create, start or stop EC2 instances.
As another example, users can deploy Lambda to analyze or transform a data set uploaded to S3, and then allow applications in EC2 to use or render that manipulated data.