{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Part 4-0: Create a custom container"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We will be using [SageMaker Studio Pipelines](https://docs.aws.amazon.com/sagemaker/latest/dg/pipelines.html) to automate all of our steps that we have done so far. SageMaker Pipelines uses purpose built docker containers behind the scene to run jobs (aka [Steps](https://docs.aws.amazon.com/sagemaker/latest/dg/build-and-manage-steps.html)) in a sequence that you define (much like a DevOps CI/CD pipeline). You can build our own docker container with Python3, [Boto3 SDK](https://boto3.amazonaws.com/v1/documentation/api/latest/index.html) and [SageMaker Python SDK](https://github.com/aws/sagemaker-python-sdk) installs, so that you can make use of them to make calls to Amazon Fraud Detector APIs via Boto3 library and access SageMaker constructs such as Feature Store etc. via custom data processing scripts.\n",
"\n",
"To achieve that, you will first have to build a docker image and push it to an [ECR (Elastic Container Registry)](https://aws.amazon.com/ecr/) repo in your account. Typically this can be done using `docker` CLI and `aws cli` in your local machine pretty easily. However, SageMaker makes it even easier to use this studio environment to build, create, and push any custom container to your ECR repository using a purpose built tool known as `sagemaker-studio-image-build` and use the custom container image in your Notebooks for your ML projects. \n",
"\n",
"For more information on this, refer to [this blog post](https://aws.amazon.com/blogs/machine-learning/using-the-amazon-sagemaker-studio-image-build-cli-to-build-container-images-from-your-studio-notebooks/)\n",
"\n",
"Next, install this required CLI tool into our SageMaker environment."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Install sagemaker-studio-image-build CLI tool\n",
"!pip install sagemaker-studio-image-build"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 1. Grant appropriate permissions to SageMaker\n",
"---\n",
"In order to be able to use `sagemaker-studio-image-build`, we need to first add permission to SageMaker's IAM role so that it may perform actions on your behalf. Specifically, you would add Amazon ECR and Amazon CodeBuild permissions to it. Add the AmazonEC2ContainerRegistryFullAccess and AWSCodeBuildAdminAccess policies to your Sagemaker default role.\n",
"\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to this, you will also have to add `iam:PassRole` permission to the SageMaker Studio execution role. Add the Policy document below as an inline policy to the SageMaker Studio Execution role in IAM console."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```json\n",
"{\n",
" \"Version\": \"2012-10-17\",\n",
" \"Statement\": [\n",
" {\n",
" \"Effect\": \"Allow\",\n",
" \"Action\": \"iam:PassRole\",\n",
" \"Resource\": \"arn:aws:iam::*:role/*\",\n",
" \"Condition\": {\n",
" \"StringLikeIfExists\": {\n",
" \"iam:PassedToService\": \"codebuild.amazonaws.com\"\n",
" }\n",
" }\n",
" }\n",
" ]\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As a last and final step, you must also add a trust relationship in the SageMaker Studio Execution role to allow CodeBuild to assume this role. To add a trust relationship\n",
"* Navigate to IAM Console\n",
"* Search for your SageMaker execution role. (You can find your Sagemaker execution role name from Sagemaker Studio console)\n",
"* Click on the \"Trust Relationships\" tab > Click the \"Edit Trust relationship\" button\n",
"* Add the following Trust relationship to any pre-existing trust relationship\n",
"\n",
"```json\n",
"{\n",
" \"Effect\": \"Allow\",\n",
" \"Principal\": {\n",
" \"Service\": \"codebuild.amazonaws.com\"\n",
" },\n",
" \"Action\": \"sts:AssumeRole\"\n",
"}\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In a normal situation, your final trust relationship should look something like this\n",
"\n",
"```json\n",
"{\n",
" \"Version\": \"2012-10-17\",\n",
" \"Statement\": [\n",
" {\n",
" \"Effect\": \"Allow\",\n",
" \"Principal\": {\n",
" \"Service\": \"sagemaker.amazonaws.com\"\n",
" },\n",
" \"Action\": \"sts:AssumeRole\"\n",
" },\n",
" {\n",
" \"Effect\": \"Allow\",\n",
" \"Principal\": {\n",
" \"Service\": \"codebuild.amazonaws.com\"\n",
" },\n",
" \"Action\": \"sts:AssumeRole\"\n",
" }\n",
" ]\n",
"}\n",
"```\n",
"\n",
"
"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"