Ravindra BagaleCourses & study guides मराठी

Chapter 84: DevOps Projects Part 14: Capstone CI/CD EKS DR Serverless E-commerce Terraform Jenkins Argo Prometheus

84.3 Project 99: Serverless deploy with Lambda + API Gateway

Friends, not every API needs a cluster. Today we deploy a Lambda function behind API Gateway and prove a URL returns JSON.

What you will build in this project

  1. Write a Lambda handler.
  2. Deploy with IaC.
  3. Attach API Gateway HTTP API.
  4. Hit the URL and read JSON.

1. Start from a real company story

At Policybazaar in Gurgaon, a lead-capture form kept a whole EC2 fleet warm for a few spikes a day.

Rough cost: about Rs 8 lakh a year in idle compute.

Meera moved the form API to Lambda plus API Gateway.

Today we ship one serverless endpoint.

browserHTTPSAPIGWrouteLambdahandlerJSON200
Picture 1: browser -> API Gateway -> Lambda -> JSON

Ravindra Bagale's Tip

Students put long work inside the request path. Keep handlers short; push heavy work to a queue. Keep that habit.

2. Start and prepare

Step 1: Why this project

Why: Real outages teach. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: echo why. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 1: a1

Type this:

echo 'Today we build lambda99'

You will see:

Today we build lambda99

What it means: Why.

Example 2: a2

Type this:

mkdir -p /tmp/lambda99 && echo 'Scope ready'

You will see:

Scope ready

What it means: Scope.

Example 3: a3

Type this:

echo 'Keep commands short and check every output'

You will see:

Keep commands short and check every output

What it means: Habit.

Step 2: Prepare workspace

Why: Folders and notes. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: prep. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 4: b1

Type this:

mkdir -p /tmp/lambda99 && printf '%s\n' 'lab notes' > /tmp/lambda99/NOTES.txt && cat /tmp/lambda99/NOTES.txt

You will see:

lab notes

What it means: Notes.

Example 5: b2

Type this:

echo 'Work only under /tmp/lambda99 for this project'

You will see:

Work only under /tmp/lambda99 for this project

What it means: Path.

Example 6: b3

Type this:

echo 'Write ACCEPT.txt at the end'

You will see:

Write ACCEPT.txt at the end

What it means: Accept file.

Step 3: Handler

Why: Short code. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: handler. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 7: c1

Type this:

printf '%s\n' 'def handler(event, context):' '    return {"statusCode": 200, "body": "{\"ok\": true}"}' > /tmp/lambda99/app.py && cat /tmp/lambda99/app.py

You will see:

return {"statusCode": 200, "body": "{\"ok\": true}"}

What it means: Handler.

Example 8: c2

Type this:

echo 'Keep dependencies thin; zip only what you need'

You will see:

Keep dependencies thin; zip only what you need

What it means: Zip.

Example 9: c3

Type this:

echo 'Set timeout and memory deliberately; defaults waste money'

You will see:

Set timeout and memory deliberately; defaults waste money

What it means: Limits.

3. Build the main pieces

Step 4: Package

Why: Zip or image. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: pkg. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 10: d1

Type this:

echo 'cd /tmp/lambda99 && zip -r function.zip app.py'

You will see:

cd /tmp/lambda99 && zip -r function.zip app.py

What it means: Zip.

Example 11: d2

Type this:

echo 'Container images work for heavier runtimes'

You will see:

Container images work for heavier runtimes

What it means: Image.

Example 12: d3

Type this:

echo 'Pin a runtime version; do not float forever'

You will see:

Pin a runtime version; do not float forever

What it means: Pin.

Step 5: Deploy IaC

Why: Terraform or SAM. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: deploy. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 13: e1

Type this:

printf '%s\n' 'resource aws_lambda_function form {' '  function_name = "lead-form"' '  role          = aws_iam_role.lambda.arn' '  handler       = "app.handler"' '  runtime       = "python3.12"' '  filename      = "function.zip"' '}' > /tmp/lambda99/main.tf && cat /tmp/lambda99/main.tf

You will see:

function_name = "lead-form"

What it means: TF.

Example 14: e2

Type this:

echo 'terraform apply -auto-approve'

You will see:

terraform apply -auto-approve

What it means: Apply.

Example 15: e3

Type this:

printf '%s\n' 'Acceptance: function state Active in the region' > /tmp/lambda99/ACCEPT.txt && cat /tmp/lambda99/ACCEPT.txt

You will see:

Acceptance: function state Active in the region

What it means: Gate.

Step 6: API Gateway

Why: HTTP API. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: apigw. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 16: f1

Type this:

printf '%s\n' 'Create HTTP API' 'Route POST /lead -> Lambda integration' 'Stage $default auto-deploy' > /tmp/lambda99/API.txt && cat /tmp/lambda99/API.txt

You will see:

Route POST /lead -> Lambda integration

What it means: API.

Example 17: f2

Type this:

echo 'Grant apigateway.amazonaws.com invoke permission on the function'

You will see:

Grant apigateway.amazonaws.com invoke permission on the function

What it means: Perm.

Example 18: f3

Type this:

echo 'Copy the invoke URL into NOTES.txt'

You will see:

Copy the invoke URL into NOTES.txt

What it means: URL.

4. Automate and verify

Step 7: Prove with curl

Why: Smoke. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: curl. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 19: g1

Type this:

echo 'curl -sS -X POST "$URL/lead" -H "content-type: application/json" -d "{\"email\":\"a@b.com\"}"'

You will see:

curl -sS -X POST "$URL/lead" -H "content-type: application/json" -d "{\"email\":\"a@b.com\"}"

What it means: curl.

Example 20: g2

Type this:

echo 'Expect {"ok": true} and HTTP 200'

You will see:

Expect {"ok": true} and HTTP 200

What it means: Expect.

Example 21: g3

Type this:

echo 'Check CloudWatch logs for the request id'

You will see:

Check CloudWatch logs for the request id

What it means: Logs.

Step 8: Cold starts

Why: Reality. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: cold. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 22: h1

Type this:

printf '%s\n' 'Cold start: first invoke after idle pays init time' 'Provisioned concurrency only when SLA needs it' > /tmp/lambda99/COLD.txt && cat /tmp/lambda99/COLD.txt

You will see:

Cold start: first invoke after idle pays init time

What it means: Cold.

Example 23: h2

Type this:

echo 'Smaller packages and lighter runtimes help'

You will see:

Smaller packages and lighter runtimes help

What it means: Size.

Example 24: h3

Type this:

echo 'Measure p99; do not guess'

You will see:

Measure p99; do not guess

What it means: Measure.

Step 9: When not Lambda

Why: Choose wisely. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: when. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 25: i1

Type this:

printf '%s\n' 'Long CPU jobs, sticky websockets, and huge local disks may fit EC2/EKS better' 'Spiky HTTP and event glue fit Lambda well' > /tmp/lambda99/WHEN.txt && cat /tmp/lambda99/WHEN.txt

You will see:

Spiky HTTP and event glue fit Lambda well

What it means: When.

Example 26: i2

Type this:

echo 'Do not rewrite a healthy EKS service for fashion'

You will see:

Do not rewrite a healthy EKS service for fashion

What it means: Fashion.

Example 27: i3

Type this:

echo 'Cost model both options for your traffic shape'

You will see:

Cost model both options for your traffic shape

What it means: Cost model.

5. Harden and accept

Step 10: Story Policybazaar

Why: Gurgaon lesson. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: story. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 28: j1

Type this:

printf '%s\n' 'At Policybazaar in Gurgaon, a lead form kept EC2 warm for rare spikes.' 'Meera moved it to Lambda plus API Gateway.' > /tmp/lambda99/STORY.txt && wc -l /tmp/lambda99/STORY.txt

You will see:

2 /tmp/lambda99/STORY.txt

What it means: Story.

Example 29: j2

Type this:

echo 'Rough cost: about Rs 8 lakh a year in idle compute.'

You will see:

Rough cost: about Rs 8 lakh a year in idle compute.

What it means: Cost.

Example 30: j3

Type this:

echo 'Meera: PASS'

You will see:

Meera: PASS

What it means: Sign-off.

Step 11: Proof

Why: Files. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: ls. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 31: k1

Type this:

ls /tmp/lambda99

You will see:

NOTES.txt\nSTORY.txt\nACCEPT.txt

What it means: Tree.

Example 32: k2

Type this:

test -f /tmp/lambda99/ACCEPT.txt && echo ACCEPT_OK

You will see:

ACCEPT_OK

What it means: Accept present.

Example 33: k3

Type this:

echo 'Next: End-to-end e-commerce capstone project 100'

You will see:

Next: End-to-end e-commerce capstone project 100

What it means: Bridge.

Step 12: Acceptance

Why: Serverless URL returns JSON. We do this before the next step so mistakes stay small and easy to undo. Read the output carefully; the numbers and names tell you if the machine is healthy.

What: final. Follow the numbered examples below. Type each command, read the exact output, and match the meaning line before you move on. That is how real teams learn a tool.

Example 34: z1

Type this:

echo 'Lambda deployed'

You will see:

Lambda deployed

What it means: A.

Example 35: z2

Type this:

echo 'API route live'

You will see:

API route live

What it means: B.

Example 36: z3

Type this:

echo 'curl 200 JSON'

You will see:

curl 200 JSON

What it means: C.

Check yourself

Q1. Who moved Policybazaar form?

Meera

Q2. Compute?

Lambda

Q3. Front door?

API Gateway

Q4. Long work in handler?

no

Q5. Idle EC2 cost?

high

Q6. Prove with?

curl JSON

Friends, you shipped a serverless URL. Did you understand? Let us try it once more. Change the JSON field, redeploy, and prove the URL updates without an EC2.

Lab: try at home

  1. Handler.
  2. Deployed.
  3. API URL.
  4. JSON 200.

Recap

  • Lambda for spiky load.
  • API Gateway front door.
  • IaC the function.
  • Short handlers.
  • Watch cold starts.