Created
January 24, 2023 23:25
-
-
Save ola0x/8398e39387df974e8139bb6d7002af89 to your computer and use it in GitHub Desktop.
example of deploying a model to SageMaker using the Boto3 SDK for Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Prepare and upload your model artifacts to an Amazon S3 bucket. The bucket must be in the same region as the SageMaker endpoint you will create. | |
#Create an IAM role with the necessary permissions for SageMaker to access the S3 bucket and other AWS services on your behalf. | |
#Use the AWS SDK for Python (Boto3) to create a SageMaker model resource. The model resource describes the location of the model artifacts and the Docker image containing the inference code. | |
#Create an endpoint configuration, which defines the resources that SageMaker should use when hosting the endpoint. This includes the number and type of instances to use, and the configurations for the model and the data processing. | |
#Use the endpoint configuration to create an endpoint. This deploys the model and makes it available for inference. | |
#Use the SDK to send requests to the endpoint and receive inferences. | |
import boto3 | |
# Specify your AWS Region | |
aws_region = '<aws_region>' | |
# Create a low-level SageMaker service client | |
sagemaker = boto3.client('sagemaker', region_name=aws_region) | |
# Create a SageMaker model | |
model_name = '<model_name>' | |
model_s3_path = 's3://<bucket_name>/<path_to_model_artifacts>' | |
image = '<docker_image>' | |
sagemaker.create_model(ModelName=model_name, | |
Containers=[{'Image': image, | |
'ModelDataUrl': model_s3_path}]) | |
# Create an endpoint configuration | |
config_name = '<config_name>' | |
instance_type = '<instance_type>' | |
sagemaker.create_endpoint_config(EndpointConfigName=config_name, | |
ProductionVariants=[{'InstanceType': instance_type, | |
'ModelName': model_name}]) | |
# Create an endpoint | |
endpoint_name = '<endpoint_name>' | |
sagemaker.create_endpoint(EndpointName=endpoint_name, | |
EndpointConfigName=config_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment