
Learn Amazon SageMaker
By :

A common misconception is that you can't use SageMaker outside of the AWS cloud. Obviously, it is a cloud-based service, and its most appealing capabilities require cloud infrastructure to run. However, many developers like to set up their development environment their own way, and SageMaker lets them do that: in this section, you will learn how to install the SageMaker SDK on your local machine or on a local server. In later chapters, you'll learn how to train and deploy models locally.
It's good practice to isolate Python environments in order to avoid dependency hell. Let's see how we can achieve this using two popular projects: virtualenv
(https://virtualenv.pypa.io) and Anaconda (https://www.anaconda.com/).
If you've never worked with virtualenv
before, please read this tutorial before proceeding – https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/:
sagemaker
, and activate it:$ mkdir workdir $ cd workdir $ python3 -m venv sagemaker $ source sagemaker/bin/activate
boto3
, the SageMaker SDK, and the pandas
library (https://pandas.pydata.org/), which is also required:$ pip install boto3 sagemaker pandas
$ python3 Python 3.7.4 (default, Aug 13 2019, 15:17:50)>>> import boto3 >>> import sagemaker >>> print(boto3.__version__)1.12.39 >>> print(sagemaker.__version__)1.55.3 >>> exit
The installation looks fine. Your own versions will certainly be newer and that's fine. Now, let's run a quick test with a local Jupyter server (https://jupyter.org/). If Jupyter isn't installed on your machine, you can find instructions at https://jupyter.org/install:
$ pip install ipykernel $ python3 -m ipykernel install --user --name=sagemaker
$ jupyter notebook
sagemaker
kernel is available, so let's select it in the New menu, as seen in the following screenshot:Figure 1.2 Creating a new notebook
Figure 1.3 Checking the SDK version
This completes the installation with virtualenv
. Don't forget to terminate Jupyter, and to deactivate your virtualenv
:
$ deactivate
Anaconda includes a package manager named conda
that lets you create and manage isolated environments. If you've never worked with conda
before, you should do the following first:
Perform the following steps to install the Sagemaker SDK with conda:
conda
environment named conda-sagemaker
:$ conda create -y -n conda-sagemaker $ conda activate conda-sagemaker
pandas
, boto3
, and the SageMaker SDK. The latter has to be installed with pip
, as it's not available as a conda
package:$ conda install -y boto3 pandas $ pip install sagemaker
$ conda install -y jupyter ipykernel $ python3 -m ipykernel install --user --name conda-sagemaker
conda-sagemaker
kernel is present in the New menu, as is visible in the next screenshot:$ jupyter notebook
Figure 1.4 Creating a new conda environment
Figure 1.5 Checking the SDK version
This completes the installation with conda
. Whether you'd rather use it over virtualenv
is largely a matter of personal preference. You can definitely run all notebooks in this book and build your own projects with one or the other.
Amazon Identity and Access Management (IAM) enables you to manage access to AWS services and resources securely (https://aws.amazon.com/iam). Of course, this applies to Amazon SageMaker as well, and you need to make sure that your AWS user has sufficient permissions to invoke the SageMaker API.
Note:
If you're not familiar with IAM at all, please read the following documentation: https://docs.aws.amazon.com/IAM/latest/UserGuide/introduction.html
You can run a quick test by using the AWS CLI on one of the SageMaker APIs, for example, list-endpoints
. I'm using the eu-west-1
region here, but feel free to use the region that is nearest to you:
$ aws sagemaker list-endpoints --region eu-west-1 { "Endpoints": []}
If you get an error message complaining about insufficient permissions, you need to update the IAM role attached to your AWS user.
If you own the AWS account in question, you can easily do this yourself in the IAM console, by adding the AmazonSageMakerFullAccess
managed policy to your role. Note that this policy is extremely permissive: this is fine for a development account, but certainly not for a production account.
If you work with an account where you don't have administrative rights (such as a company-provided account), please contact your IT administrator to add SageMaker permissions to your AWS account.
For more information on SageMaker permissions, please refer to the documentation: https://docs.aws.amazon.com/sagemaker/latest/dg/security-iam.html.