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:
- The Pulumi CLI
- An AWS account with programmatic access
- The AWS CLI (optional, but recommended and used in this guide)
- Permission to manage SageMaker and IAM
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.
mkdir my-project; cd my-projectpulumi new sagemaker-aws-pythonmkdir my-project; cd my-projectpulumi new sagemaker-aws-pythonThe 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:
| Key | Description | Default |
|---|---|---|
| aws:region | The AWS region where resources will be created | us-east-1 |
pulumi config set
:
pulumi config set aws:region <value>pulumi config set aws:region "<value>"When setup completes, you’ll see a few files:
Pulumi.yamlcontains the project name and the language it’s written in.Pulumi.dev.yamlcontains thedevstack’s settings, including the values you just entered.__main__.pyis the program. It sets the model, the instance type and the model server’s settings.huggingface_llm.pydefines the component__main__.pyuses, 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.
pulumi upWhen 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:
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.jsonCustomize 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 microsecondsDeploy again. The preview shows the alarm as an update:
pulumi upThen list the alarms with their thresholds. Their names start with the endpoint’s name:
aws cloudwatch describe-alarms \ --alarm-name-prefix $(pulumi stack output EndpointName) \ --query "MetricAlarms[].[AlarmName,Threshold]" \ --output tableTo see what else you can add, browse the AWS provider in the Pulumi Registry.
Cleaning up#
You can destroy the stack and all of its infrastructure with pulumi destroy, then remove the stack with pulumi stack rm:
pulumi destroypulumi stack rm