๐ช 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
๐งช Example Goal
Endpoint | Method | Functionality |
---|---|---|
/hello | GET | Returns greeting message |
/submit | POST | Accepts JSON and stores it |
๐ง Step-by-Step Setup
✅ Step 1: Create Lambda Functions
๐ Lambda 1: HelloFunction
(for GET)
๐ Lambda 2: SubmitFunction
(for POST)
๐ผ Deploy these Lambda functions in AWS Console or CLI
✅ Step 2: Create a REST API in API Gateway
-
Open API Gateway → Create API → Select REST API (not HTTP API)
-
Choose:
-
Name:
MyRestAPI
-
Endpoint Type:
Regional
-
Click Create API
-
✅ Step 3: Create Resources and Methods
๐ Create /hello
resource
-
Select your API → Resources
-
Click Actions > Create Resource
-
Resource Name:
hello
, Resource Path:/hello
๐ Add GET
Method to /hello
-
Select
/hello
→ Actions → Create Method → SelectGET
-
Integration type: Lambda Function
-
Region: Your region
-
Function name:
HelloFunction
-
Save → Allow permissions
๐ Create /submit
resource
-
Actions → Create Resource
-
Resource Path:
/submit
๐ Add POST
Method to /submit
-
Select
/submit
→ Create Method → POST -
Integration: Lambda Function
-
Function name:
SubmitFunction
-
Save → Allow permissions
✅ Step 4: Enable CORS (if needed)
-
For both
/hello
and/submit
-
Actions → Enable CORS
-
Save and Deploy again
✅ Step 5: Deploy the API
-
Click Actions > Deploy API
-
Deployment stage:
prod
(or create a new one) -
Click Deploy
You’ll get an endpoint like:
✅ Step 6: Access the API
๐น Test GET
endpoint:
Output:
๐น Test POST
endpoint:
Output:
✅ Optional: Secure with IAM/Key/Auth
Method | Use case |
---|---|
API Key | Rate limiting and usage plans |
IAM | Internal access via roles |
Cognito Authorizer | User-based authentication |
Lambda Authorizer | Custom auth logic |
๐ฆ Summary
Step | Description |
---|---|
Create Lambda | Business logic (GET/POST) |
Create REST API | In API Gateway |
Add Resources & Methods | /hello (GET), /submit (POST) |
Integrate with Lambda | Select your function and region |
Deploy API | Use prod or any stage |
Access & Test | Via browser, Postman, or curl |
No comments:
Post a Comment