すべてのインスタンスが同じ IAM ロールを使用する場合、どの SageMaker ノートブックインスタンスが特定の API コールを行ったかを判断するにはどうすればよいですか?

所要時間3分
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年前
コメントはありません

関連するコンテンツ