Shutting Down Amazon SageMaker Studio Apps on a Scheduled Basis With Amazon EventBridge, AWS Lambda, and Boto3

Sofian Hamiti
Jan 25 · 4 min read

Amazon SageMaker Studio is a fully integrated IDE unifying the tools needed for ML development. With Studio you can write code, track experiments, visualize data, and perform debugging and monitoring in a Jupyterlab-based interface. SageMaker manages the creation of the underlying instances and resources so you can get started quickly in your ML project.

Image for post
Image for post

When creating or launching a Notebook, an Interactive Shell, or a Terminal based on a SageMaker Image, the resources run as Apps on Amazon EC2 instances for which you incur cost, and you must shut them down to stop the metering.

Currently, although you can install the Sagemaker-Studio-Autoshutdown-Extension in Studio, the installation needs to be done for every user profile.

Image for post
Image for post

In this post I show how you can shut down all Studio Apps on your account on a scheduled basis with AWS Lambda and Amazon EventBridge. This can help you ensure no Studio app is left running all night by your ML teams.

Image for post
Image for post
Photo by Icons8 Team on Unsplash

Visiting the Lambda tutorials, and How To Get Started With Amazon EventBridge could be a good start if those things sound new to you.

Walkthrough overview

Then we will set up an EventBridge rule to run the Lambda function every day at 18:00 UTC.

Image for post
Image for post
Architecture overview

You can find the code in my Github repository.

Using Lambda and Boto3 to delete Studio apps

For your convenience, I have prepared the following example script. It uses Boto3 to stop and delete the Studio apps in your account:

With line 11 and line 26 we create an iterator returning paginated lists of Studio apps. We use paginators here as there can be many Apps in your account.

From line 28, we iterate through the lists, and use the delete_app method to stop and delete the ones that are running.

Note that we do not delete the apps named default as they are used to access the Studio Jupyterlab interface and do not incur compute costs. You can leave them as Ready.

You can create the Lambda function with the script in your account, test it, and verify its effect in SageMaker Studio as shown below:

Image for post
Image for post
Image for post
Image for post
An example Studio user summary before and after triggering the Lambda function in my account

Scheduling the Lambda function with EventBridge

In this example I set the function to run at 18:00 UTC with the following expression: cron(0 18 * * ? *). You can adjust this based on your time zone and the schedule of your choice.

Below is a CloudFormation template to deploy the Lambda Function, the EventBridge rule, and a few permissions into your account:

Deploying the stack into your account

git clone https://github.com/SofianHamiti/aws-lambda-shutdown-studio-kernel.gitcd aws-lambda-shutdown-studio-kernelaws cloudformation create-stack \
--stack-name studio-lambda-shutdown \
--template-body file://template.yaml \
--capabilities CAPABILITY_NAMED_IAM

Make sure you have the AWS CLI installed and configured in your environment if you use the commands.

When your stack is created, you can see your Lambda function in the console:

Image for post
Image for post

The Startup

Medium's largest active publication, followed by +771K people. Follow to join our community.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store