

 **協助改進此頁面** 

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

若要為本使用者指南貢獻內容，請點選每個頁面右側面板中的**在 GitHub 上編輯此頁面**連結。

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# Amazon EKS 連接器 IAM 角色
<a name="connector-iam-role"></a>

您可以連接 Kubernetes 叢集，以在 中檢視它們 AWS 管理主控台。若要連接至 Kubernetes 叢集，請建立 IAM 角色。

## 檢查是否有現有的 EKS 連接器角色
<a name="check-connector-role"></a>

您可使用以下程序，檢查帳戶是否已有 Amazon EKS 連接器角色。

1. 前往 https://console.aws.amazon.com/iam/ 開啟 IAM 主控台。

1. 在左側導覽窗格中，選擇 **Roles** (角色)。

1. 搜尋 `AmazonEKSConnectorAgentRole` 的角色清單。如果包含 `AmazonEKSConnectorAgentRole` 的角色不存在，請參閱 [建立 Amazon EKS 連接器代理程式角色](#create-connector-role) 以建立角色。如果包含 `AmazonEKSConnectorAgentRole` 的角色存在，請選取角色以檢視連接的政策。

1. 選擇**許可**。

1. 確定 **AmazonEKSConnectorAgentPolicy** 受管政策已附加到該角色。如果已連接政策，則您的 Amazon EKS 連接器角色應已設定妥當。

1. 選擇 **Trust Relationships** (信任關係)，然後選擇 **Edit trust policy** (編輯信任政策)。

1. 確認信任關係包含下列政策。如果信任關係符合下列政策，請選擇 **Cancel** (取消)。如果信任關係不符合，請將政策複製到**編輯信任政策**視窗中，然後選擇**更新政策**。

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": {
                   "Service": [
                       "ssm.amazonaws.com"
                   ]
               },
               "Action": "sts:AssumeRole"
           }
       ]
   }
   ```

## 建立 Amazon EKS 連接器代理程式角色
<a name="create-connector-role"></a>

您可以使用 AWS 管理主控台 or AWS CloudFormation 來建立連接器代理程式角色。

 AWS CLI  

1. 建立名為 `eks-connector-agent-trust-policy.json` 的檔案，其中包含用於 IAM 角色的下列 JSON。

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Effect": "Allow",
               "Principal": {
                   "Service": [
                       "ssm.amazonaws.com"
                   ]
               },
               "Action": "sts:AssumeRole"
           }
       ]
   }
   ```

1. 建立名為 `eks-connector-agent-policy.json` 的檔案，其中包含用於 IAM 角色的下列 JSON。

   ```
   {
       "Version":"2012-10-17",		 	 	 
       "Statement": [
           {
               "Sid": "SsmControlChannel",
               "Effect": "Allow",
               "Action": [
                   "ssmmessages:CreateControlChannel"
               ],
               "Resource": "arn:aws:eks:*:*:cluster/*"
           },
           {
               "Sid": "ssmDataplaneOperations",
               "Effect": "Allow",
               "Action": [
                   "ssmmessages:CreateDataChannel",
                   "ssmmessages:OpenDataChannel",
                   "ssmmessages:OpenControlChannel"
               ],
               "Resource": "*"
           }
       ]
   }
   ```

1. 使用信任政策和您在先前清單項目中建立的政策，建立 Amazon EKS 連接器代理程式角色。

   ```
   aws iam create-role \
        --role-name AmazonEKSConnectorAgentRole \
        --assume-role-policy-document file://eks-connector-agent-trust-policy.json
   ```

1. 將政策連接到 Amazon EKS 連接器代理程式角色。

   ```
   aws iam put-role-policy \
        --role-name AmazonEKSConnectorAgentRole \
        --policy-name AmazonEKSConnectorAgentPolicy \
        --policy-document file://eks-connector-agent-policy.json
   ```

 AWS CloudFormation  

1. 將下列 AWS CloudFormation 範本儲存至本機系統上的文字檔案。
**注意**  
此範本也會建立服務連結角色，否則會在呼叫 `registerCluster` API 時建立。如需詳細資訊，請參閱 [使用角色將 Kubernetes 叢集連線至 Amazon EKS](using-service-linked-roles-eks-connector.md)。

   ```
   ---
   AWSTemplateFormatVersion: '2010-09-09'
   Description: 'Provisions necessary resources needed to register clusters in EKS'
   Parameters: {}
   Resources:
     EKSConnectorSLR:
       Type: AWS::IAM::ServiceLinkedRole
       Properties:
         AWSServiceName: eks-connector.amazonaws.com
   
     EKSConnectorAgentRole:
       Type: AWS::IAM::Role
       Properties:
         AssumeRolePolicyDocument:
           Version: '2012-10-17'
           Statement:
             - Effect: Allow
               Action: [ 'sts:AssumeRole' ]
               Principal:
                 Service: 'ssm.amazonaws.com'
   
     EKSConnectorAgentPolicy:
       Type: AWS::IAM::Policy
       Properties:
         PolicyName: EKSConnectorAgentPolicy
         Roles:
           - {Ref: 'EKSConnectorAgentRole'}
         PolicyDocument:
           Version: '2012-10-17'
           Statement:
             - Effect: 'Allow'
               Action: [ 'ssmmessages:CreateControlChannel' ]
               Resource:
               - Fn::Sub: 'arn:${AWS::Partition}:eks:*:*:cluster/*'
             - Effect: 'Allow'
               Action: [ 'ssmmessages:CreateDataChannel', 'ssmmessages:OpenDataChannel', 'ssmmessages:OpenControlChannel' ]
               Resource: "*"
   Outputs:
     EKSConnectorAgentRoleArn:
       Description: The agent role that EKS connector uses to communicate with AWS services.
       Value: !GetAtt EKSConnectorAgentRole.Arn
   ```

1. 開啟 [AWS CloudFormation 主控台](https://console.aws.amazon.com/cloudformation/)。

1. 選擇 **Create stack (建立堆疊) > 使用新資源 (標準)**。

1. 針對 **Specify template (指定範本)**，選擇 **Upload a template file (上傳範本檔案)**，然後選擇 **Choose file (選擇檔案)**。

1. 選擇您剛建立的檔案，然後選擇 **Next (下一步)**。

1. 針對 **Stack name (堆疊名稱)**，輸入您角色的名稱 (例如 `eksConnectorAgentRole`)，然後選擇 **Next (下一步)**。

1. 在 **Configure stack options** (設定堆疊選項) 頁面，選擇 **Next** (下一步)。

1. 在 **Review** (檢閱) 頁面上，檢閱您的資訊，確認該堆疊可能會建立 IAM 資源，並選擇 **Create stack** (建立堆疊)。