# Lambda

Lambda is a service that lets you run code without managing servers.

How it works:

1. You write a small piece of code (a function).
2. AWS Lambda runs it only when it’s needed (triggered by an event).
3. You pay only for the time the code runs, not for idle servers.

Analogy:

* Think of it like a coffee machine that turns on only when you press the button. You don’t keep it running all day—just use it when needed.

Example:

* When someone uploads a photo to your website, Lambda automatically resizes the image without you running a full server for it.

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FkaQ2X6qbv0RfCRBDqnIX%2Fimage.png?alt=media&#x26;token=60b30fe0-f598-4840-9013-ae1c19059d19" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FlAL0MXzVA53NYIMxFz2i%2Fimage.png?alt=media&#x26;token=b581fdce-dc2f-4055-809b-90d4d289c79d" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2Fmu20dA3EvAStzF3YkYqE%2Fimage.png?alt=media&#x26;token=8b3b870d-fa55-49d0-8420-ac682c849212" alt=""><figcaption></figcaption></figure>

### **Stateless Vs Stateful**

1\. Stateless:

* The system doesn’t remember anything about past interactions.
* Every request is independent.
* Analogy: Like a vending machine—you put money and choose a snack, it doesn’t care what you did before.
* Example: Most web servers handling HTTP requests (each request is treated separately).

2\. Stateful:

* The system remembers past interactions and uses that info for the next request.
* Analogy: Like a video game save—your progress is remembered.
* Example: Online shopping cart (it remembers what items you added).

Key difference:

* Stateless = memoryless, simple, easier to scale.
* Stateful = remembers history, needed when context matters.

### **Lambda Networking Mode:**

By default, AWS Lambda is serverless and runs inside AWS’s own network. But sometimes your function needs to talk to things inside your VPC (Virtual Private Cloud) like RDS databases, EC2 instances, or private subnets.

Here’s how it works:

1\. Default (No VPC)

* Lambda has internet access (can call public APIs, S3, etc.).
* But it cannot access private resources inside your VPC.

👉 Example: Lambda can call `google.com` or AWS services, but not your private RDS database

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2F662DudE85aEMA35T4wdH%2Fimage.png?alt=media&#x26;token=da715a4f-3c51-4282-9490-3465053ff78f" alt=""><figcaption></figcaption></figure>

2\. **With VPC Configuration**

* You connect Lambda to your VPC + subnets + security groups.
* Now it can talk to private resources (like RDS, EC2, ElastiCache).
* But ⚠️ by default, it loses internet access (because private subnets don’t have direct internet).

👉 Example: Lambda can query your private RDS database, but it can’t call `google.com` anymore.

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FmbzqWvHLk09CHmxbrs2L%2Fimage.png?alt=media&#x26;token=a2c12a1e-e3c3-4e5a-bed4-57625bb28603" alt=""><figcaption></figcaption></figure>

3\. **Regaining Internet (NAT Gateway)**

* If your Lambda needs both private VPC resources and the internet, you place it in a private subnet with a NAT Gateway in a public subnet.
* This way it can reach the internet through NAT, and still talk to private resources.

**Security**

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FFng055UiKHA3KGcAvgPR%2Fimage.png?alt=media&#x26;token=e4343c33-2cf6-4684-abf5-4ac3d91cec20" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2F8N8VOWuXcuVXSvBao210%2Fimage.png?alt=media&#x26;token=99fb9d98-c5b6-4bc6-939d-db7b56320cb2" alt=""><figcaption></figcaption></figure>

**Simple Analogy:**

* Think of Lambda as a delivery worker:
  * Default = worker is outside (can go anywhere in the city = internet).
  * Inside VPC = worker enters your company building (can only talk to people inside, no city access).
  * NAT Gateway = a door with a security guard that lets the worker go outside to the city and come back.

✅ So, Lambda networking = choosing where Lambda runs (public vs private) and setting up routes (internet or private).

### **Lambda Invocation**

**Synchronous**

* Caller waits for the result.
* If Lambda fails, the caller sees the error.
* Examples:
  * API Gateway calling Lambda (user clicks a button → expects immediate response).
  * CLI/SDK calling Lambda directly.
* Analogy: Asking a waiter for water and standing there until they bring it.

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FZIroIQYKXNyM5JciGa1e%2Fimage.png?alt=media&#x26;token=0ea0ca15-6a88-4d46-946f-2d25126b5076" alt=""><figcaption></figcaption></figure>

**Asynchronous Invocation**

* Caller doesn’t wait. Lambda is triggered in the background.
* If it fails, AWS retries automatically.
* Examples:
  * S3 event → upload a file → Lambda runs to process it.
  * SNS (notifications) → triggers Lambda.
* Analogy: Ordering food online — you place the order and move on, the restaurant delivers later.

Idempotent An operation is idempotent if doing it once or many times gives the same result.

Examples

* Idempotent:
  * Setting your password to `1234` → doing it again still results in `1234`.
  * `DELETE /user/123` → deleting the same user again won’t bring them back; result is the same (user gone).
* Not Idempotent:

  * Adding money to your bank account → if you repeat it, balance keeps increasing.
  * `POST /order` → every request creates a new order (so multiple calls = multiple orders).

  Analogy

  * Idempotent: Turning off a light switch. Whether you flip it once or 10 times → the light stays off.
  * Not Idempotent: Pressing a “dispense candy” button. Every press gives you more candy

✅ In AWS Lambda:\
You often want your functions to be idempotent, so if the same event is retried (like S3 triggering twice), the result doesn’t duplicate (e.g., no double watermarking on an image).

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FxggWzW3oWUitUkwjO7tv%2Fimage.png?alt=media&#x26;token=45083b1b-410c-4c6c-8b89-5c557cd1db7f" alt=""><figcaption></figcaption></figure>

### **Event Source Mapping (Stream-based Invocation)**

* Lambda polls event sources like DynamoDB Streams, Kinesis, or SQS queues and processes records automatically.
* Analogy: A worker constantly checking a mailbox (queue) and processing new letters.

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2F3unTnthn45M9b3P06fMD%2Fimage.png?alt=media&#x26;token=0a14f08f-2e10-4084-a42e-7cbebd1c0cf2" alt=""><figcaption></figcaption></figure>

✅ So in short:

* Sync = Wait for answer
* Async = Fire and forget
* Event Source Mapping = Continuous polling from a queue/stream

### **Lambda version**

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2Fqi8nu78Xj2WYiJBoyxBR%2Fimage.png?alt=media&#x26;token=05f51d44-4313-45b1-8dd5-07cb5b905871" alt=""><figcaption></figcaption></figure>

### **Lambda Runtime**

<figure><img src="https://1856860631-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FsNc001Xcz63mWjGXymkd%2Fuploads%2FI6spnaPKVg9HJHIcIVIw%2Fimage.png?alt=media&#x26;token=488f0c75-ee53-401a-a51b-6079dfdde295" alt=""><figcaption></figcaption></figure>
