How to Invoke a SageMaker Endpoint: A Step-by-Step Guide

One of the key features of SageMaker is the ability to deploy machine learning models as endpoints, which can be invoked to make predictions on new data.

Amazon SageMaker is a fully managed machine learning service that enables data scientists and developers to build, train, and deploy machine learning models at scale. One of the key features of SageMaker is the ability to deploy machine learning models as endpoints, which can be invoked to make predictions on new data.

In this blog post, we will walk you through the process of invoking a SageMaker endpoint. We will cover the following topics:

  • Setting up a SageMaker endpoint
  • Creating an IAM role
  • Invoking the endpoint using the AWS SDK
CTA

Table of Contents

  1. Setting up a SageMaker endpoint
  2. Creating an IAM role
  3. Invoking the endpoint using the AWS SDK
  4. Conclusion

Setting up a SageMaker endpoint

The first step in invoking a SageMaker endpoint is to set up the endpoint itself. This involves creating a SageMaker model and deploying it to an endpoint.

To create a SageMaker model, you will need a trained machine learning model in one of the supported formats (e.g., TensorFlow, PyTorch, Scikit-learn). You can use SageMaker’s built-in algorithms or bring your own model.

Once you have a trained model, you can create a SageMaker model by following these steps:

  • Open the SageMaker console.
  • Click on “Create model”.
  • Give your model a name and choose the appropriate container image for your model.
  • Specify the location of your trained model artifacts (e.g., S3 bucket).
  • Specify any additional configuration settings (e.g., instance type, number of instances).

Once you have created your SageMaker model, you can deploy it to an endpoint by following these steps:

  • Open the SageMaker console.
  • Click on “Create endpoint”.
  • Give your endpoint a name and choose the appropriate configuration settings (e.g., instance type, number of instances).
  • Select the model you created in the previous step.
  • Click on “Create endpoint”.

Your SageMaker endpoint is now ready to be invoked.

Creating an IAM role

Before you can invoke a SageMaker endpoint, you need to create an IAM role that has permission to access the endpoint. This role will be used by the AWS SDK to authenticate your requests to the endpoint.

To create an IAM role, follow these steps:

  • Open the IAM console.
  • Click on “Roles”.
  • Click on “Create role”.
  • Choose “AWS service” as the type of trusted entity.
  • Choose “SageMaker” as the service that will use this role.
  • Choose the appropriate permissions for your role (e.g., read-only access to S3).
  • Give your role a name and click on “Create role”.

Make note of the ARN (Amazon Resource Name) of your IAM role, as you will need it in the next step.

CTA

Invoking the endpoint using the AWS SDK

Now that you have set up your SageMaker endpoint and created an IAM role, you are ready to invoke the endpoint using the AWS SDK. The AWS SDK provides a simple and convenient way to interact with SageMaker endpoints from your code.

To invoke a SageMaker endpoint using the AWS SDK, follow these steps:

  • Install the AWS SDK for your programming language of choice.
  • Create a new AWS SDK client object for the SageMaker service.
  • Create a SageMaker runtime client object using your IAM role ARN.
  • Prepare your input data in the appropriate format for your model.
  • Invoke the endpoint using the invoke_endpoint method of the SageMaker runtime client object.
  • Parse the output data returned by the endpoint.

Here is an example Python code snippet that demonstrates how to invoke a SageMaker endpoint using the AWS SDK:

import boto3
import json

# Create a SageMaker client object
sagemaker = boto3.client('sagemaker')

# Create a SageMaker runtime client object using your IAM role ARN
runtime = boto3.client('sagemaker-runtime',
                       aws_access_key_id=<your-access-key-id>,
                       aws_secret_access_key=<your-secret-access-key>,
                       aws_session_token=<your-session-token>,
                       region_name=<your-region-name>)

# Prepare your input data in the appropriate format for your model
input_data = {'input': [1, 2, 3, 4, 5]}

# Invoke the endpoint using the `invoke_endpoint` method of the SageMaker runtime client object
response = runtime.invoke_endpoint(EndpointName='<your-endpoint-name>',
                                   ContentType='application/json',
                                   Body=json.dumps(input_data))

# Parse the output data returned by the endpoint
output_data = json.loads(response['Body'].read().decode())
print(output_data)

In this example, we create a SageMaker client object and a SageMaker runtime client object using our IAM role ARN. We then prepare our input data in the appropriate format for our model, invoke the endpoint using the invoke_endpoint method of the SageMaker runtime client object, and parse the output data returned by the endpoint.

CTA

Conclusion

In this blog post, we have shown you how to invoke a SageMaker endpoint using the AWS SDK. We walked you through the process of setting up a SageMaker endpoint, creating an IAM role, and invoking the endpoint using the AWS SDK. We hope that this guide has been helpful and that you are now able to successfully invoke SageMaker endpoints in your own projects.


About Saturn Cloud

Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.