Kubernate

AWS - Lambda

 AWS Lambda is a serverless compute service from Amazon Web Services that lets you run code without managing servers. You just write the code, upload it, and AWS automatically runs it when an event happens.


What is AWS Lambda? (Simple definition)

👉 AWS Lambda = Run code on demand, pay only when it runs

  • No server setup or maintenance

  • Automatically scales up/down

  • You pay only for execution time (in milliseconds)


How AWS Lambda Works (Step-by-step)

  1. You write code (Java, Python, Node.js, etc.)

  2. Upload it to AWS Lambda

  3. Define a trigger (event)

  4. AWS runs your code only when that event happens

Common Triggers

  • API call (via API Gateway)

  • File upload to S3

  • Database update (DynamoDB)

  • Message in SQS / Kafka (MSK)

  • Scheduled job (like CRON)


Simple Architecture

Client → API Gateway → AWS Lambda → Database

or

S3 File Upload → AWS Lambda → Process File → Save Result

Supported Languages

  • Java

  • Python

  • Node.js

  • Go

  • C#

  • Ruby

(Java is very common in enterprise projects)


Example 1: Simple Lambda (Java)

public class HelloLambda {
    public String handleRequest(String input) {
        return "Hello " + input + " from AWS Lambda";
    }
}

AWS automatically calls handleRequest() when triggered.


Example 2: Real-world Use Cases

1️⃣ Banking / Enterprise

  • Validate transactions

  • Fraud detection triggers

  • Generate statements

  • Process payment events

2️⃣ IT / Microservices

  • Backend APIs (serverless microservices)

  • Authentication & authorization

  • Data transformation

3️⃣ Automation

  • Run nightly jobs

  • Cleanup old files

  • Send alerts & notifications


How to Use AWS Lambda (Practically)

Step 1: Create Lambda Function

  • Go to AWS Console → Lambda

  • Click Create function

  • Choose runtime (Java / Python)

  • Upload code or use editor

Step 2: Add Trigger

  • API Gateway → REST/HTTP API

  • S3 → Object upload

  • EventBridge → Schedule

Step 3: Set Permissions

  • Attach IAM role

  • Grant access to S3, DB, etc.

Step 4: Test & Deploy

  • Use test event

  • Check CloudWatch logs


Pricing (Very Important)

You pay for:

  • Number of requests

  • Execution duration

  • Memory allocated

💡 Free Tier:

  • 1 million requests/month

  • 400,000 GB-seconds/month


Advantages

✅ No server management
✅ Auto scaling
✅ Cost-effective
✅ Fast deployment
✅ Ideal for microservices


Limitations

⚠ Cold start (Java especially)
⚠ Max execution time: 15 minutes
⚠ Not good for long-running tasks
⚠ Limited local storage


AWS Lambda vs EC2

FeatureLambdaEC2
Server Management❌ No✅ Yes
Auto Scaling✅ YesManual/Auto
Pay ModelPer executionPer hour
Best forEvent-drivenLong-running apps

When Should YOU Use Lambda?

✔ Event-driven systems
✔ Microservices APIs
✔ Batch & automation jobs
✔ Low-to-medium traffic apps

❌ Long-running processes
❌ Heavy compute workloads



No comments:

Post a Comment

Spring Boot - Bean LifeCycle

 Here is a clear, step-by-step lifecycle of a Spring Boot application , explained in a simple + interview-ready way. 🔄 Spring Boot Applica...

Kubernate