Skip to main content

Search the Dev Center

Search tutorials, templates, examples, and more.

SageMaker AWS Python Starter

A Python program to deploy a Huggingface LLM model to Amazon SageMaker with CloudWatch monitoring

Before you begin#

For this walkthrough, you'll need:

Create a new project#

This starter deploys the NousResearch/Llama-2-7b-chat-hf model from Hugging Face to an Amazon SageMaker endpoint, with an IAM role and two CloudWatch alarms, and exports the endpoint’s name.

Terminal window
mkdir my-project; cd my-project
pulumi new sagemaker-aws-python

The pulumi new command walks you through creating and configuring the new project and stack. A stack is an instance of a project, and you can have many of them (dev, staging, prod), each with its own configuration settings. Depending on the language, you may also be asked for your preferred package manager or toolchain.

Then it asks for the template’s one setting, aws:region, the AWS region to deploy into:

KeyDescriptionDefault
aws:regionThe AWS region where resources will be createdus-east-1
You can change values like these later with pulumi config set :
Terminal window
pulumi config set aws:region <value>

When setup completes, you’ll see a few files:

  • Pulumi.yaml contains the project name and the language it’s written in.
  • Pulumi.dev.yaml contains the dev stack’s settings, including the values you just entered.
  • __main__.py is the program. It sets the model, the instance type and the model server’s settings.
  • huggingface_llm.py defines the component __main__.py uses, which creates the IAM role, the SageMaker model, endpoint configuration and endpoint, and the alarms.

Deploy the project#

The endpoint runs on an ml.g5.2xlarge GPU instance, which AWS bills by the hour until you destroy the stack. If your account’s quota for ml.g5.2xlarge for endpoint usage is 0, request an increase in Service Quotas before you deploy.

Deploy with pulumi up. It starts with a preview of what it’s about to create and asks before it changes anything. Choose yes to deploy.

The endpoint takes several minutes to come up, since SageMaker has to launch the instance and load the model.

Pulumi adds a random suffix to most resource names, so Llama2Llm-endpoint becomes something like Llama2Llm-endpoint-4f8a2c1. This is autonaming. It lets you deploy the same program to more than one stack, and lets Pulumi create a replacement before it deletes the old resource, without names colliding.

Terminal window
pulumi up

When it finishes, Pulumi prints the stack’s outputs, values it only knows once the resources exist. This program exports the endpoint’s name as EndpointName. Read it any time with pulumi stack output EndpointName, or pass it to another command. This one sends the model a prompt and prints its answer:

Terminal window
aws sagemaker-runtime invoke-endpoint \
--endpoint-name $(pulumi stack output EndpointName) \
--content-type application/json \
--cli-binary-format raw-in-base64-out \
--body '{"inputs": "In 3 words, name the biggest mountain on earth?"}' \
response.json && cat response.json

Customize the project#

The program creates a CloudWatch alarm on SageMaker’s ModelLatency metric with a threshold of 100,000 microseconds. Halve it: in huggingface_llm.py, change the HighLatency alarm’s threshold to 50000:

cloudwatch.MetricAlarm(f"{name}-HighLatency",
metric_name="ModelLatency",
namespace="AWS/SageMaker",
statistic="Average",
period=60,
evaluation_periods=1,
threshold=50000, # in microseconds

Deploy again. The preview shows the alarm as an update:

Terminal window
pulumi up

Then list the alarms with their thresholds. Their names start with the endpoint’s name:

Terminal window
aws cloudwatch describe-alarms \
--alarm-name-prefix $(pulumi stack output EndpointName) \
--query "MetricAlarms[].[AlarmName,Threshold]" \
--output table

To see what else you can add, browse the AWS provider in the Pulumi Registry.

Cleaning up#

Terminal window
pulumi destroy
pulumi stack rm

Related

The infrastructure platform for humans and agents.