Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Machine Learning with Amazon SageMaker Cookbook

You're reading from   Machine Learning with Amazon SageMaker Cookbook 80 proven recipes for data scientists and developers to perform machine learning experiments and deployments

Arrow left icon
Product type Paperback
Published in Oct 2021
Publisher Packt
ISBN-13 9781800567030
Length 762 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Joshua Arvin Lat Joshua Arvin Lat
Author Profile Icon Joshua Arvin Lat
Joshua Arvin Lat
Arrow right icon
View More author details
Toc

Table of Contents (11) Chapters Close

Preface 1. Chapter 1: Getting Started with Machine Learning Using Amazon SageMaker 2. Chapter 2: Building and Using Your Own Algorithm Container Image FREE CHAPTER 3. Chapter 3: Using Machine Learning and Deep Learning Frameworks with Amazon SageMaker 4. Chapter 4: Preparing, Processing, and Analyzing the Data 5. Chapter 5: Effectively Managing Machine Learning Experiments 6. Chapter 6: Automated Machine Learning in Amazon SageMaker 7. Chapter 7: Working with SageMaker Feature Store, SageMaker Clarify, and SageMaker Model Monitor 8. Chapter 8: Solving NLP, Image Classification, and Time-Series Forecasting Problems with Built-in Algorithms 9. Chapter 9: Managing Machine Learning Workflows and Deployments 10. Other Books You May Enjoy

Pushing the custom R algorithm container image to an Amazon ECR repository

In the previous recipe, we prepared and built the custom container image using the docker build command. In this recipe, we will push the custom container image to an Amazon ECR repository. If this is your first time hearing about Amazon ECR, it is simply a fully managed container registry that helps us manage our container images. After pushing the container image to an Amazon ECR repository, we will use this image for training and deployment in the Using the custom R algorithm container image for training and inference with Amazon SageMaker Local Mode recipe.

Getting ready

Here are the prerequisites for this recipe:

  • This recipe continues from the Building and testing the custom R algorithm container image recipe.
  • Permission to manage the Amazon ECR resources if you're using an AWS IAM user with a custom URL.

How to do it...

The initial steps in this recipe focus on creating the ECR repository. Let's get started:

  1. Use the search bar in the AWS Console to navigate to the Elastic Container Registry console. Click Elastic Container Registry when you see it in the search results:
    Figure 2.99 – Navigating to the ECR console

    Figure 2.99 – Navigating to the ECR console

    As we can see, we can use the search bar to quickly navigate to the Elastic Container Registry service.

  2. Click the Create repository button:
    Figure 2.100 – Create repository button

    Figure 2.100 – Create repository button

    Here, the Create repository button is at the top right of the screen.

  3. In the Create repository form, specify a Repository name. Use the value of $IMAGE_NAME from the Building and testing the custom R algorithm container image recipe. In this case, we will use chap02_r:
    Figure 2.101 – Create repository form

    Figure 2.101 – Create repository form

    Here, we have the Create repository form. For Visibility settings, we chose Private and we set the Tag immutability configuration to Disabled.

  4. Scroll down until you see the Create repository button. Leave the other configuration settings as-is and click Create repository:
    Figure 2.102 – Create repository button

    Figure 2.102 – Create repository button

    Finally, to complete the repository creation process, click the Create repository button at the bottom of the page.

  5. Click chap02_r:
    Figure 2.103 – Link to the ECR repository page

    Figure 2.103 – Link to the ECR repository page

    Here, we have a link under the Repository name column. Clicking this link should redirect us to a page containing details about the repository.

  6. Click View push commands:
    Figure 2.104 – View push commands button (upper right)

    Figure 2.104 – View push commands button (upper right)

    The View push commands button can be found at the top right of the page.

  7. You can optionally copy the first command, aws ecr get-login-password …, from the dialog box:
    Figure 2.105 – Push commands dialog box

    Figure 2.105 – Push commands dialog box

    Here, we can see multiple commands that we can use. We will only need the first one (aws ecr get-login-password …). Click the icon with two overlapping boxes at the right-hand side of the code box to copy the entire line to the clipboard.

  8. Navigate back to the AWS Cloud9 environment IDE and create a new Terminal. You can also reuse an existing one:
    Figure 2.106 – New Terminal

    Figure 2.106 – New Terminal

    The preceding screenshot shows us how to create a new Terminal. We click the green plus button and then select New Terminal from the list of options. Note that the green plus button is right under the Editor pane.

  9. Navigate to the ml-r directory:
    cd /home/ubuntu/environment/opt/ml-r
  10. Get the account ID using the following commands:
    ACCOUNT_ID=$(aws sts get-caller-identity | jq -r ".Account")
    echo $ACCOUNT_ID
  11. Specify the IMAGE_URI value and use the ECR repository name we specified while creating the repository in this recipe. In this case, we will run IMAGE_URI="chap02_r":
    IMAGE_URI="<insert ECR Repository URI>"
    TAG="1"
  12. Authenticate with Amazon ECR so that we can push our Docker container image to an Amazon ECR repository in our account later:
    aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com

    Important note

    Note that we have assumed that our repository is in the us-east-1 region. Feel free to modify this region in the command if needed. This applies to all the commands in this chapter.

  13. Use the docker tag command:
    docker tag $IMAGE_URI:$TAG $ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/$IMAGE_URI:$TAG
  14. Push the image to the Amazon ECR repository using the docker push command:
    docker push $ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/$IMAGE_URI:$TAG

Now that we have completed this recipe, we can proceed with using this custom algorithm container image with SageMaker in the next recipe. But before that, let's see how this works!

How it works…

In the Building and testing the custom R algorithm container image recipe, we used docker build to prepare the custom container image. In this recipe, we created an Amazon ECR repository and pushed our custom container image to it. We also used the docker push command to push the custom container image we built to the ECR repository.

Important note

Don't forget to include the api.r file inside the container when writing this Dockerfile and running the build step. The Python counterpart recipe copies the train and serve scripts to the /opt/ml directory inside the container, while the R recipe copies the train, serve, and api.r files to the /opt/ml directory. If the api.r file is not included, the following line in the serve script file will trigger an error and cause the script to fail: pr <- plumb("/opt/ml/api.r").

You have been reading a chapter from
Machine Learning with Amazon SageMaker Cookbook
Published in: Oct 2021
Publisher: Packt
ISBN-13: 9781800567030
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image