
In this section, we will implement a Lambda function to call the model deployed on SageMaker Endpoint (step 6) and create a REST API Gateway so that the client can send inference requests. This is the important link to help turn the ML model into a complete prediction service.
Create a Lambda function that calls SageMaker Endpoint to handle inference.
Connect Lambda to API Gateway to create REST API.
Check inference from Postman or browser.
invoke-ml-endpointPython 3.9AmazonSageMakerFullAccess and AWSLambdaBasicExecutionRole)

Replace the default content in the Code tab with the following code:
import json
import boto3
import os
runtime = boto3.client('sagemaker-runtime')
ENDPOINT_NAME = os.environ.get('ENDPOINT_NAME', 'ml-blog-endpoint')
def lambda_handler(event, context):
try:
body = json.loads(event['body'])
features = body.get('features')
if features is None:
return {
"statusCode": 400,
"body": json.dumps({"error": "Missing 'features' in request body"})
}
response = runtime.invoke_endpoint(
EndpointName=ENDPOINT_NAME,
ContentType='application/json',
Body=json.dumps({"features": features})
)
result = json.loads(response['Body'].read().decode())
return {
"statusCode": 200,
"headers": {"Content-Type": "application/json"},
"body": json.dumps({"prediction": result})
}
except Exception as e:
return {
"statusCode": 500,
"body": json.dumps({"error": str(e)})
}
📌 Explanation:
📌 Save the Invoke URL for example:
https://abc123xyz.execute-api.ap-southeast-1.amazonaws.com/prod/predict

You can use Postman or curl command to test:
curl -X POST \
https://abc123xyz.execute-api.ap-southeast-1.amazonaws.com/prod/predict \
-H "Content-Type: application/json" \
-d '{
"features": [0.45, 0.12, 0.88, 0.33]
}'
{
"prediction": 1
}

Check Lambda logs in CloudWatch Logs → help debug if errors occur.
Monitor metrics like Invocations, 4XXError, Latency to ensure API stability.
💡 You can add API authentication using API Keys, Cognito User Pools, or IAM Auth if deploying in production.
🎉 You have successfully built a Lambda function to call SageMaker Endpoint, created REST API Gateway, and successfully tested inference.