What is AWS Lambda Function ?

DevOps | Cloud Practitioner | AWS | GIT | Kubernetes | Terraform | ArgoCD | Gitlab
Lambda belongs to compute family and solves problem of serverless . These two characteristics main.
Lambda Functions for DevOps Engineers
Lambda Functions Use Cases for DevOps:
Cloud Cost Optimization: One key use of Lambda functions in DevOps is to help optimize cloud costs. Lambda functions can be triggered by various AWS services (like S3, CloudWatch, etc.) to perform automated tasks, which helps in managing costs effectively.
Event-Driven Actions: Lambda functions can be triggered by events from various services. For example, when a file is uploaded to S3, it can automatically trigger a Lambda function to process that file.
Serverless Architecture:
- Serverless: Lambda functions are part of serverless architecture, where you don't need to manage servers directly. AWS takes care of provisioning, scaling, and maintaining the infrastructure. You just focus on writing code.
Lambda vs EC2:
EC2: With EC2 (Elastic Compute Cloud), you need to provision and manage servers. You have more control over the infrastructure but also more responsibility.
Lambda: Lambda, on the other hand, is fully managed. You only focus on writing code, and AWS automatically handles the scaling and infrastructure management. It’s "serverless", meaning you don't need to worry about the underlying servers or managing them.
Understanding Lambda Functions and the Problem They Solve
When you think about Lambda, it’s important to first understand what problem it solves on AWS. Every AWS service is designed to solve a specific problem or need.
S3: Solves the problem of storage.
EC2: Solves the problem of compute (providing virtual servers for workloads).
Lambda: Solves the problem of serverless computing.
Lambda functions are part of the compute family, but unlike EC2, they follow a serverless architecture. Let’s break this down step by step.
EC2 vs. Lambda: Key Differences
EC2 (Elastic Compute Cloud):
What it does: EC2 provides you with a virtual server. You configure it with the specific image, instance type (CPU, memory), security settings, etc. Once configured, you can run applications on it.
Responsibilities: You are responsible for provisioning, scaling, and tearing down the instance. After you're done with your job, you have to manually shut down the EC2 instance.
Use Case: Ideal when you need full control over your servers and need to run long-lived services or applications.
Lambda:
What it does: Lambda also provides compute power (like EC2), but it does so in a serverless way. This means:
No need to specify compute resources (like CPU, RAM).
AWS automatically provisions the compute resources based on the needs of your application.
Responsibilities: You don’t need to manage the servers. Lambda takes care of everything for you. It automatically scales the compute power when required and tears it down once the function finishes execution.
Use Case: Ideal for short-lived, event-driven applications where you don't want to manage servers.
What is Serverless Architecture?
In serverless architecture, you don’t manage the servers. With Lambda, AWS manages everything:
Scaling: Automatically scales the application as needed. If there’s more traffic or load, Lambda will allocate more resources.
Tear down: After the function completes, the compute resources are automatically shut down. There’s no need for you to manually handle the infrastructure.
How Lambda Works: A Real-World Example
Let’s say you create a Lambda function to handle a calculator application:
Create the Lambda function: You write a function to perform a basic calculator operation (e.g., addition of two numbers).
Trigger the Lambda: A user triggers this function, for example, by sending a request to an API Gateway.
Lambda Execution: AWS automatically provisions compute resources (e.g., CPU, memory) depending on your function’s requirements.
Task Completion: Once the addition is complete (e.g., 2 + 3), the Lambda function terminates.
Tear Down: The compute resources that were used to run the function are automatically torn down by AWS.
Key Advantages of Lambda Over EC2
No Server Management:
EC2: You need to configure, monitor, and maintain the server. You also need to make sure it's properly scaled based on usage.
Lambda: No server management needed. AWS automatically handles everything.
Cost Efficiency:
EC2: You pay for the compute capacity 24/7, even if you’re not using it.
Lambda: You only pay for the compute time that your function uses. Once the task is done, you stop paying.
Auto-Scaling:
EC2: You need to configure scaling manually (e.g., using auto-scaling groups).
Lambda: Lambda scales automatically based on the incoming requests. If more traffic comes, AWS automatically provisions more resources.
Automatic Cleanup:
EC2: Once the work is done, you need to manually shut down the instance.
Lambda: After the function completes, AWS automatically cleans up the resources.
Summary till now:
To sum up:
EC2 is for when you need complete control over virtual machines and long-running processes.
Important: Lambda is for serverless applications that are event-driven, where you only want to execute functions without worrying about servers or infrastructure.
With Lambda, AWS takes care of everything for you:
Provisioning: Automatically provisions the required compute.
Scaling: Automatically scales up or down based on demand.
Cleanup: Automatically shuts down the resources once the task is done.
This makes Lambda an excellent choice for tasks that are short-lived, cost-sensitive, and event-driven, such as those in DevOps workflows.
This is a fantastic example that clearly demonstrates how Lambda functions fit into real-world scenarios, especially from a DevOps perspective. Let’s break down and summarize the key points that you’ve covered.
Scenario: Food Delivery Platform
Imagine you have a food delivery platform, where a user places an order, completes payment, and then logs out of the application. The key idea here is that after the user has completed their task (i.e., the payment), there is no reason to keep the server running. This is where Lambda shines.
Traditional Server Approach (EC2):
If you were using EC2, you would have to manually create and maintain the instance, and you’d still be paying for the compute resources even if no one is using them after the payment is processed.
Example: You'd need to have a virtual server running all the time to handle transactions, which may not be efficient or cost-effective for a brief interaction like a payment processing.
Serverless Approach (Lambda):
In a Lambda-based architecture, AWS would automatically provision the compute power as needed for handling the payment transaction page. Once the transaction is done and the user's task is completed, AWS automatically tears down the server. No need for manual intervention.
Pay-as-you-go: You only pay for the compute time the Lambda function uses, so it's highly cost-effective for such intermittent tasks.
Why Lambda for This Scenario?
Auto-scaling: The serverless model scales as needed. If the payment volume spikes, Lambda will allocate more resources to handle the load and then scale down once the load decreases.
Cost Efficiency: You don't incur costs when the function isn't running. The server is created only when needed and destroyed once the task is complete.
No Server Management: AWS automatically manages the compute resources, freeing you from worrying about server maintenance, scaling, or teardown.
Lambda for DevOps: Cost Optimization Example
As you mentioned, a common responsibility for DevOps engineers is cost optimization on the cloud. Lambda functions are a powerful tool for this task because they allow you to automate the process of identifying unused or underutilized resources and taking corrective actions (e.g., terminating unused resources or notifying stakeholders).
For instance, as a DevOps engineer, you can:
Detect unused resources (e.g., EBS volumes, EC2 instances, Elastic IPs).
Automate cleanup actions like deleting unused resources or triggering notifications (e.g., alerting the relevant teams about resources that are not being used).
Schedule Lambda functions: You can set Lambda functions to run at regular intervals using CloudWatch events (e.g., every morning at 10 AM), making sure you don't leave any unnecessary resources running that could be costing the organization.
Lambda Use Case: Security and Compliance
Lambda can also help with enforcing security policies, such as ensuring that no GP2 EBS volumes are used in company, or that no public S3 buckets exist for security purpose.
Automated Security Audits: Lambda functions can run scheduled security checks to detect any violations (like public S3 buckets or use of insecure EBS types).
Notifications: If a violation is found, Lambda can send notifications to the responsible teams (e.g., via SNS).
Compliance Enforcement: Automatically convert or notify users to convert non-compliant resources (like from GP2 to GP3 volumes). Simple no one will convert gp2 → gp3 and if done lambda will tell us.
This is a great example of how Lambda functions can help ensure that your organization adheres to security policies in a cost-efficient and automated manner.
Why Lambda for DevOps Engineers?
As a DevOps engineer, Lambda can be incredibly helpful for:
Automating routine tasks like cost optimization, security audits, and resource cleanup.
Event-driven operations: Lambda allows you to respond to events such as resource creation or deletion, and take action without manual intervention.
Scale without overhead: Whether you're working with one or a hundred resources, Lambda can scale automatically to accommodate the needs of your infrastructure.
Lambda for Cost Optimization in Practice
Let's say you have to run a daily report checking for unused AWS resources. Here's how Lambda can be the perfect fit:
You write a Lambda function that checks for unused EC2 instances, Elastic IPs, or other resources.
Trigger it daily at 10 AM using CloudWatch Events.
Action taken: If any resource is found to be underutilized or unused, you can either delete it, or send a notification to the responsible team (e.g., via SNS).
Automatic Scaling: Lambda scales up or down based on the amount of work needed (e.g., checking the resources), and you only pay for the time it takes to run the function.
This eliminates the need to spin up and tear down EC2 instances manually every day, and the cost savings are significant.
Conclusion:
In summary, Lambda functions are a powerful tool for DevOps engineers, especially when it comes to:
Cost optimization: Automating the process of detecting unused resources and taking actions like deletion or notifications.
Security and compliance: Enforcing company policies by automating checks for things like insecure resource configurations.
Serverless benefits: Reducing overhead in terms of provisioning and maintaining infrastructure, while only paying for the compute resources you actually use.
AWS Lambda vs EC2 Instance:
EC2 (Virtual Server):
Virtual Server: You manage the entire server setup.
Pay for uptime: You pay for the server’s running time, even if idle.
Manual Scaling: You need to handle scaling and server maintenance..
Costs: EC2 is billed based on the instance type and duration, regardless of whether it’s being used or not, meaning you’ll pay for the uptime of the server even if it’s idle.
AWS Lambda (Serverless):
AWS Lambda, on the other hand, is a serverless compute service. This means you don't need to manage servers at all. You just write your code (in languages like Python, Node.js, Java, Go, etc.), and Lambda takes care of provisioning, scaling, and managing the infrastructure for you.
With Lambda, you only need to focus on writing the logic for your function, and you deploy it as a Lambda function. You don’t need to worry about operating systems or servers.
Serverless: Since there is no underlying server you need to manage, the infrastructure is abstracted away by AWS.
Pay-per-use: Lambda follows a pay-per-use model, meaning you only pay for the time your function runs. The billing is based on the duration of execution (measured in milliseconds) and the amount of resources used (memory allocation).
Key Advantages of AWS Lambda:
No Server Setup:
- You don't need to worry about setting up or configuring servers, as you would with EC2. Lambda abstracts away the server management entirely. You just focus on the code.
No Start and Stop:
With EC2, you would have to start and stop your virtual machine (VM) to run tasks, which can be time-consuming and requires you to manage the state of the server.
In contrast, AWS Lambda takes care of scaling up and scaling down your compute power automatically, based on the incoming request. No need to manually start or stop anything.
No Additional Cost:
In an EC2 setup, you're paying for the server's uptime, regardless of how much it's used. If you have a server running 24/7 but only use it for a few minutes each day, you’ll still incur the full cost.
With Lambda, you only pay when your function is actually executed. There’s no charge for idle time, so it’s much more cost-effective for occasional or event-driven workloads.
Serverless:
- Lambda is completely serverless, meaning you don’t need to manage the infrastructure or worry about scaling. AWS handles that for you. You simply write the code and deploy it.
Example:
For instance, if you want to process an image uploaded to an S3 bucket, you could write a Lambda function to automatically process the image whenever it’s uploaded. AWS Lambda will:
Scale automatically to handle multiple image uploads at once.
Only charge you for the time spent processing the images (in milliseconds).
No server setup or management required, and you only pay when the function is triggered.
Comparison Summary:
| Feature | EC2 Instance | AWS Lambda |
| Server Management | Full control, need to manage servers manually | No server management, serverless |
| Scaling | Manual scaling or set up auto-scaling | Automatic scaling, no management needed |
| Cost | Charges for the instance uptime | Pay only for the execution time |
| Maintenance | You must patch and manage the system | No maintenance, fully managed by AWS |
| Use Case | Long-running applications, web servers | Short-lived, event-driven workloads |
Conclusion:
If you have workloads that don’t require persistent servers running all the time, Lambda is the optimal choice due to its pay-per-use model and the fact that you don't need to manage servers or worry about scaling.
AWS Lambda Deep Dive for Beginners
Introduction to Serverless Computing
Today, we're going to embark on an exciting journey into the world of serverless computing and explore AWS Lambda, a powerful service offered by Amazon Web Services.
So, what exactly is "serverless computing"? Don't worry; it's not about eliminating servers altogether. Instead, serverless computing is a cloud computing execution model where you, as a developer, don't have to manage servers directly. You focus solely on writing and deploying your code, while the cloud provider takes care of all the underlying infrastructure.
Understanding AWS Lambda
In this serverless landscape, AWS Lambda shines as a leading service. AWS Lambda is a compute service that lets you run your code in response to events without the need to provision or manage servers. It automatically scales your applications based on incoming requests, so you don't have to worry about capacity planning or dealing with server maintenance.
How Lambda Functions Fit into the Serverless World
At the heart of AWS Lambda are "Lambda functions." These are individual units of code that perform specific tasks. Think of them as small, single-purpose applications that run independently.
Here's how Lambda functions fit into the serverless world:
Event-Driven Execution: Lambda functions are triggered by events. An event could be anything, like a new file being uploaded to Amazon S3, a request hitting an API, or a specific time on the clock. When an event occurs, Lambda executes the corresponding function.
No Server Management: As a developer, you don't need to worry about managing servers. AWS handles everything behind the scenes. You just upload your code, configure the trigger, and Lambda takes care of the rest.
Automatic Scaling: Whether you have one user or one million users, Lambda scales automatically. Each function instance runs independently, ensuring that your application can handle any level of incoming traffic without manual intervention.
Pay-per-Use: One of the most attractive features of serverless computing is cost efficiency. With Lambda, you pay only for the compute time your code consumes. When your code isn't running, you're not charged.
Supported Languages: Lambda supports multiple programming languages like Node.js, Python, Java, Go, and more. You can choose the language you are comfortable with or that best fits your application's needs.
Real-World Use Cases
Now, let's explore some real-world use cases to better understand how AWS Lambda can be applied:
Automated Image Processing: Imagine you have a photo-sharing app, and users upload images every day. You can use Lambda to automatically resize or compress these images as soon as they are uploaded to S3.
Chatbots and Virtual Assistants: Build interactive chatbots or voice-controlled virtual assistants using Lambda. These assistants can perform tasks like answering questions, fetching data, or even controlling smart home devices.
Scheduled Data Backups: Use Lambda to create scheduled tasks for backing up data from one storage location to another, ensuring data resilience and disaster recovery.
Real-Time Analytics: Lambda can process streaming data from IoT devices, social media, or other sources, allowing you to perform real-time analytics and gain insights instantly.
API Backends: Develop scalable API backends for web and mobile applications using Lambda. It automatically handles the incoming API requests and executes the corresponding functions.


