This is an example of MLOps implementation using Amazon SageMaker and GitHub Actions.
In this example, we will automate a model-build pipeline that includes steps for data preparation, model training, model evaluation, and registration of that model in the SageMaker Model Registry. The resulting trained ML model is deployed from the model registry to staging and production environments upon the approval.
This is the link to the blog post.
The followings are prerequisites to completing the steps in this example:
If you don't have a CodeStar Connection to your GitHub account already, follow this link to create one.
Your CodeStar Connection ARN will look like this:
arn:aws:codestar-connections:us-west-2:account_id:connection/aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
In the above, aEXAMPLE-8aad-4d5d-8878-dfcab0bc441f
is the unique Id for this connection. We'll be using this Id when we create our SageMaker project later in this example.
We need to create a secret in AWS secret Manager that holds our GitHub personal access token. If you do not have a personal access token for GitHub, you need to create one following the instructions here.
Note: You can create either classic or fine-grained access token. However, make sure the token has access to the Contents and Actions (workflows, runs and artifacts) for that repository.
Then, go to the AWS Secrets Manager, click on Store a new secret, select “Other type of secret” for Choose Secret type, then give a name to your secret in the “key” and add your personal access token to its associated “value”- click next, type a name for your Secret name and click next and then store.
In order to give permission to the GitHub Actions to deploy the SageMaker endpoints in your AWS environment, you need to create an IAM user.
Use iam/GithubActionsMLOpsExecutionPolicy.json
to provide enough permission to this user to deploy your endpoints.
Next, generate an Access Key for this user. You'll use this key in the next step when you set up your GitHub Secrets.
The following are the steps to prepare your github account to run this example.
You can reuse an existing github repo for this example. However, it's easier if you create a new repository. This repository is going to contain all the source code for both sagemaker pipeline build and deployments.
Copy the contents of the seedcode
directory in the root of your github repository. e.g. .github
directory should be under the root of your github repo.
Got your GitHub repository - on the top of the repository, select Settings - then in the security section go to the Secrets and variables and choose Actions. Choose the New repository secret:
Note: This is the Access Key for the IAM user which you just created in the previous step.
In order to create a manual approval step in our deployment pipelines, we use GitHub Environments.
production
.Required reviewers
and then add the reviewers. You can choose yourself for this example.Note: Environment feature is not available in some types of GitHub plans. Check the documentation here.
Simply zip the lambda_function.py
and upload it to an S3 bucket.
cd lambda_functions/lambda_github_workflow_trigger
zip lambda-github-workflow-trigger.zip lambda_function.py
Then upload the lambda-github-workflow-trigger.zip
to a bucket which can be accessed later on by the ServiceCatalog.
Now, let's create a Lambda layer for the dependencies of the lambda_function which we just uploaded.
Create a python virtual environment and install the dependencies.
mkdir lambda_layer
cd lambda_layer
python3 -m venv .env
source .env/bin/activate
pip install pygithub
deactivate
Now let's create our zip file.
mv .env/lib/python3.9/site-packages/ python
zip -r layer.zip python
Publish the layer to AWS.
aws lambda publish-layer-version --layer-name python39-github-arm64 \
--description "Python3.9 pygithub" \
--license-info "MIT" \
--zip-file fileb://layer.zip \
--compatible-runtimes python3.9 \
--compatible-architectures "arm64"
Now, all of your functions can refer to this layer to satisfy their dependencies.
For further reading on Lambda Layer, visit this link.
At this stage we use the provided CloudFormation template to create a ServiceCatalog which helps us to create custom projects in SageMaker.
Before creating the ServiceCatalog, in the template.yml
file, change the S3Bucket
to your bucket which you have uploaded the lambda zip file.
GitHubWorkflowTriggerLambda:
...
Code:
S3Bucket: <your bucket>
S3Key: lambda-github-workflow-trigger.zip
...
Create or reuse your SageMaker Domain for the following steps. If you don't have a domain, use the instruction here to create your domain with a Quick Setup.
If the SageMaker-provided templates do not meet your needs (for example, you want to have more complex orchestration in the CodePipeline with multiple stages or custom approval steps), create your own templates. We recommend starting by using SageMaker-provided templates to understand how to organize your code and resources and build on top of it. https://docs.aws.amazon.com/sagemaker/latest/dg/sagemaker-projects-templates-custom.html
To do this, after you enable administrator access to the SageMaker templates,
log in to the https://console.aws.amazon.com/servicecatalog/
On the AWS Service Catalog console, under Administration, choose Portfolios.
Choose Create a new portfolio.
Name the portfolio SageMaker Organization Templates.
Download the template.yml to your computer. This template is a Cloud Formation tempalte that provisions all the CI/CD resources we need as configuarion and infrustruce as code. You can study the template in more details to see what resources are deployed as part of it. This template has been customised to integrate with GitHub and GitHub actions.
Choose the new portfolio.
Choose Upload a new product.
For Product name¸ enter a name for your template. We chose build-deploy-github.
For Description, enter my custom build and deploy template.
For Owner, enter your name.
Under Version details, for Method, choose Use a template file.
Choose Upload a template.
Upload the template you downloaded.
For Version title, choose 1.0.
Choose Review.
Review your settings and choose Create product.
Choose Refresh to list the new product.
Choose the product you just created.
On the Tags tab, add the following tag to the product:
Back in the portfolio details, you see something similar to the following screenshot (with different IDs).
On the Constraints tab, choose Create constraint.
For Product, choose build-deploy-github (the product you just created).
For Constraint type, choose Launch.
Under Launch Constraint, for Method, choose Select IAM role.
Choose AmazonSageMakerServiceCatalogProductsLaunchRole.
Choose Create.
On the Groups, roles, and users tab, choose Add groups, roles, users.
On the Roles tab, select the role you used when configuring your SageMaker Studio domain. This is where the SageMaker Domain Role can be found.
Choose Add access.
In the previous sections, you prepared the Custom MLOps project environment. Now, let's create a project using this template.
In the aws console, navigate to Amazon SageMaker Domains
Choose the domain that you want to create this project in.
From the Launch menu choose Studio. You'll be redirected to the SageMaker Studio environment.
In the Studio, from the left menu, under the Deployments, choose Projects
Select Create Project.
At the top of the list of templates, Choose Organization templates.
If you have gone through all the previous steps successfully, you should be able to see a new custom project template named build-deploy-github. Select that template and click on Select Project Template.
Besides to the Name and Description, you need to provide the following details:
Code Repository Info: This is the owner of your GitHub Repository, e.g. for a repository at https://github.com/pooyavahidi/my-repo
, the owner would be pooyavahidi
.
GitHub Repository Name: This is the name of the repository which you copied the seedcode in. It would be just the name of the repo. e.g. in https://github.com/pooyavahidi/my-repo
, the repo is my-repo
.
Codestar connection unique id: This is the unique Id of the CodeStar connection which you created in the previous steps.
Name of the secret in the Secrets Manager which stores GitHub token: This is the name of the Secret in the Secrets Manager which you have created and stored the GitHub Token.
GitHub workflow file for deployment. e.g. deploy.yml: This is the name of the GitHub workflow file (at .github/workflows/deploy.yml
location) where you have the deployment instructions. For this example, you can keep it as default which is deploy.yml
Click Create Project.
After creating your project, make sure you update the AWS_REGION
and SAGEMAKER_PROJECT_NAME
environment variables in your GitHub Workflow files accordingly. Workflow files are in your GitHub repo (copied from seedcode), inside .github/workflows
directory. Make sure you update both build.yml and deploy.yml files.
...
env:
AWS_REGION: <region>
SAGEMAKER_PROJECT_NAME: <your project name>
...
Now your environment is ready to go! You can start by exploring the pipelines
directory, make changes and push those changes to your github repository, and see how all the steps of build and then deploy are automated.
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.