write a aws lambda function that takes json as input and calls a sagemaker endpoint do…
- User
write a aws lambda function that takes json as input and calls a sagemaker endpoint do the inference and push that into s3 bucket
- Assistant
write a aws lambda function that takes json as input and calls a sagemaker endpoint do the inference and push that into s3 bucket
Alright, here's a Python example of an AWS Lambda function that takes a JSON input, calls a SageMaker endpoint for inference, and then pushes the inference results into an S3 bucket.
To achieve this, you will need the following:
The following AWS Lambda function is written in Python and uses the Boto3 library to interact with SageMaker and S3:
import json
import boto3
import os
# Initialize boto3 clients
sagemaker_runtime = boto3.client('sagemaker-runtime')
s3_client = boto3.client('s3')
# Environment variables for the SageMaker endpoint name and S3 bucket name
SAGEMAKER_ENDPOINT = os.environ['SAGEMAKER_ENDPOINT']
S3_BUCKET = os.environ['S3_BUCKET']
# Lambda function handler
def lambda_handler(event, context):
try:
# Read the input JSON from the event
input_data = event['body'] # Modify as per the structure of your API or input source
# Convert input JSON to string
input_str = json.dumps(input_data)
# Invoke SageMaker endpoint
response = sagemaker_runtime.invoke_endpoint(
EndpointName=SAGEMAKER_ENDPOINT,
ContentType='application/json',
Body=input_str
)
# Read the inference response
result = response['Body'].read().decode('utf-8')
# Optionally, parse the result into JSON if needed
inference_result = json.loads(result)
# Define a unique filename for S3
file_name = f"inference_results_{context.aws_request_id}.json"
# Upload inference result to S3
s3_client.put_object(
Bucket=S3_BUCKET,
Key=file_name,
Body=json.dumps(inference_result)
)
# Return the S3 file URL
file_url = f"s3://{S3_BUCKET}/{file_name}"
return {
'statusCode': 200,
'body': json.dumps({'message': 'Inference successful', 'file_url': file_url})
}
except Exception as e:
return {
'statusCode': 500,
'body': json.dumps({'message': 'Error during inference or saving to S3', 'error': str(e)})
}
Initialization: Initialize the Boto3 clients for SageMaker Runtime and S3.
Environment Variables:
SAGEMAKER_ENDPOINT: The name of the deployed SageMaker endpoint, provided as an environment variable.S3_BUCKET: The S3 bucket name where inference results are stored, provided as an environment variable.Lambda Function Handler:
event object. You may need to adjust this based on how your input is structured.invoke_endpoint method.Create Lambda Function:
Set Environment Variables:
SAGEMAKER_ENDPOINT: Set this to the name of your SageMaker endpoint.S3_BUCKET: Set this to your S3 bucket name.IAM Roles/Permissions:
AmazonSageMakerFullAccess and AmazonS3FullAccess (or more restrictive policies as needed).Test the Lambda Function:
By following these steps, you should be able to deploy a Lambda function that processes JSON input, performs inference using SageMaker, and stores the results in an S3 bucket.
Not yet included in a published release.