The Number Classification API is a serverless application hosted on AWS Lambda and exposed via API Gateway. It takes a number as a query parameter and returns interesting mathematical properties, including whether the number is prime, perfect, an Armstrong number, and a fun fact.
- An AWS account
- Basic knowledge of AWS Lambda and API Gateway
- Determines if a number is prime and perfect.
- Identifies special properties like Armstrong and odd/even.
- Computes the sum of digits.
- Fetches a fun fact about the number from Numbers API.
- Handles CORS for cross-origin requests.
- Returns JSON responses with appropriate HTTP status codes.
Endpoint:
GET <your-api-url>/api/classify-number?number=371
Response Example (200 OK):
{
"number": 371,
"is_prime": false,
"is_perfect": false,
"properties": ["armstrong", "odd"],
"digit_sum": 11,
"fun_fact": "371 is an Armstrong number because 3^3 + 7^3 + 1^3 = 371"
}Response Example (400 Bad Request):
{
"number": "invalid_input",
"error": true
}This guide walks you through deploying the Number Classification API on AWS using AWS Lambda and API Gateway (REST API).
- Sign in to the AWS Management Console.
- Navigate to AWS Lambda and click Create function.
- Select Author from scratch and enter:
- Function name:
NumberClassificationAPI - Runtime: Python 3.x
- Execution role: Create a new role with basic Lambda permissions.
- Function name:
- Click Create function.
- In the Lambda function page, go to Code and click Upload from →
.zip file. - Upload the ZIP file containing your function code and dependencies (ensure
requestsis included). - Set Handler - Ensure the handler name matches your main function (e.g.,
lambda_function.lambda_handlerif your file islambda_function.py) -
- Click Deploy
-
Navigate to API Gateway
- Go to the AWS Management Console
- Search for API Gateway and open the service
-
Create a New API
- Click Create API
- Select REST API (not HTTP or WebSocket)
- Choose Build
- Set an API name (e.g.,
NumberClassificationAPI) - Click Create API
-
Create a New Resource
- In the Resources section, click Actions → Create Resource
- Set Resource Name:
classify-number - Click Create Resource
-
Create a Method (GET)
- Select the
classify-numberresource - Click Actions → Create Method
- Choose GET and click the checkmark
- In the Integration Type, select Lambda Function
- Enter the name of your Lambda function (e.g.,
NumberClassificationAPI) - Click Save and confirm the permissions
- Select the
-
Enable Lambda Proxy Integration
- Select the GET method
- Check Use Lambda Proxy Integration
- Click Save
-
In API Gateway, navigate to your API and go to CORS settings.
-
Set allowed origins to
*. -
Set allowed methods to
GET. -
Add response headers:
Access-Control-Allow-Headers:Content-TypeAccess-Control-Allow-Methods:GETAccess-Control-Allow-Origin:*
-
Save
-
Create a Deployment Stage
- Click Actions → Deploy API
- Create a new stage (e.g.,
prod) - Click Deploy
-
Get the Invoke URL
- After deployment, copy the Invoke URL from the Stages section
- Your API will be accessible at:
https://your-api-id.execute-api.your-region.amazonaws.com/prod/api/classify-number?number=371
- Copy your Invoke URL from API Gateway.
- Test the API using a browser, Postman, or curl:
curl -X GET "<your-api-url>/api/classify-number?number=371" - Ensure you receive a valid JSON response.
Your Number Classification API is now fully deployed and accessible. 🚀 Test your endpoint, validate responses, and share your API URL.
Happy coding! 🎉