Ravindra BagaleCourses & study guides English Track your progress

API Gateway, Lambda आणि DynamoDB ने Serverless API Backend Pattern

फूड कार्ट खिडकीवर ऑर्डर घेते, टिकिट लिहिते, किचन शिजवते. Serverless API backend ती खिडकी: API Gateway = counter, Lambda = एका टिकिटचा cook, DynamoDB = order book.

या chapter मध्ये तुम्ही काय शिकाल

  • Counter/cook/book map
  • HTTP vs REST vs Function URL
  • Request flow + status codes
  • Edge auth + throttle
  • Project chapters link

1. रोजचे उदाहरण: फूड कार्ट

Meera (Pune) momo cart: खिडकी = पेमेंट; किचन सार्वजनिक नाही. API Gateway = HTTPS खिडकी; DynamoDB = ticket.

तुम्ही URL shortener project आणि REST API project मध्ये variants केले. इथे pattern आणि design choices.

2. हे pattern का?

हे का लागतं?

EC2 public IP, TLS विसरणं, gateway मध्ये business logic — pattern तीन boxes स्वच्छ ठेवतो.

हे काय आहे?

Serverless API backend = HTTPS edge + Lambda + data + IAM.

3. Vertical diagram

Client -> API Gateway -> Lambda -> DynamoDBOptional: Cognito; SNS after write
Lambda→table IAM skip नका
Client (web / mobile) API Gateway HTTP APIJWT/IAM auth, throttle, routes Lambda handlerValidate, map status codes DynamoDBPK design from access patterns Optional: SNS / EventBridge after write
API backend — edge, compute, data.

4. HTTP vs REST vs Function URL

Optionकधीकाळजी
HTTP APIनवीन APIs; JWTREST पेक्षा कमी features
REST APIAPI keys/usage plansमहाग
Function URLQuick demosDIY routing

Ravindra Bagale's Tip

Default: HTTP API + JWT/IAM. REST फक्त गरज असेल तर.

5. How: request path

कसं करायचं (step by step)

  1. Routes कागदावर.
  2. Table from access patterns.
  3. Lambda handler.
  4. Least-privilege role.
  5. HTTP API + CORS.
  6. Auth + throttle.
  7. curl + logs.
  8. Optional SNS.

HTTP API create

aws apigatewayv2 create-api --name meera-momo-api --protocol-type HTTP --region ap-south-1 \
  --query '{Id:ApiId,Ep:ApiEndpoint}'

अर्थ: Invoke URL मिळते. Lab नंतर delete.

Route wire

aws apigatewayv2 create-integration --api-id $API --integration-type AWS_PROXY \
  --integration-uri $LAMBDA_ARN --payload-format-version 2.0 --region ap-south-1

अर्थ: AWS_PROXY = पूर्ण HTTP event.

6. Status codes

Codeअर्थClient
200Read OKBody
201Createdid ठेवा
202Async acceptPoll/webhook
400ValidationFix
401/403AuthRe-login
404MissingNot found
429ThrottleBackoff
500ServerRetry+alert

7. Mini-examples

Python response

def ok(body, code=200):
    import json
    return {"statusCode": code, "headers": {"content-type": "application/json"},
            "body": json.dumps(body)}
print(ok({"orderId": "o-1"}, 201)["statusCode"])

Output: 201. अर्थ: statusCode महत्त्वाचा.

Node validate

exports.handler = async (event) => {
  if (!event.body) return {statusCode: 400, body: '{"error":"empty"}'};
  return {statusCode: 200, body: event.body};
};

अर्थ: DynamoDB आधी fail fast.

curl smoke

curl -sS -X POST "$API_URL/orders" -H 'content-type: application/json' \
  -d '{"sku":"MOMO-STEAM","qty":2,"currency":"INR"}'

अर्थ: 201 + orderId किंवा 400.

8. Lab: sketch + optional API

Org block नसेल तर ap-south-1 (Mumbai). आधी budget alert.

कसं करायचं (step by step)

  1. Routes+keys+codes.
  2. Optional sandbox.
  3. curl + request ids.
  4. Throttle → 429.
  5. Cleanup.

Cleanup (billing थांबवा)

  1. API → Lambda → table → role.
  2. Log group.

9. Auth आणि multi-tenant

JWT gateway वर. Cognito. SaaS: tenantId token+PK — फक्त body वर विश्वास नका.

10. CORS

Browser CORS हवा; native mobile ला नको.

Routes

aws apigatewayv2 get-routes --api-id $API --region ap-south-1 --query 'Items[].RouteKey'

अर्थ: Routes confirm.

11. एकाच pattern — वेगवेगळ्या industries

  • Fintech (Aisha, Lagos): payment webhooks → SQS → Lambda.
  • Ecommerce (Rohan, Pune): checkout API; inventory SNS fan-out.
  • Healthcare (Mei, Singapore): SNS SMS; PHI + fine-grained IAM.
  • EdTech (Diego, São Paulo): EventBridge nightly cleanup.
  • Logistics (Fatima, Casablanca): EventBridge + saga.
  • Media (Kenji, Osaka): S3 → Lambda thumbs + fan-out.
  • SaaS (Priya, Bengaluru): Cognito + per-tenant partition.
  • Gov (Omar, Amman): strangler fig per form route.

12. Troubleshooting checklist

  1. Region confirm करा.
  2. CloudWatch Logs + request id.
  3. Execution role permissions.
  4. DLQ + oldest message age.
  5. curl -v + CORS.
  6. SFN event history.
  7. Linked chapter पुन्हा वाचा.
  8. Cost: concurrency/retries.

13. सामान्य errors आणि fixes

प्रत्येक row = खरी चूक. Panic करण्याआधी fix वाचा.

Error / लक्षणFix
Public Lambda IPGateway.
Default RESTHTTP API.
200 for create201/202.
IAM *Least privilege.
CORS *Explicit origin.
Rules in GatewayLambda.
No throttleThrottle.
Trust body tenantJWT claim.
Monolith handlerSplit.
Forgotten APIsDelete.

14. FAQ

प्र. HTTP API prod?

हो — WAF/auth.

प्र. Cognito?

Authorizer.

प्र. Function URL?

Demos vs routing.

प्र. RDS?

VPC cost; DDB prefer.

प्र. Version?

/v1 or stages.

प्र. Shortener?

Same pattern.

15. Interview Q&A (Beginner / Mid / Senior + MNC)

प्र. Beginner boxes

GW+Lambda+data.

प्र. Why Gateway

Routing/auth.

प्र. 201 vs 202

Created vs accepted.

प्र. Least privilege

PutItem on ARN.

प्र. Senior tenant

PK+JWT.

प्र. MNC auth

Edge+object authz.

16. Tips — English, मराठी आणि हिंदी

Ravindra Bagale's Tip (EN)

Keep the window, the cook, and the order book separate — Gateway, Lambda, DynamoDB.

Ravindra Bagale's Tip (MR)

खिडकी, cook आणि order book वेगळे ठेवा — Gateway, Lambda, DynamoDB.

Tip (HI)

खिड़की, रसोइया और ऑर्डर बुक अलग रखें — Gateway, Lambda, DynamoDB.

घरी करून बघा

  1. Side project sketch.
  2. Status codes list.

थोडक्यात सांगायचं तर

  1. गोष्टी पासून सुरू, मग services.
  2. Existing chapters link — labs copy नको.
  3. Failure साठी design.
  4. Measure → मग पैसे.

17. Payload आणि pagination

  1. Large files → S3 keys.
  2. nextToken; no unbounded.
  3. content-type check.
  4. Idempotency-Key.

18. Aliases नी blue/green

Versions + alias live; weighted shift. Integration = alias ARN, $LATEST नको.

Publish version

aws lambda publish-version --function-name meera-orders --region ap-south-1 --query Version

अर्थ: Rollback साठी version pin.

19. Contract tests

CI मध्ये curl contract tests — checkout customers आधी.

20. Architecture decision checklist

  1. User journey 5 bullets, no AWS names.
  2. Sync vs async mark.
  3. Failure mode.
  4. Idempotency key.
  5. Data from access patterns.
  6. Cost 1x and 10x.
  7. Quotas ap-south-1.
  8. Auth decision.
  9. On-call + alarm.
  10. Deep chapter link.
  11. मग console/IaC.

21. Copy-paste quick commands

# Region and identity
aws configure get region
aws sts get-caller-identity

# Lambda landscape
aws lambda list-functions --region ap-south-1 --max-items 20 \
  --query 'Functions[].{N:FunctionName,R:Runtime,T:Timeout,M:MemorySize}' --output table
aws lambda get-account-settings --region ap-south-1

# API Gateway (HTTP APIs)
aws apigatewayv2 get-apis --region ap-south-1 --query 'Items[].{N:Name,Id:ApiId,Ep:ProtocolType}' --output table

# Queues and topics
aws sqs list-queues --region ap-south-1
aws sns list-topics --region ap-south-1 --query 'Topics[].TopicArn'

# Event buses and state machines
aws events list-event-buses --region ap-south-1
aws stepfunctions list-state-machines --region ap-south-1 --query 'stateMachines[].name'

# DynamoDB tables (names only)
aws dynamodb list-tables --region ap-south-1

Sandbox + read-only IAM.

22. अजून industry आणि stack stories

  • Fintech Lagos: webhook → SQS → idempotent ledger.
  • Ecommerce Pune: throttle + fan-out.
  • Healthcare SG: SNS + PHI IAM.
  • EdTech: EventBridge cleanup.
  • Logistics: bus + saga.
  • Media: avoid recursive S3.
  • SaaS: tenant partition.
  • Gov: strangler peel.
  • Startup: Function URL → HTTP API.
  • Retail: FIFO vs Standard.
  • Python: thin handlers.
  • Node: async/await.
  • Java: SnapStart.
  • .NET: package size.
  • Go: static binary.
  • PHP: hybrid.

23. Glossary

Termअर्थ
Cold startपहिल्या invoke ची init
Idempotencyदोनदा = तोच effect
DLQFail झालेले messages
ChoreographyEvents; conductor नाही
OrchestrationSFN central
Fan-outएक → अनेक
Fan-inअनेक → join
Strangler figMonolith peel
SagaSteps + compensate
Provisioned concurrencyWarm N; pay

24. Practice: मोठ्याने explain

  1. 60s: serverless API backend गोष्ट.
  2. 60s: sync vs async checkout.
  3. 60s: Lambda नको कधी.
  4. 60s: DLQ + idempotency.
  5. Record ×2.

25. Safe lab hygiene

  • Tag owner+expiry.
  • Budget alarm आधी.
  • Read-only first.
  • Cleanup.
  • No secrets in env/screenshots.
  • No provisioned on hello.

27. Myths

Mythखरं
Servers नाहीतServers आहेत; manage नको
नेहमी स्वस्तSteady वर EC2
No OpsIAM/data/alarms
एक LambdaBounded contexts
Queues optionalLoad leveler

28. Serverless error budget

Checkout error budget 0.1% असेल तर aggressive retries email duplicates देऊन budget जाळू शकतात. Money SLO ≠ notify SLO.

  1. दोन SLOs.
  2. Money retries bounded.
  3. Notify best-effort.
  4. Money page; notify ticket.

29. Regional notes (ap-south-1)

  • Labs Mumbai.
  • Feature availability.
  • Data residency.
  • Latency India.

30. Naming conventions

# Suggested prefixes
# Lambda:  pay-intake-prod, pay-worker-prod
# SQS:     pay-orders.fifo / pay-orders-dlq
# SNS:     pay-order-events
# EB bus:  pay.domain
# SFN:     pay-refund-saga
# DDB:     pay-orders-prod

env + domain नावात ठेवा. CloudWatch grep सोपा.

31. Pre-mortems

  1. Double charge headline.
  2. DLQ drain plan.
  3. Cognito outage mode.
  4. Runbook.

32. Currency / locale

Currencies आणि names rotate करा — global mental model.

उजळणी, मुलाखत आणि तुलना cards

हा lesson काही मिनिटांत revise करा. Cards phone वर save करा किंवा PNG download करा. प्रत्येक card वरचा QR हा lesson उघडतो.

उजळणी card: API + Lambda + DynamoDB. No edge = messy. HTTP API → Lambda → DynamoDB. API → Lambda → PutItem. Food-cart stack.
उजळणी card: API + Lambda + DynamoDB · PNG download करा · Card page उघडा
उजळणी card: 201 vs 202. 200 hides truth. 201 sync; 202 async. Sync 201; queue 202. Codes first.
उजळणी card: 201 vs 202 · PNG download करा · Card page उघडा
तुलना: HTTP API vs REST API. HTTP API: Simple default. REST API: Rich when needed.
तुलना: HTTP API vs REST API · PNG download करा · Card page उघडा
तुलना: HTTP 201 vs HTTP 202. HTTP 201: Created now. HTTP 202: Accepted later.
तुलना: HTTP 201 vs HTTP 202 · PNG download करा · Card page उघडा
मुलाखत card: HTTP API default. REST habit. HTTP API first. Two reasons.
मुलाखत card: HTTP API default · PNG download करा · Card page उघडा
मुलाखत card: JWT at edge. Late auth. Authorizer at edge. JWT + PK.
मुलाखत card: JWT at edge · PNG download करा · Card page उघडा
मुलाखत card: AWS_PROXY event. Hidden event. Full event in. Query in event.
मुलाखत card: AWS_PROXY event · PNG download करा · Card page उघडा
मुलाखत card: Least-privilege PutItem. Wildcard fail. PutItem + ARN. Table ARN.
मुलाखत card: Least-privilege PutItem · PNG download करा · Card page उघडा

अजून cards: Ravi Sir's Cards.

सराव Tasks

या scenario बद्दल

Vidarbha Cotton Mills हा नागपूर मधला वस्त्रोद्योग व्यवसाय आहे. Ganesh Wankhede त्यांचं IT व cloud काम पाहतात. त्यांचे पैसे INR मध्ये मोजले जातात आणि AWS resources ap-south-1 Region मध्ये चालतात. खालच्या tasks मध्ये तुम्ही या lesson मध्ये शिकलेलं वापरून त्यांची खरी कामं करता. सगळी नावं, account IDs, IP addresses व numbers सरावासाठी काल्पनिक आहेत.

0 / 16

सोपं Window

का: Metaphor.

काय करायचं: Window तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं HTTP API

का: Default.

काय करायचं: HTTP API तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं 202

का: Codes.

काय करायचं: 202 Accepted तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं 201

का: Codes.

काय करायचं: 201 Created तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

सोपं Get-rest

का: CLI.

काय करायचं: get.

कसं करायचं:
  1. Task वाचा आणि ते कोणत्या AWS service बद्दल आहे ते ठरवा.
  2. aws लिहा, मग service चं नाव, मग operation (action शब्द).
  3. प्रत्येक आवश्यक flag (--name) व त्यापुढे task मधली value लिहा.
  4. Flags कोणत्याही क्रमाने चालतात. Check answer दाबा.

सोपं Stack

का: Parts.

काय करायचं: edge/compute/store.

कसं करायचं:
  1. Task मधल्या top-level keys पासून सुरू करा.
  2. Child keys 2 spaces ने indent करा (tabs नकोत).
  3. प्रत्येक property मध्ये task मधली value भरा.
  4. Check answer दाबा. Keys चा क्रम महत्त्वाचा नाही.

सोपं Stage

का: Ops.

काय करायचं: Stages तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Proxy

का: Integration.

काय करायचं: Full event तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम IAM

का: Scope.

काय करायचं: Scoped PutItem तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम JWT

का: Auth.

काय करायचं: Authorizer तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम URL

का: Scope.

काय करायचं: Demos तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

मध्यम Codes

का: Map.

काय करायचं: accepted/created.

कसं करायचं:
  1. Task मधल्या top-level keys पासून सुरू करा.
  2. Child keys 2 spaces ने indent करा (tabs नकोत).
  3. प्रत्येक property मध्ये task मधली value भरा.
  4. Check answer दाबा. Keys चा क्रम महत्त्वाचा नाही.

अवघड CORS

का: Web.

काय करायचं: Browsers तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड Tenant

का: SaaS.

काय करायचं: JWT+PK तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड Body

का: Anti.

काय करायचं: Body alone no तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.

अवघड Lab

का: Hygiene.

काय करायचं: Delete lab तर 1.

कसं करायचं:
  1. Task मधले numbers लिहून घ्या.
  2. कागदावर step by step काढा.
  3. Box मध्ये फक्त final उत्तर लिहा.
  4. Check answer दाबा.