CICD Pipeline : From Jupyter Notebook to AWS Sagemaker Endpoint

Naresh Reddy
Analytics Vidhya
Published in
3 min readAug 7, 2020

In this post I’ll walk through, how to build a CICD pipeline to deploy Machine learning model in AWS. To build the CICD pipeline, I’ve used AWS Code Commit(code repository) and CodeBuild ,CloudWatch, ECR, Lambda.

I’ll break the process into 3 steps.

Step 1: Create a CodeCommit repository and integrate with Sagemaker Jupyter Notebooks.

Step 2: Integrate CodeCommit to trigger a CodeBuild project which builds a Docker image and push it to the ECR repository.

Step 3: Lambda function to create a Sagemaker endpoint.

Step 1 : Create a CodeCommit repository and integrate with Sagemaker Jupyter Notebooks.

Create a repository in CodeCommit, From local either push or upload the inference code, nginx.conf,serve,wsgi.py along with dockerfile to CodeCommit.

Code : https://github.com/NareshReddyy/Sagemaker_deploy_own_model.git

You can refer to this article on how to deploy own model on Sagemaker.

Now integrate Sagemaker Notebook with CodeCommit using Sagemaker Notebook Git repositories settings, you’ve the option to use GitHub in place of CodeCommit .

Step 2 : Integrate CodeCommit to trigger a CodeBuild project which builds a Docker image and push it ECR repository.

After pushing your code to CodeCommit repository, create a CodeBuild project.

Select the CodeCommit Repository, which has source code.

When you call AWS CodeBuild to run a build, you must provide information about the build environment, which represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build.

Create a buildspec file that CodeBuild uses to run a build, which contains the build commands and related settings. Create buildspec.yml and save it in the root(top level) directory in CodeCommit repository.

Environment variables for buildspec file

Create a Lambda function add event to start a build in CodeBuild whenever someone commits to CodeCommit repository.

In Trigger configuration, select the code repository, events, branch on which trigger should be configured, and in the custom data pass the CodeBuild project name.

Step 3: Lambda function to create a Sagemaker endpoint.

Create a lambda function to create Sagemaker endpoint with a trigger from CloudWatch rule, where any commits to the ECR repository triggers lambda.

In order to create Sagemaker endpoint, you need to create Sagemaker endpoint Configuration, model.

You can also use CloudFormation, which is IaC(Infrastructure as Code) to deploy the code and create Sagemaker endpoint.

Please do comment about possible improvements and suggestions.

--

--