Skip to content

Commit 283194b

Browse files
Additional SSM tags for SSMManager
1 parent 4f3570e commit 283194b

File tree

1 file changed

+31
-2
lines changed

1 file changed

+31
-2
lines changed

sagemaker_ssh_helper/sm-init-ssm

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,44 @@ cp /etc/amazon/ssm/seelog.xml.template /etc/amazon/ssm/seelog.xml
3434

3535
CURRENT_REGION=$(aws configure get region || echo "$AWS_REGION")
3636

37+
_install_jq
38+
39+
SSH_CREATOR=$(aws sts get-caller-identity | jq --raw-output '.UserId')
40+
SSH_TIMESTAMP=$(date +%s)
41+
42+
if [ -f /opt/ml/metadata/resource-metadata.json ]; then
43+
# SageMaker Studio and notebook instances
44+
RESOURCE_NAME=$(jq --raw-output '.ResourceName' < /opt/ml/metadata/resource-metadata.json)
45+
RESOURCE_ARN=$(jq --raw-output '.ResourceArn' < /opt/ml/metadata/resource-metadata.json)
46+
elif [ -f /opt/ml/config/processingjobconfig.json ]; then
47+
# Processing job
48+
RESOURCE_NAME=$(jq --raw-output '.ProcessingJobName' < /opt/ml/config/processingjobconfig.json)
49+
RESOURCE_ARN=$(jq --raw-output '.ProcessingJobArn' < /opt/ml/config/processingjobconfig.json)
50+
elif [[ "$TRAINING_JOB_NAME" != "" ]]; then
51+
# Training job
52+
RESOURCE_NAME=$TRAINING_JOB_NAME
53+
RESOURCE_ARN=$TRAINING_JOB_ARN # empty for local mode
54+
elif [[ "$TRANSFORM_JOB_ARN" != "" ]]; then
55+
# Transform job
56+
RESOURCE_NAME=$(echo $TRANSFORM_JOB_ARN | awk -F/ '{print $2}')
57+
RESOURCE_ARN=$TRANSFORM_JOB_ARN
58+
else
59+
# Probably, endpoint
60+
RESOURCE_NAME=""
61+
RESOURCE_ARN=""
62+
fi
63+
64+
echo "sm-init-ssm: Detected SageMaker resource: $RESOURCE_NAME [$RESOURCE_ARN]"
65+
66+
SSH_SSM_TAGS="[{\"Key\": \"SSHOwner\", \"Value\": \"$SSH_OWNER_TAG\"}, {\"Key\": \"SSHCreator\", \"Value\": \"$SSH_CREATOR\"}, {\"Key\": \"SSHTimestamp\", \"Value\": \"$SSH_TIMESTAMP\"}, {\"Key\": \"SSHResourceName\", \"Value\": \"$RESOURCE_NAME\"}, {\"Key\": \"SSHResourceArn\", \"Value\": \"$RESOURCE_ARN\"}]"
67+
3768
response=$(aws ssm create-activation \
3869
--description "Activation for Amazon SageMaker integration with SSH and IDEs" \
3970
--iam-role "$SSH_SSM_ROLE" \
4071
--registration-limit 1 \
4172
--region "$CURRENT_REGION" \
4273
--tags "$SSH_SSM_TAGS")
4374

44-
_install_jq
45-
4675
acode=$(echo $response | jq --raw-output '.ActivationCode')
4776
aid=$(echo $response | jq --raw-output '.ActivationId')
4877

0 commit comments

Comments
 (0)