모든 인스턴스가 동일한 IAM 역할을 사용하는 경우, 특정 API 호출을 수행한 SageMaker 노트북 인스턴스를 식별하려면 어떻게 해야 하나요?

4분 분량
0

여러 개의 Amazon SageMaker 노트북 인스턴스가 있습니다. 모두 동일한 AWS Identity and Access Management(IAM) 역할을 사용합니다. 각 API 작업에 대한 AWS CloudTrail 이벤트는 작업을 수행한 노트북 인스턴스와 관계없이 동일한 principalId(세션 이름)를 표시합니다. 어떤 노트북 인스턴스가 어떤 API 작업을 수행했는지 어떻게 알 수 있습니까?

간략한 설명

IAM 역할이 동일한 여러 SageMaker 인스턴스가 있는 경우, CloudTrail 이벤트로 특정 API 작업을 수행한 노트북 인스턴스를 확인할 수 없습니다.

예:

{
    "eventVersion": "1.05",
    "userIdentity": {
        "type": "AssumedRole",
        "principalId": "AAAAAAAAAAAAAAAAAA:SageMaker",
       
    "arn": "arn:aws:sts::111122223333:assumed-role/AmazonSageMaker-ExecutionRole/SageMaker",

해결 방법

1.    SageMaker 노트북 인스턴스에 대한 IAM 실행 역할을 생성합니다. 또는 기존 실행 역할을 사용합니다. 다음 단계에서 실행 역할의 Amazon 리소스 이름(ARN)은 arn:aws:iam::111122223333:role/service-role/AmazonSageMaker-ExecutionRole입니다.

2.    sts:AssumeRole을 포함하는 IAM 정책을 실행 역할에 연결합니다. sts:AssumeRole 작업을 통해 실행 역할은 다른 세션 이름을 사용하여 자체적으로 위임할 수 있습니다.

예:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": "sts:AssumeRole",
            "Resource": "arn:aws:iam::111122223333:role/service-role/AmazonSageMaker-ExecutionRole"
        }
    ]
}

3.    다음과 유사한 노트북 수명 주기 구성 시작 스크립트를 생성합니다. 이 예제 스크립트는 노트북 인스턴스 이름을 검색한 다음, 해당 이름을 세션 이름으로 사용합니다.

#Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.

#Permission is hereby granted, free of charge, to any person obtaining a copy of this
#software and associated documentation files (the "Software"), to deal in the Software
#without restriction, including without limitation the rights to use, copy, modify,
#merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so.

#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
#INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
#PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
#HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
#OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
#SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#!/bin/bash

set -ex

# Obtain the name of the notebook instance
nbname=$(jq -r '.ResourceName' /opt/ml/metadata/resource-metadata.json)
echo "Notebook Name = $nbname"

# Use the AWS Command Line Interface (AWS CLI) to obtain the Amazon Resource Name (ARN) of the IAM execution role
nbinfo=$(aws sagemaker describe-notebook-instance --notebook-instance-name $nbname)
nbrole=$(jq -r '.RoleArn' <<< "$nbinfo")
echo "Notebook Role = $nbrole"

# Obtain the Region of the notebook instance
nbregion=$(aws configure get region)
echo "Notebook Region = $nbregion"

# Write Assume Role Provider Settings to a new config file
echo "Writing new config file"
cat > /home/ec2-user/.aws/config.new <<EOF1
[default]
region=$nbregion
role_arn = $nbrole
credential_source = Ec2InstanceMetadata
role_session_name = $nbname
sts_regional_endpoints = regional
EOF1

echo "Moving new config to config file"
sudo mv /home/ec2-user/.aws/config.new /home/ec2-user/.aws/config

# Secure the "config" file so that it can't be deleted/updated without root user permissions
sudo chattr +i /home/ec2-user/.aws/config

4.    SageMaker 노트북 인스턴스를 생성하고(예: test-2) 이전 단계에서 생성한 수명 주기 구성 스크립트를 연결합니다.

5.    루트 액세스가 꺼진 상태에서 SageMaker 노트북 인스턴스를 생성합니다. 이렇게 하면 사용자 ec2-user가 구성 파일을 삭제하거나 업데이트하지 못하도록 제한합니다.

6.    API 작업을 수행한 노트북 인스턴스를 식별하려면 CloudTrail 이벤트를 확인합니다. userIdentity 객체에서 principalIdarn은 노트북 인스턴스 이름을 표시합니다. 예를 들어, 다음 이벤트 세부 정보는 test-2라는 SageMaker 노트북 인스턴스가 API를 호출했음을 표시합니다.

{
    "eventVersion": "1.05",
    "userIdentity": {
        "type": "AssumedRole",
        "principalId": "AAAAAAAAAAAAAAAAAAAA:test-2",
        "arn": "arn:aws:sts::111122223333:assumed-role/AmazonSageMaker-ExecutionRole/test-2",
        "accountId": "111122223333",
        "accessKeyId": "AAAAAAAAAAAAAAAAAAAA",
        "sessionContext": {
            "sessionIssuer": {
                "type": "Role",
                "principalId": "AAAAAAAAAAAAAAAAAAAA",
                "arn": "arn:aws:iam::111122223333:role/service-role/AmazonSageMaker-ExecutionRole",
                "accountId": "111122223333",
                "userName": "AmazonSageMaker-ExecutionRole"
            },
            "webIdFederationData": {},
            "attributes": {
                "mfaAuthenticated": "false",
                "creationDate": "2020-09-12T00:45:04Z"
            }
        },
        "invokedBy": "im.amazonaws.com"
    },
    "eventTime": "2020-09-12T00:49:04Z",
    "eventSource": "sagemaker.amazonaws.com",
    "eventName": "CreateEndpoint",
    "awsRegion": "us-east-1",
    "sourceIPAddress": "im.amazonaws.com",
    "userAgent": "im.amazonaws.com",
    "requestParameters": {
        "endpointName": "sagemaker-mxnet-ep",
        "endpointConfigName": "sagemaker-mxnet-epc",
        "tags": []
    },
    "responseElements": {
        "endpointArn": "arn:aws:sagemaker:us-east-1:111122223333:endpoint/sagemaker-mxnet-ep"
    },
    "requestID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "eventID": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",
    "eventType": "AwsApiCall",
    "recipientAccountId": "111122223333"
}

관련 정보

SageMaker 역할

AWS 공식
AWS 공식업데이트됨 2년 전
댓글 없음