How to Expose a Python-based AWS Lambda Function as a REST API using API Gateway?
Share
Condition for Expose a Python-based AWS Lambda Function as a REST API using API Gateway
Description: It demonstrates how to expose a Lambda function as a REST API endpoint using Amazon API Gateway. The Lambda function contains a simple Python handler that returns a static message and HTTP status code. After the Lambda is created, API Gateway is configured to trigger the function via an HTTP request. Once deployed, users can access the endpoint publicly over the internet. This workflow illustrates how serverless compute and API Gateway work together to provide fully managed, scalable backend services without requiring servers.
Steps
Step 1: Create the Lambda Function
Open AWS Console → Lambda.
Click Create function.
Select Author from scratch.
Enter:
Function name: lambda-api-demo
Runtime: Python 3.10 (or any available)
Architecture: x86_64
Leave permissions as default.
Click Create function.
Step 2: Add the Python Code
In the Lambda code editor, replace the default code with:
Step 3: Create a REST API (API Gateway)
Open AWS Console → API Gateway.
Click Create API.
Choose REST API → Build.
Under "Create new API":
Select New API
Name: lambda-api-service
Click Create API.
Step 4: Create a Resource
In the left panel, click Actions → Create Resource.
Resource name: hello
Resource path: /hello
Click Create Resource.
Step 5: Create a GET Method
Select the /hello resource.
Click Actions → Create Method.
Choose GET → ✓.
Set:
Integration type: Lambda Function
Use Lambda Proxy Integration: Enabled
Region: choose your region
Lambda Function: lambda-api-demo
Click Save.
When prompted for permission, click OK (API Gateway will add invoke permissions).
Step 6: Deploy the API
Click Actions → Deploy API.
Deployment stage:
Choose New Stage
Stage name: prod
Click Deploy.
Step 7: Test the Endpoint
After deployment, you will get an Invoke URL, for example:
https://abcd1234.execute-api.ap-south-1.amazonaws.com/prod/hello
Open it in your browser.
Output: Hello from API