-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathSagemaker-stop-instances.yaml
112 lines (92 loc) · 3.15 KB
/
Sagemaker-stop-instances.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
AWSTemplateFormatVersion: '2010-09-09'
Description: Cloudformation template to discover all the 'InService' Sagemaker notebooks instances and stop them when idle.
Parameters:
ScheduledTime:
Type: String
Description: The specific time in cron syntax when the Amazon Eventbridge rule should trigger(e.g.("cron 0 8 * *? *")for 8:00 AM UTC)
LambdaRoleName:
Type: String
Description: Name of the IAM role for the lambda function.
Resources:
LambdaRole:
Type: AWS::IAM::Role
Properties:
RoleName: !Ref LambdaRoleName
AssumeRolePolicyDocument:
Version : '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service: lambda.amazonaws.com
Action: sts:AssumeRole
Policies:
- PolicyName: SageMakerNotebookPolicy
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
- sagemaker:StopNotebookInstance
- sagemaker:ListNotebookInstances
Resource: !Join
- ''
- - 'arn:'
- !Ref AWS::Partition
- ':sagemaker:'
- !Ref AWS::Region
- ':'
- !Ref AWS::AccountId
- ':notebook-instance/*'
LambdaFunction:
Type : AWS::Lambda::Function
Properties:
FunctionName: sagemaker_stop_instance_v2
Handler: index.handler
Role: !GetAtt LambdaRole.Arn
Timeout: 120
Runtime: python3.9
Code:
ZipFile: |
import json
import boto3
sagemaker = boto3.client("sagemaker")
def get_instances_with_status(status):
response = sagemaker.list_notebook_instances(StatusEquals=status)
instances = response.get('NotebookInstances',[])
return instances
def stop_notebook_instance(instance_name):
try:
response = sagemaker.stop_notebook_instance(NotebookInstanceName=instance_name)
print(f"The notebook instance{instance_name} is stopping")
except Exception as e:
print(f"Error stopping notebook instance{instance_name}:{str(e)}")
def handler(event, context):
status_to_check = ['Pending','InService']
for status in status_to_check:
instances = get_instances_with_status(status)
instance_name =[]
for instance in instances:
instance_name.append(instance["NotebookInstanceName"])
if not instance_name:
return {
'statusCode': 200,
'body': json.dumps('No Sagemaker instances are in running mode that needs to be stopped')}
else:
for instance_name in instance_name:
stop_notebook_instance(instance_name)
EventRule:
Type: AWS::Events::Rule
Properties:
Description: Eventbridge rule to trigger lambda at a specific time
ScheduleExpression: !Ref ScheduledTime
State: ENABLED
Targets:
- Arn: !GetAtt LambdaFunction.Arn
Id: TargetFunction
Outputs:
LambdaFunctionArn:
Description: ARN of the created Lambda Function
Value: !GetAtt LambdaFunction.Arn
LambdRoleArn:
Description: ARN of the IAM role created for the lambda function
Value : !GetAtt LambdaRole.Arn