Shutting Down Amazon SageMaker Studio Apps on a Scheduled Basis With Amazon EventBridge, AWS Lambda, and Boto3
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.
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.
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.
We will first create a Lambda function to list the Studio apps in your account and delete them.
Then we will set up an EventBridge rule to run the Lambda function every day at 18:00 UTC.
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:
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:
Scheduling the Lambda function with EventBridge
Here we create an EventBridge rule to run the Lambda function on a schedule. You can define the schedule using cron expressions. see Schedule Expressions for Rules for more details.
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
You can deploy the stack into your account via the console or by executing the following commands in your environment.
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 \
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: