Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Errors while using sagemaker api to invoke endpoints

I've deployed an endpoint in sagemaker and was trying to invoke it through my python program. I had tested it using postman and it worked perfectly ok. Then I wrote the invocation code as follows

import boto3
import pandas as pd
import io
import numpy as np

def np2csv(arr):
    csv = io.BytesIO()
    np.savetxt(csv, arr, delimiter=',', fmt='%g')
    return csv.getvalue().decode().rstrip()


runtime= boto3.client('runtime.sagemaker')
payload = np2csv(test_X)

runtime.invoke_endpoint(
    EndpointName='<my-endpoint-name>',
    Body=payload,
    ContentType='text/csv',
    Accept='Accept'
)

Now whe I run this I get a validation error

ValidationError: An error occurred (ValidationError) when calling the InvokeEndpoint operation: Endpoint <my-endpoint-name> of account <some-unknown-account-number> not found.

While using postman i had given my access key and secret key but I'm not sure how to pass it when using sagemaker apis. I'm not able to find it in the documentation also.

So my question is, how can I use sagemaker api from my local machine to invoke my endpoint?

like image 910
Sujay DSa Avatar asked Jan 25 '18 08:01

Sujay DSa


2 Answers

I also had this issue and it turned out to be my region was wrong.

Silly but worth a check!

like image 76
Jack Avatar answered Sep 28 '22 06:09

Jack


When you are using any of the AWS SDK (including the one for Amazon SageMaker), you need to configure the credentials of your AWS account on the machine that you are using to run your code. If you are using your local machine, you can use the AWS CLI flow. You can find detailed instructions on the Python SDK page: https://aws.amazon.com/developers/getting-started/python/

Please note that when you are deploying the code to a different machine, you will have to make sure that you are giving the EC2, ECS, Lambda or any other target a role that will allow the call to this specific endpoint. While in your local machine it can be OK to give you admin rights or other permissive permissions, when you are deploying to a remote instance, you should restrict the permissions as much as possible.

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "sagemaker:InvokeEndpoint",
            "Resource": "arn:aws:sagemaker:*:1234567890:endpoint/<my-endpoint-name>"
        }
    ]
}
like image 44
Guy Avatar answered Sep 28 '22 06:09

Guy