🏫 The School›🪪 AWS›⚡ धडा 19 — Lambda: फक्त बोलावल्यावरच अस्तित्वात असणारा मदतनीस (serverless functions)
🖼️ See the drawing + lab 🏠 Course home 🌿 Branch on GitHub ✏️ View source
🖼️ आकृती आणि labThe drawing + lab पूर्ण पानावर उघडा ↗Open full page ↗

⚡ धडा 19 — Lambda: फक्त बोलावल्यावरच अस्तित्वात असणारा मदतनीस (serverless functions)

📍 तुम्ही इथे आहात: 20 पैकी धडा 19 · मागील: lesson-18-cloudwatch · पुढील: lesson-20-the-bill


📦 या ब्रँचमध्ये काय आहे

धडे 01–19. Code चालवण्याचा दुसरा मार्ग — बाकच नाही. खरी file:

🧒 5 वर्षांच्या मुलाला समजावल्यासारखे

बाक भाड्याने घेणे (EC2) म्हणजे बाक रिकामा बसलेला असतानाही पैसे भरणे. पण काही कामे म्हणजे छोटी कामे असतात: फोटो upload झाला की त्याचा आकार बदला, मध्यरात्री एक अहवाल चालवा. अशा कामांसाठी शाळेकडे एक जादुई बोलावल्यावर येणारा मदतनीस ⚡ आहे: Lambda.

दोन प्रामाणिक तळटीपा:

🗺️ आकृती

flowchart LR
    ev["🔔 events<br/>📦 S3 upload · ⏰ schedule · 🌐 URL"]
    fn["⚡ helper appears 💨<br/>runs hello.py<br/>≤ 15 min, per-100ms billing"]
    hat["🎩 execution role<br/>may touch: this bucket, logs"]
    out["✅ errand done<br/>result → S3 / response"]
    gone["👻 …vanishes<br/>idle cost: $0"]
    ev -->|"1"| fn
    hat -->|"2 worn during"| fn
    fn -->|"3"| out
    fn -->|"4"| gone

❓ काय

🤔 का

"Code कुठे चालतो" याचे हे तिसरे उत्तर आहे: बाक भाड्याने घ्या (EC2), बाकांची शाळा चालवा (k8s), किंवा कामांचे वर्णन करा (Lambda). खऱ्या systems चे संपूर्ण जोडणी-थर — thumbnails, webhooks, रात्रीची साफसफाई, धडा 12 मधल्या ops scripts — छोटी कामे म्हणून सर्वात स्वस्त आणि सोपे असतात. तिन्ही paradigms (आणि त्यांची bills) माहीत असणे म्हणजेच "architect" असणे.

🔧 कसे

zip fn.zip hello.py
aws lambda create-function --function-name hello --runtime python3.12 \
  --handler hello.handler --zip-file fileb://fn.zip --role $HELPER_ROLE_ARN
aws lambda invoke --function-name hello --payload '{"name":"class 3A"}' out.json
cat out.json     # {"greeting": "hello, class 3A"}

🧪 करून पाहा (~10 मिनिटे, मोफत — महिन्याला दहा लाख कामे $0)

वरच्या चार commands चालवा (role ची trust policy धडा 04 चीच, शब्दशः — टोपी कोण घालू शकतो: lambda.amazonaws.com). ते पाच वेळा पटापट invoke करा; CloudWatch डायरीतल्या REPORT ओळी वाचा — milliseconds मध्ये, एक cold start आणि चार warm ओळखा.

aws lambda delete-function --function-name hello   # tidy, as always

✅ तपासा — तुम्हाला काय दिसायला हवे

aws lambda invoke {"greeting": "hello, class 3A"} लिहितो; CloudWatch log group एक cold Init Duration आणि चार warm invocations दाखवतो.

🧹 साफसफाई — चालू ठेवू नका

aws lambda delete-function --function-name hello — दहा लाख invocations मोफत आहेत, तरीही नीटनेटके ठेवा

⚠️ नेहमीच्या चुका

⏭️ पुढे

तुम्ही आता बाक, lockers, receptions, कार्यालये आणि मदतनीस भाड्याने घेता. एक कौशल्य उरले, आणि तेच हा छंद परवडणारा ठेवते: मीटर वाचणे. धडा 20 — bill.

⚡ Lesson 19 — Lambda: the helper who exists only while called (serverless functions)

📍 You are here: Lesson 19 of 20 · Previous: lesson-18-cloudwatch · Next: lesson-20-the-bill


📦 What's in this branch

Lessons 01–19. The other way to run code — no desk at all. Real file:

🧒 Explain like I'm 5

Renting a desk (EC2) means paying for the desk while it sits empty. But some jobs are errands: resize a photo when one is uploaded, run a report at midnight. For errands, the school has a magical on-call helper ⚡: Lambda.

Two honest footnotes:

🗺️ Diagram

flowchart LR
    ev["🔔 events<br/>📦 S3 upload · ⏰ schedule · 🌐 URL"]
    fn["⚡ helper appears 💨<br/>runs hello.py<br/>≤ 15 min, per-100ms billing"]
    hat["🎩 execution role<br/>may touch: this bucket, logs"]
    out["✅ errand done<br/>result → S3 / response"]
    gone["👻 …vanishes<br/>idle cost: $0"]
    ev -->|"1"| fn
    hat -->|"2 worn during"| fn
    fn -->|"3"| out
    fn -->|"4"| gone

❓ What

🤔 Why

This is the third answer to "where does code run": rent desks (EC2), run a school of desks (k8s), or describe errands (Lambda). Whole glue-layers of real systems — thumbnails, webhooks, nightly cleanups, the ops scripts from lesson 12 — are cheapest and simplest as errands. Knowing all three paradigms (and their bills) is what "architect" means.

🔧 How

zip fn.zip hello.py
aws lambda create-function --function-name hello --runtime python3.12 \
  --handler hello.handler --zip-file fileb://fn.zip --role $HELPER_ROLE_ARN
aws lambda invoke --function-name hello --payload '{"name":"class 3A"}' out.json
cat out.json     # {"greeting": "hello, class 3A"}

🧪 Try it (~10 min, free — a million errands/month are $0)

Run the four commands above (the trust policy for the role is lesson 04's, verbatim — who may wear the hat: lambda.amazonaws.com). Invoke it five times fast; read the REPORT lines in the CloudWatch diary — spot the one cold start vs four warm ones, in milliseconds.

aws lambda delete-function --function-name hello   # tidy, as always

✅ Verify — what you should see

aws lambda invoke writes {"greeting": "hello, class 3A"}; the CloudWatch log group shows one cold Init Duration and four warm invocations.

🧹 Clean up — do not leave running

aws lambda delete-function --function-name hello — a million invocations are free, but tidy anyway

⚠️ Common mistakes

⏭️ Next

You now rent desks, lockers, receptions, offices, and helpers. One skill left, and it's the one that keeps this hobby affordable: reading the meter. Lesson 20 — the bill.

← PreviouscloudwatchNext →the bill

This page is the lesson's README from the lesson-19-lambda branch, shown here so the whole School stays on one site. Code files open on GitHub at the same branch.