Awesome Cloud — Serverless (Lambda) vs Containers (Kubernetes)
Published: 2022-07-01 16:41:27

Comparisons: Serverless vs Containers (Lambda vs Kubernetes). How to choose?



## TL;DR:
Serverless and Containers have some high-level similarities. They eliminate complexity and make it easier to deploy and scale applications.

Serverless works well if you need to perform relatively simple processing of events without maintaining underlying infrastructure. Containers are the ideal choice if you need full control over the application hosting environment.

## What is Serverless?
Serverless computing allows you to build and run applications and services without thinking about servers. Serverless applications don’t require you to provision, scale, and manage any servers. You can build them for nearly any type of application or backend service, and everything required to run and scale your application with high availability is handled for you.

Building serverless applications means that you can focus on your core product instead of worrying about managing and operating servers or runtimes. You are only responsible for providing the serverless function and are not aware of the underlying compute resources. The serverless runtime provisions server resources automatically, and customers are billed according to the number of times and the duration their function actually ran. Serverless is a model of computing that runs code on-demand without the need to provision or manage infrastructure. Development teams simply deploy their code on a serverless platform and are only charged when that code runs and consumes server resources.

## What are Containers?
Containers provide a standard way to package your application’s code, configurations, and dependencies into a single object. Containers share an operating system installed on the server and run as resource-isolated processes, ensuring quick, reliable, and consistent deployments, regardless of environment.

Containers are lightweight and provide a consistent, portable software environment for applications to easily run and scale anywhere. Containers make it easier to manage your underlying infrastructure, whether on-premises or in the cloud, so you can focus on innovation and your business needs.

Container orchestration (such as Kubernetes) automates the scheduling, development, networking, scaling, health monitoring, and management of your containers.

## Similarities: Serverless (Lambda) and Containers (Kubernetes)
Serverless and Containers are not identical technologies. However, they provide certain overlapping functionality:

Both are more efficient than virtual machines.
Both allow you to deploy application code.
Both abstract applications away from the host environment.
## Key Differences: Serverless (Lambda) vs Containers (Kubernetes)
**General Difference**
**Serverless**

No servers to provision or manage.
Automatically scale with usage.
Pay as you use (never pay for idle).
Highly available.
Container

Standard unit of software that package up the code and all its dependencies.
Application runs quickly and reliably from one computing environment to another.

## Environment
**Serverless**

- Underlying infrastructure managed by Cloud Provider. You cannot choose infrastructure own your own.
- No Patching headache.
- Can’t install software (e.g. WebServer, AppServer, Custom Software) in underlying environment.
- Code libraries can be installed. (e.g. npm, NuGet packages)

## Container

- Complete control of environment. You control underlying infrastructure configuration such as VM Size, OS, AMI, etc.
- Requires management (e.g. patching, updates) and orchestration.
- Install almost any software.
- Prepackaged images with different software available.

## Resource Configuration
**Serverless**

- Allocate memory and CPU proportionally.
- Choose memory from 128 MB to 10 GB.
- CPU cores are allocated proportionally between 1 to 6 cores.
- Limited deployment package size: 250 MB - unzipped, 50 MB - zipped.
- No attached hard disk.

**Container**

You can configure memory and CPU allocation as per need.
Can choose underlying EC2 instance type appropriate to workload.
AWS Lambda Supports Container Image

## Scaling
**Serverless**

- No scaling configuration is required. Scales automatically.
- Usually, each request connection invokes a new Lambda instance.
- Control using concurrency limit.

**Container**

- One Pod can serve more than one connection.
- Configure scaling using HPA (Horizontal Pod Autoscaling) and/or CA (Cluster Autoscaling).

##  Cost

**Serverless**

- With serverless computing, you only pay for what you use (Pay-as-you-use).
- For highly burstable workloads, serverless can lead to significant cost savings. For workloads with consistent demand, serverless may not make much difference (or sometime it is more costlier).
**Container**

- With containers, you pay for them as long as they’re on (Pay-as-you-go). Containers are constantly running in most of the cases.
- AWS Fargate Serverless compute for containers.


## Run Duration

**Serverless**

- Serverless functions typically run for a short period of time (minutes or seconds) and are shut down as soon as they finish processing the current event.
- Maximum runtime: 15 minutes (900 seconds).

**Container**

- No runtime time limit constraints. Containers can run for prolonged periods of time.
- Integration with other services

**Serverless**

- (AWS Lambda) Natively integrated with S3, SNS, SQS, and many other AWS services.
**Container**

- (Amazon EKS) Integrated with a few AWS services, such as IAM, ALB with help of Ingress.


##  High Availability

**Serverless**

- Lambda is inherently highly available, out of the box. (Each Lambda deployed on Multi-AZ automatically)
- No need for a Load Balancer.
**Container**

- You need to ensure high availability.

## Logging and Monitoring

**Serverless**

- Integrated with CloudWatch for monitoring.
- Logs go to CloudWatch out of the box.
- Logs can be sent to other logging systems using Lambda Extensions.

**Container**

- (For Kubernetes) Fluentbit/Fluentd can be used to send logs to CloudWatch, S3, ES, Splunk, Datadog, etc.
- For monitoring, you can install an agent to work with Prometheus, Grafana, etc.

## Portability/Vendor lock-in

**Serverless**

- With serverless, You are highly dependent on the platform that runs your code.

**Container**

- Containers can run anywhere. You just need container runtime installed.
- For example, using AWS Lambda functions makes an app more dependent on the AWS platform, while with Docker containers can be deployed on any platform that can run Docker.

## Supported languages

**Serverless**

- To run an application in a serverless model, the serverless runtime must explicitly support that language (different platforms support different languages).

**Container**

- Applications can be containerized as long as the underlying host server supports the language they are written in.

## Development and Testability

**Serverless**

- Serverless is more difficult to run outside a cloud environment. You are limited to the cloud platform running functions.
- Can’t perform the same level of testing against serverless functions. Local serverless frameworks do exist, but are still complex and not widely adopted.
**
Container
**

- Containers run the same no matter where they are deployed. You can easily run and test your applications anywhere.
- Containers can easily be run in a cloud, local data center or on a developer’s workstation.

##  Security

**Serverless**

- Control what service/api can invoke Lambda using resource policy.
- Control what service can Lambda invoke using IAM Role.
- Multiple AuthN/Z methods with API Gateway.
- Same security group:subnet combo reuse same ENI (IP).
- FedRAMP (high) compliance.

**Container**

- Pods support security groups and IAM roles to access other AWS resources.
- OPA (Open Policy Agent) to enforce semantic validation of objects during create, update, and delete operations.
- Each Pod can have its own IP from VPC.
- EKS on EC2, FedRAMP in Gov regions.

## Operational Complexity

**Serverless**

Serverless architecture has no backend to manage. There is effectively no infrastructure to manage.

**Container**

- Containers take longer to set up initially than serverless functions because it is necessary to configure system settings, libraries, and so on. It is possible to offload infrastructure management to a provider, but that isn’t always the case.

## Use Cases
**When to use Serverless?**

- If traffic pattern is unpredictable, and want automatically scaling in response to demand.
- If you want to process and analyze data at scale but want to avoid infrastructure management and resources your application consumes.
- If you don’t want to pay when there is no traffic at all. (i.e you want go for pay as you use pricing model).
- You want native integration with other AWS services such as SQS, SNS, S3, etc. For example, trigger Lambda function when message added to SQS Queue.
- You want to develop cloud-native applications based on event-driven architectures (especially for green fields apps).
- Data processing: Serverless can enable data processing from multiple sources using simple functions.
- IoT: Serverless computing provides an event-driven and straightforward way for IoT devices and external systems to communicate asynchronously.

## When to use Containers?

- If traffic pattern is predictable, and want rapid scaling (for sudden spikes) without any concurrency limit constraints.
- If you want to use the operating system of your own choice and leverage full control over underlying infrastructure.
- If you want to use software with specific version requirements (such as Web Server, App Server) and third-party softwares/tools, then containers are great to start with.
- Faster legacy application migration to the cloud: Containers make it easy to package entire applications and move them to the cloud without needing to make any code changes.
- Microservices: Because containers are portable, lightweight, provide process isolation, and easy to deploy, they are an excellent fit for creating loosely coupled microservices.
- Batch processing: Package batch processing and ETL jobs into containers to start jobs quickly and scale them dynamically in response to demand.
- Machine learning: Use containers to quickly scale machine learning models for training and inference and run them close to your data sources on any platform.
- Deploy anywhere/Hybrid applications: Containers let you standardize how code is deployed, making it easy to build workflows for applications that run between on-premises and across multiple clouds environments.
- CI/CD: Containers provide DevOps teams with a way to eliminate environment differences between dev, QA, staging, and production deployments. As a result, they are highly useful in CI/CD workflows.

## Summary
Containers and serverless computing are two of the most popular methods for deploying applications. Containers work better for some use cases, while in others, serverless is what you need. None is better than the other in an absolute sense, each response to specific needs. Serverless and Containers can compensate for each other’s weaknesses. They can coexist and be integrated, as needed, in a single project.