ml
How to Deploy a Large Language Model from Hugging Face to AWS: Part 3 β API Gateway Setup
π Part 3: Creating an HTTP API in AWS API Gateway to Access Lambda (with Screenshot Guides)
1. Create an HTTP API via API Gateway
Purpose: The HTTP API allows you to expose the Lambda function via a public HTTP endpoint that you can call from external applications (e.g., Postman, Jupyter Notebook).
Steps:
- Go to the AWS Console β API Gateway β APIs.
- Click on Create API.
- Choose HTTP API and click Build.
πΉ Screenshot Tip:
πΉ Screenshot of the API Gateway landing page β Create API
2. Configure API Integration with Lambda
Steps:
- Add Integration:
- Integration type: Lambda function.
- Choose your Lambda function:
generate-text-lamini. - Check the box Add permissions to Lambda function β this allows API Gateway to invoke your Lambda automatically.
- Click Next.
πΉ Screenshot Tip:
πΉ Screenshot the Add Integration screen showing your Lambda function selected
3. Configure Route
Steps:
- Define Route:
- Method: POST
- Resource path:
/generate
- Click Next.
πΉ Screenshot Tip:
πΉ Screenshot the Route configuration screen showing POST and /generate
4. Configure Stage
Steps:
- Stage name: leave as $default (or set a custom stage name if preferred).
- Enable auto-deploy (this publishes changes automatically without extra steps).
- Click Next β Create.
πΉ Screenshot Tip:
πΉ Screenshot the Stage configuration screen with $default and Auto deploy enabled
5. Find Your Invoke URL
Steps:
- After creation, you will land on the API dashboard.
- Youβll see the Invoke URL at the top.
Format example:
https://your-api-id.execute-api.us-east-2.amazonaws.com
- Append
/generateto the Invoke URL.
πΉ Final API Endpoint:
https://your-api-id.execute-api.us-east-2.amazonaws.com/generate
πΉ Screenshot Tip:
πΉ Screenshot of the API Invoke URL and the Routes tab showing /generate
6. (Optional) Test API Directly in AWS Console
- Go to API Gateway β APIs β your API β Routes β POST /generate β Test.
- Request Body:
{
"prompt": "Write a poem about the ocean."
}
- Click Test and verify the Lambda/SageMaker pipeline works.
πΉ Screenshot Tip:
πΉ Screenshot of the API Gateway testing console showing request/response
7. Example API Request from External Client (Python)
You can now interact with your model remotely using a simple Python script.
import requests
# Replace with your actual invoke URL
url = "https://your-api-id.execute-api.us-east-2.amazonaws.com/generate"
data = {"prompt": "Write an article about deploying LLM models to AWS services"}
response = requests.post(url, json=data)
print(response.json())
πΉ Screenshot Tip:
πΉ Optional: Screenshot of API call from a Jupyter Notebook or terminal
πΉ Summary Checklist:
- β Created HTTP API with Lambda integration.
- β
Configured
POST /generateroute linked to Lambda. - β
Auto-deployed to
$defaultstage. - β Retrieved and validated public HTTP endpoint.
- β Tested end-to-end pipeline from API Gateway β Lambda β SageMaker Endpoint.
πΉ Important Notes:
- β
Ensure your Lambda functionβs IAM role includes
sagemaker:InvokeEndpointpermission (covered in Part 2). - β The Invoke URL is what you will use to send prompts to your model.
- β You can monitor Lambda/API logs in CloudWatch Logs.
πΉ Next Step: You are now ready to send HTTP requests to your SageMaker-powered LLM model through API Gateway!