Skip to content

Sagemaker Serverless Endpoint

0

I got this issue.

Error: Got unexpected extra argument (serve)

This is my Dockerfile.

FROM python:3.11

Set working directory

WORKDIR /opt/ml/code

Copy the FastAPI app and requirements file

COPY inference.py /opt/ml/code/inference.py COPY requirements.txt /opt/ml/code/requirements.txt

Install dependencies

RUN pip install --no-cache-dir -r requirements.txt

Expose the port for the FastAPI app

EXPOSE 8080

Run the FastAPI app using Uvicorn

ENTRYPOINT ["uvicorn", "inference:app", "--reload", "--host", "0.0.0.0", "--port", "8080"]

asked 2 years ago311 views

1 Answer
0

The error you're encountering suggests that there's a mismatch between how SageMaker expects to run your container and how your Dockerfile is configured.

For SageMaker endpoints, including serverless ones, the container needs to be set up slightly differently. SageMaker expects to be able to run your container with a 'serve' argument, which your current ENTRYPOINT doesn't accommodate.

To resolve this, you should modify your Dockerfile in the following way:

  1. Instead of directly running uvicorn in the ENTRYPOINT, create a separate script (e.g., 'serve.py') that will start your FastAPI application.

  2. Make this script executable and use it as your ENTRYPOINT.

Here's how you can modify your Dockerfile:

FROM python:3.11

WORKDIR /opt/ml/code

COPY inference.py requirements.txt serve.py ./

RUN pip install --no-cache-dir -r requirements.txt

RUN chmod +x serve.py

EXPOSE 8080

ENTRYPOINT ["python", "serve.py"]

Then, create a 'serve.py' file with the following content:

import subprocess
import sys

if __name__ == "__main__":
    subprocess.run(["uvicorn", "inference:app", "--host", "0.0.0.0", "--port", "8080"])

This setup allows SageMaker to run your container with the 'serve' argument, which it will ignore, while still starting your FastAPI application correctly.

Remember to ensure that your 'inference.py' file contains a FastAPI app named 'app', as that's what the uvicorn command in 'serve.py' expects.

Also, make sure your container can respond to health check requests on the '/ping' endpoint and handle inference requests on the '/invocations' endpoint, as these are required by SageMaker.

By making these changes, your container should be compatible with SageMaker's expectations for serverless endpoints.
Sources
Custom Inference Code with Hosting Services - Amazon SageMaker AI
Sagemaker endpoint creation failing | AWS re:Post

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.