Vikram Ranabhatt
1 min readJun 4, 2022

--

R Model Training and Deployment on AWS Sagemaker

Problem statement:

Aim was to write model in R and use sagemaker api deploy endpoint.

Working:

Model writing, training and deploy in sagemaker needs following task.

  1. Write Model code in R- we use reticulate. It embdeds python session within R session. This allows us to use sagemaker estimator. we can use jupyter notebook or RStudio. In this case i used RStudio to create model(wrote the code in R)
  2. Create Sample R docker container. Refer the link
  3. Fetching data and do some pre processing work.AWS sagemaker provides PreProcessing job to do this. R Preproceeeing Job SagemakerREEProcess. Python Pre processing Job SagemakerPyPreProcess
  4. Training Job- Sagemaker provide training Job to train model. SagemakerRCodeTrainingStep
  5. Deploy Model- Sagemaker Estimator does the deployment of endpoint on sagemaker.
print("===============DEPLOY===============")
model_endpoint <- estimator$deploy(initial_instance_count = 1L,
instance_type = 'ml.t2.medium')

5. Expose the endpoint through API Gateway-Refer Resources section

Design:

This article is based on Bring your own algorithm, as not all AI/ML pipeline can. Data scientists writes code in R , There are very few article about CI/CD using R model on AWS Sage maker(I may be wrong).There are plenty article on Python CI/CD. So i decided to create one and post it for others to use it.

Summary:

This article is aims to create, train and deploy R Model on

  1. Create Sample R docker container
  2. Sample R Model code
  3. Preprocessing Job(Python)
  4. Train it on AWS Sagemaker using sagemaker training job
  5. Deploy it using serverless.com
  6. CI/CD pipeline

Reference:

https://github.com/chrisrana/r-model-sagemaker-api/blob/main/devops/serverless.yml

--

--

Vikram Ranabhatt

Have 15 years of experience in software development. Experience in building data pipeline and back end. Python, C++, AWS. Currently in MLOPS