Wednesday, 25 June 2025

What Is AWS API Gateway

 


๐Ÿšช What Is AWS API Gateway?

AWS API Gateway is a fully managed service that allows you to create, publish, monitor, and secure REST, HTTP, and WebSocket APIs.

✅ Use Cases:

  • Front-end to Lambda functions

  • Backend for mobile/web apps

  • Proxy to internal services

  • Integrate with AWS services like S3, DynamoDB


๐Ÿงฑ Architecture: API Gateway + Lambda

plaintext
Client (Browser / App / Curl) ↓ (HTTP) API Gateway (REST API) ↓ AWS Lambda (Business Logic) ↓ (Optional) DynamoDB / S3 / RDS

๐Ÿงช Example Goal

EndpointMethodFunctionality
/helloGETReturns greeting message
/submitPOSTAccepts JSON and stores it

๐Ÿ”ง Step-by-Step Setup


✅ Step 1: Create Lambda Functions

๐Ÿ“ Lambda 1: HelloFunction (for GET)

python
def lambda_handler(event, context): return { "statusCode": 200, "body": "Hello from Lambda!" }

๐Ÿ“ Lambda 2: SubmitFunction (for POST)

python
import json def lambda_handler(event, context): body = json.loads(event["body"]) name = body.get("name", "Guest") return { "statusCode": 200, "body": json.dumps({"message": f"Hello, {name}!"}) }

๐Ÿ”ผ Deploy these Lambda functions in AWS Console or CLI


✅ Step 2: Create a REST API in API Gateway

  1. Open API Gateway → Create API → Select REST API (not HTTP API)

  2. Choose:

    • Name: MyRestAPI

    • Endpoint Type: Regional

    • Click Create API


✅ Step 3: Create Resources and Methods

๐Ÿ“ Create /hello resource

  1. Select your API → Resources

  2. Click Actions > Create Resource

  3. Resource Name: hello, Resource Path: /hello

๐Ÿ“ Add GET Method to /hello

  1. Select /hello → Actions → Create Method → Select GET

  2. Integration type: Lambda Function

  3. Region: Your region

  4. Function name: HelloFunction

  5. Save → Allow permissions


๐Ÿ“ Create /submit resource

  1. Actions → Create Resource

  2. Resource Path: /submit

๐Ÿ“ Add POST Method to /submit

  1. Select /submit → Create Method → POST

  2. Integration: Lambda Function

  3. Function name: SubmitFunction

  4. Save → Allow permissions


✅ Step 4: Enable CORS (if needed)

  1. For both /hello and /submit

  2. Actions → Enable CORS

  3. Save and Deploy again


✅ Step 5: Deploy the API

  1. Click Actions > Deploy API

  2. Deployment stage: prod (or create a new one)

  3. Click Deploy

You’ll get an endpoint like:

bash
https://abcd1234.execute-api.us-east-1.amazonaws.com/prod

✅ Step 6: Access the API

๐Ÿ”น Test GET endpoint:

bash
curl https://abcd1234.execute-api.us-east-1.amazonaws.com/prod/hello

Output:

json
Hello from Lambda!

๐Ÿ”น Test POST endpoint:

bash
curl -X POST \ -H "Content-Type: application/json" \ -d '{"name": "Mitesh"}' \ https://abcd1234.execute-api.us-east-1.amazonaws.com/prod/submit

Output:

json
{"message": "Hello, Mitesh!"}

✅ Optional: Secure with IAM/Key/Auth

MethodUse case
API KeyRate limiting and usage plans
IAMInternal access via roles
Cognito AuthorizerUser-based authentication
Lambda AuthorizerCustom auth logic

๐Ÿ“ฆ Summary

StepDescription
Create LambdaBusiness logic (GET/POST)
Create REST APIIn API Gateway
Add Resources & Methods/hello (GET), /submit (POST)
Integrate with LambdaSelect your function and region
Deploy APIUse prod or any stage
Access & TestVia browser, Postman, or curl

No comments:

Post a Comment