

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

# IAM ロールのセットアップ
<a name="spark-troubleshooting-agent-iam-setup"></a>

## 前提条件
<a name="spark-troubleshooting-agent-iam-prerequisites"></a>

開始する前に、以下があることを確認してください。
+ IAM 管理アクセス権を持つ AWS アカウント
+ AWS CLI がインストールされ、設定されています。詳細については、「 [AWS CLI のインストール](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html)」を参照してください。

後続のコマンドで使用するには、次の変数を設定します。

```
ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)
REGION=$(aws configure get region)
```

## ステップ 1: IAM ロールを作成する
<a name="spark-troubleshooting-agent-iam-step1"></a>

SMUS MCP サーバーは、IAM ロールを使用して AWS サービスレベルでオペレーションを承認します。個別の MCP 固有のアクセス許可は必要ありません。

**IAM ロールを作成するには (AWS CLI)**

1. アカウントがロールを引き受けることを許可する信頼ポリシードキュメントを作成します。

   ```
   cat > mcp-trust-policy.json << EOF
   {
     "Version": "2012-10-17",
     "Statement": [
       {
         "Sid": "AllowAccountToAssumeRole",
         "Effect": "Allow",
         "Principal": { "AWS": "arn:aws:iam::${ACCOUNT_ID}:root" },
         "Action": "sts:AssumeRole"
       }
     ]
   }
   EOF
   ```

1. ロールを作成します。

   ```
   aws iam create-role \
     --role-name SparkTroubleshootingMCPRole \
     --assume-role-policy-document file://mcp-trust-policy.json
   ```

## ステップ 2: デプロイモードのアクセス許可をアタッチする
<a name="spark-troubleshooting-agent-iam-step2"></a>

Spark デプロイプラットフォームに一致するアクセス許可ポリシーをアタッチします。使用するプラットフォームに応じて、以下を 1 つ以上アタッチできます。

### オプション A: EC2 での EMR
<a name="spark-troubleshooting-agent-iam-emr-ec2"></a>

1. ポリシードキュメントを作成します。

   ```
   cat > emr-ec2-policy.json << 'EOF'
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
         "Sid": "EMREC2ReadAccess",
         "Effect": "Allow",
         "Action": [
           "elasticmapreduce:DescribeCluster",
           "elasticmapreduce:DescribeStep",
           "elasticmapreduce:ListSteps",
           "elasticmapreduce:ListClusters",
           "elasticmapreduce:DescribeJobFlows"
         ],
         "Resource": ["*"]
       },
       {
         "Sid": "EMRS3LogAccess",
         "Effect": "Allow",
         "Action": ["s3:GetObject", "s3:ListBucket"],
         "Resource": "*"
       },
       {
         "Sid": "EMRPersistentApp",
         "Effect": "Allow",
         "Action": [
           "elasticmapreduce:CreatePersistentAppUI",
           "elasticmapreduce:DescribePersistentAppUI",
           "elasticmapreduce:GetPersistentAppUIPresignedURL"
         ],
         "Resource": ["*"]
       }
     ]
   }
   EOF
   ```

1. ポリシーを作成してアタッチします。

   ```
   aws iam put-role-policy \
     --role-name SparkTroubleshootingMCPRole \
     --policy-name EMREC2TroubleshootingAccess \
     --policy-document file://emr-ec2-policy.json
   ```

または、ロールが既に使用している [AmazonElasticMapReduceFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonElasticMapReduceFullAccess.html) AWS 管理ポリシーをアタッチすることもできます。

```
aws iam attach-role-policy \
  --role-name SparkTroubleshootingMCPRole \
  --policy-arn arn:aws:iam::aws:policy/AmazonElasticMapReduceFullAccess
```

### オプション B: AWS Glue
<a name="spark-troubleshooting-agent-iam-glue"></a>

1. ポリシードキュメントを作成します。

   ```
   cat > glue-policy.json << EOF
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
         "Sid": "GlueReadAccess",
         "Effect": "Allow",
         "Action": [
           "glue:GetJob",
           "glue:GetJobRun",
           "glue:GetJobRuns",
           "glue:GetJobs",
           "glue:BatchGetJobs"
         ],
         "Resource": ["arn:aws:glue:*:${ACCOUNT_ID}:job/*"]
       },
       {
         "Sid": "GlueCloudWatchLogsAccess",
         "Effect": "Allow",
         "Action": ["logs:GetLogEvents", "logs:FilterLogEvents"],
         "Resource": ["arn:aws:logs:*:${ACCOUNT_ID}:log-group:/aws/glue/*"]
       },
       {
         "Sid": "GlueSparkWebUI",
         "Effect": "Allow",
         "Action": [
           "glue:RequestLogParsing",
           "glue:GetLogParsingStatus",
           "glue:GetEnvironment",
           "glue:GetStage",
           "glue:GetStages",
           "glue:GetStageFiles",
           "glue:BatchGetStageFiles",
           "glue:GetStageAttempt",
           "glue:GetStageAttemptTaskList",
           "glue:GetStageAttemptTaskSummary",
           "glue:GetExecutors",
           "glue:GetExecutorsThreads",
           "glue:GetStorage",
           "glue:GetStorageUnit",
           "glue:GetQueries",
           "glue:GetQuery",
           "glue:GetDashboardUrl"
         ],
         "Resource": ["arn:aws:glue:*:${ACCOUNT_ID}:job/*"]
       },
       {
         "Sid": "GluePassRoleAccess",
         "Effect": "Allow",
         "Action": "iam:PassRole",
         "Resource": "*",
         "Condition": {
           "StringLike": {
             "iam:PassedToService": "glue.amazonaws.com"
           }
         }
       }
     ]
   }
   EOF
   ```

1. ポリシーをアタッチします。

   ```
   aws iam put-role-policy \
     --role-name SparkTroubleshootingMCPRole \
     --policy-name GlueTroubleshootingAccess \
     --policy-document file://glue-policy.json
   ```

### オプション C: EMR Serverless
<a name="spark-troubleshooting-agent-iam-emr-serverless"></a>

1. ポリシードキュメントを作成します。

   ```
   cat > emr-serverless-policy.json << EOF
   {
     "Version": "2012-10-17",		 	 	 
     "Statement": [
       {
         "Sid": "EMRServerlessReadAccess",
         "Effect": "Allow",
         "Action": [
           "emr-serverless:GetJobRun",
           "emr-serverless:GetApplication",
           "emr-serverless:ListApplications",
           "emr-serverless:ListJobRuns",
           "emr-serverless:ListJobRunAttempts",
           "emr-serverless:GetDashboardForJobRun",
           "emr-serverless:ListTagsForResource"
         ],
         "Resource": ["*"]
       },
       {
         "Sid": "EMRServerlessCloudWatchLogsAccess",
         "Effect": "Allow",
         "Action": ["logs:GetLogEvents", "logs:FilterLogEvents"],
         "Resource": ["arn:aws:logs:*:${ACCOUNT_ID}:log-group:/aws/emr-serverless/*"]
       },
       {
         "Sid": "EMRServerlessS3LogsAccess",
         "Effect": "Allow",
         "Action": ["s3:GetObject", "s3:ListBucket"],
         "Resource": "*"
       }
     ]
   }
   EOF
   ```

1. ポリシーをアタッチします。

   ```
   aws iam put-role-policy \
     --role-name SparkTroubleshootingMCPRole \
     --policy-name EMRServerlessTroubleshootingAccess \
     --policy-document file://emr-serverless-policy.json
   ```

### オプション: 暗号化された CloudWatch Logs の KMS アクセス許可
<a name="spark-troubleshooting-agent-iam-kms"></a>

CloudWatch Logs がカスタマーマネージド KMS キーで暗号化されている場合は、以下を追加します (KMS キー ID `<KEY_ID>`に置き換えます）。

```
aws iam put-role-policy \
  --role-name SparkTroubleshootingMCPRole \
  --policy-name KMSCloudWatchLogsDecrypt \
  --policy-document "{
    \"Version\": \"2012-10-17\",
    \"Statement\": [{
      \"Effect\": \"Allow\",
      \"Action\": [\"kms:Decrypt\", \"kms:DescribeKey\"],
      \"Resource\": \"arn:aws:kms:${REGION}:${ACCOUNT_ID}:key/<KEY_ID>\"
    }]
  }"
```

## ステップ 3: MCP クライアントを設定する
<a name="spark-troubleshooting-agent-iam-step3"></a>

作成したロール ARN を使用するように MCP クライアント (Claude Desktop や Amazon Q Developer など) を設定します。

```
echo "arn:aws:iam::${ACCOUNT_ID}:role/SparkTroubleshootingMCPRole"
```

 AWS 認証情報の設定方法については、MCP クライアントのドキュメントを参照してください (通常は、このロールを引き受ける AWS プロファイル経由）。

## MCP サーバーリクエストの条件キー
<a name="spark-troubleshooting-agent-mcp-permissions-change"></a>

SMUS MCP サーバーを介して行われたすべてのリクエストに 2 つの条件キーが自動的に追加されます。
+ `aws:ViaAWSMCPService` – AWS マネージド MCP サーバーを介して行われたリクエスト`true`の場合は、 に設定します。
+ `aws:CalledViaAWSMCP` – MCP サーバーサービスプリンシパル ( など`sagemaker-unified-studio-mcp.amazonaws.com`) に設定します。

これらの条件キーを使用して、リクエストが AWS マネージド MCP サーバーから送信されたときにリソースへのアクセスを制御できます。

**例: SMUS MCP サーバー経由でアクセスした場合にのみ Glue 読み取りオペレーションを許可します。**

```
{
  "Version": "2012-10-17",		 	 	 
  "Statement": [
    {
      "Sid": "AllowGlueReadViaSMUSMCP",
      "Effect": "Allow",
      "Action": ["glue:GetJob", "glue:GetJobRun", "glue:GetJobRuns"],
      "Resource": "*",
      "Condition": {
        "StringEquals": {
          "aws:CalledViaAWSMCP": "sagemaker-unified-studio-mcp.amazonaws.com"
        }
      }
    }
  ]
}
```

**例: マネージド MCP サーバー経由でアクセスした場合、 AWS 削除オペレーションを拒否します。**

```
{
  "Effect": "Deny",
  "Action": ["s3:DeleteObject", "s3:DeleteBucket"],
  "Resource": "*",
  "Condition": {
    "Bool": {
      "aws:ViaAWSMCPService": "true"
    }
  }
}
```

条件キーの詳細については、*「IAM ユーザーガイド*」の[AWS 「グローバル条件コンテキストキー](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html)」を参照してください。