

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

# Configurazione del ruolo IAM
<a name="spark-troubleshooting-agent-iam-setup"></a>

## Prerequisiti
<a name="spark-troubleshooting-agent-iam-prerequisites"></a>

Prima di iniziare, assicurati di disporre dei seguenti elementi:
+ Un AWS account con accesso amministrativo IAM
+ AWS CLI installata e configurata. Per ulteriori informazioni, consulta [Installazione della AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).

Imposta le seguenti variabili da utilizzare nei comandi successivi:

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

## Fase 1: creazione di un ruolo IAM per
<a name="spark-troubleshooting-agent-iam-step1"></a>

Il server SMUS MCP utilizza il tuo ruolo IAM per autorizzare le operazioni a livello di servizio. AWS Non sono richieste MCP-specific autorizzazioni separate.

**Per creare il ruolo IAM (AWS CLI)**

1. Crea un documento sulla politica di fiducia che consenta al tuo account di assumere il ruolo:

   ```
   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. Crea il ruolo:

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

## Passaggio 2: allega le autorizzazioni per la modalità di distribuzione
<a name="spark-troubleshooting-agent-iam-step2"></a>

Allega la politica di autorizzazione corrispondente alla tua piattaforma di distribuzione Spark. Puoi allegare uno o più dei seguenti elementi a seconda delle piattaforme che utilizzi.

### Opzione A: EMR su EC2
<a name="spark-troubleshooting-agent-iam-emr-ec2"></a>

1. Crea il documento programmatico:

   ```
   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. Crea e allega la politica:

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

In alternativa, puoi allegare la politica [AmazonElasticMapReduceFullAccess](https://docs.aws.amazon.com/aws-managed-policy/latest/reference/AmazonElasticMapReduceFullAccess.html) AWS gestita se il tuo ruolo la utilizza già:

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

### Opzione B: AWS Aderenza
<a name="spark-troubleshooting-agent-iam-glue"></a>

1. Crea il documento programmatico:

   ```
   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. Allega la politica:

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

### Opzione C: EMR Serverless
<a name="spark-troubleshooting-agent-iam-emr-serverless"></a>

1. Crea il documento programmatico:

   ```
   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. Allega la politica:

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

### Opzione D: EMR su EKS
<a name="spark-troubleshooting-agent-iam-emr-eks"></a>

1. Crea il documento programmatico:

   ```
   cat > emr-eks-policy.json << EOF
   {
       "Version": "2012-10-17",
       "Statement": [
           {
               "Action": [
                   "emr-containers:DescribeVirtualCluster",
                   "emr-containers:DescribeJobRun",
                   "emr-containers:ListJobRuns",
                   "emr-containers:ListVirtualClusters"
               ],
               "Resource": "*",
               "Effect": "Allow",
               "Sid": "EMREKSReadAccess"
           },
           {
               "Action": [
                   "logs:GetLogEvents",
                   "logs:DescribeLogGroups",
                   "logs:DescribeLogStreams"
               ],
               "Resource": "*",
               "Effect": "Allow",
               "Sid": "EMRCloudWatchLogAccess"
           },
           {
               "Action": [
                   "elasticmapreduce:CreatePersistentAppUI",
                   "elasticmapreduce:DescribePersistentAppUI",
                   "elasticmapreduce:GetPersistentAppUIPresignedURL"
               ],
               "Resource": "*",
               "Effect": "Allow",
               "Sid": "EMRPersistentApp"
           },
           {
               "Action": [
                   "s3:GetObject",
                   "s3:ListBucket"
               ],
               "Resource": "*",
               "Effect": "Allow",
               "Sid": "EMREKSS3LogAccess"
           }
       ]
   }
   EOF
   ```

1. Allega la politica:

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

### Facoltativo: autorizzazioni KMS per i log crittografati CloudWatch
<a name="spark-troubleshooting-agent-iam-kms"></a>

Se CloudWatch i registri sono crittografati con una chiave KMS gestita dal cliente, aggiungi quanto segue (`<KEY_ID>`sostituiscilo con l'ID della tua chiave KMS):

```
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>\"
    }]
  }"
```

## Fase 3: Configurare il client MCP
<a name="spark-troubleshooting-agent-iam-step3"></a>

Configura il tuo client MCP (ad esempio, Claude Desktop o Amazon Q Developer) per utilizzare il ruolo ARN che hai creato:

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

Consulta la documentazione del tuo client MCP per informazioni su come configurare AWS le credenziali (in genere tramite un AWS profilo che assume questo ruolo).

## Chiavi di condizione per le richieste del server MCP
<a name="spark-troubleshooting-agent-mcp-permissions-change"></a>

Due chiavi di condizione vengono aggiunte automaticamente a tutte le richieste effettuate tramite il server SMUS MCP:
+ `aws:ViaAWSMCPService`— Impostato su `true` per qualsiasi richiesta effettuata tramite un server AWS MCP gestito.
+ `aws:CalledViaAWSMCP`— Impostato sul principale del servizio del server MCP (ad esempio,`sagemaker-unified-studio-mcp.amazonaws.com`).

È possibile utilizzare questi tasti di condizione per controllare l'accesso alle risorse quando le richieste provengono da un server MCP AWS gestito.

**Esempio: consente le operazioni di lettura di Glue solo quando si accede tramite il server MCP SMUS:**

```
{
  "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"
        }
      }
    }
  ]
}
```

**Esempio: nega le operazioni di eliminazione quando si accede tramite qualsiasi server MCP AWS gestito:**

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

Per ulteriori informazioni sulle chiavi di condizione, consulta le chiavi di [contesto delle condizioni AWS globali nella Guida](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_condition-keys.html) per l'*utente IAM*.