Amazon SageMaker Studio is a fully integrated development environment (IDE) for machine learning (ML) based in part on Jupyter Lab 3. Studio provides a web-based interface to interactively perform the ML development tasks required to prepare data and create, train, and deploy ML models. In Studio, you can load data, tune ML models, move between steps to tune experiments, compare results, and deploy ML models for inference.
The AWS Cloud Development Kit (AWS CDK) is an open source software development framework for building AWS CloudFormation stacks through the automatic generation of CloudFormation templates. A stack is a collection of AWS resources that can be programmatically updated, moved, or deleted. AWS CDK constructs are the building blocks of AWS CDK applications and represent the blueprint for defining cloud architectures.
Configuring Studio with the AWS CDK has become a streamlined process. The AWS CDK enables you to use native builds to define and deploy Studio using infrastructure as code (IaC), including AWS Identity and Access Management (AWS IAM) permissions and desired cloud resource configurations, all in one place. This development approach can be used in combination with other common software engineering best practices, such as automated code deployments, testing, and CI/CD pipelines. AWS CDK reduces the time required to perform typical infrastructure deployment tasks and reduces the surface area for human error through automation.
This post walks you through the steps to start setting up and deploying Studio to standardize ML model development and collaboration with other ML scientists and engineers. All the examples in the post are written in the Python programming language. However, the AWS CDK offers built-in support for many other programming languages such as JavaScript, Java, and C#.
previous requirements
To get started, the following prerequisites apply:
Clone the GitHub repository
First, let’s go clone the GitHub repository.
When the repository is successfully pulled, you can inspect the cdk directory which contains the following resources:
- cdk – Contains the main cdk resources
- app.py – Where the AWS CDK stack is defined
- cdk.json – Contains metadata and feature flags
AWS CDK Scripts
The two main files we want to see in the cdk
are subdirectory sagemaker_studio_construct.py
Y sagemaker_studio_stack.py
. Let’s look at each file in more detail.
Studio build file
The Studio build is defined in the sagemaker_studio_construct.py
proceedings.
The Studio build takes the virtual private cloud (VPC), enumerated users, AWS Region, and underlying default instance type as parameters. This AWS CDK construct accomplishes the following functions:
- Create the studio domain (
SageMakerStudioDomain
) - Sets the IAM role
sagemaker_studio_execution_role
withAmazonSageMakerFullAccess
Permissions required to create resources. Permissions should be further reduced to follow the principle of least privilege to improve security. - Sets the Jupyter server application settings: accept
JUPYTER_SERVER_APP_IMAGE_NAME
defining the jupyter-server-3 container image to use. - Set kernel gateway application configuration: take
KERNEL_GATEWAY_APP_IMAGE_NAME
defining the datascience-2.0 container image to be used. - Create a user profile for each user on the list
The following code snippet shows the relevant Studio domain AWS CloudFormation resources defined in the AWS CDK:
The following code snippet shows user profiles created from AWS CloudFormation resources:
study stack file
Once the construct is defined, you can add it by creating an instance of the class and passing the necessary arguments onto the stack. The stack creates the AWS CloudFormation resources as part of a consistent deployment. This means that if at least one cloud resource is not created, the CloudFormation stack rolls back the changes made. The following code snippet from the Studio build creates an instance inside the Studio stack:
Deploy the AWS CDK stack
To deploy your AWS CDK stack, run the following commands from the project root directory within your terminal window:
aws configure
pip3 install -r requirements.txt
cdk bootstrap --app "python3 -m cdk.app"
cdk deploy --app "python3 -m cdk.app"
Review the resources that the AWS CDK creates in your AWS account, and select yes when prompted to deploy the stack. Wait for your stack deployment to finish. This usually takes less than 5 minutes; however, adding more resources will lengthen the implementation time. You can also check the deployment status in the AWS CloudFormation console.
When the stack has been successfully deployed, verify your information by going to the Studio Control Panel. You should see the SageMaker Studio user profile you created.
If you redeploy the stack, it will check for changes and only make necessary cloud resource updates. For example, this can be used to add users or change the permissions of those users without having to recreate all the defined cloud resources.
Wipe
To remove a stack, complete the following steps:
- In the AWS CloudFormation console, choose batteries in the navigation pane.
- Open the stack you want to delete.
- In the stack details panel, choose Erase.
- Choose delete stack When prompted.
AWS CloudFormation will delete the resources created when the stack was deployed. This may take some time depending on the number of resources created.
If you encounter any issues while performing these cleanup steps, you may first need to manually remove the domain from Studio before repeating the steps in this section.
Conclution
In this post, we show how to use AWS cloud-native IaC resources to create an easily reusable template for Studio deployments. SageMaker Studio is a fully integrated web-based IDE that provides a visual interface for JupyterLab3-based ML development tasks. Using AWS CDK stacks, we were able to define constructs to create cloud components that can be easily modified, edited, or removed by making changes to the underlying CloudFormation stack.
For more information about Amazon Studio, see Amazon SageMaker Studio.
About the authors
cory peston He is a Software Engineer at the Amazon ML Solutions Lab. He is passionate about learning new technologies and leveraging that information to create reusable software solutions. He is an avid weight lifter and spends his spare time making digital art.
marcelo aberle is an ML engineer in the AWS AI organization. He leads the MLOps efforts at the Amazon ML Solutions Lab, helping clients design and implement scalable ML systems. His mission is to guide clients on their ML entrepreneurial journey and accelerate their ML path to production.
yash shah is a Scientific Manager in the Amazon ML Solutions Lab. He and his team of applied scientists and machine learning engineers work on a variety of machine learning use cases from healthcare, sports, automotive, and manufacturing.