

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

# 針對 Amazon ElastiCache 使用身分型政策 (IAM 政策)
<a name="IAM.IdentityBasedPolicies"></a>

這個主題提供以身分為基礎的政策範例，在該政策中帳戶管理員可以將許可政策連接至 IAM 身分 (即使用者、群組和角色)。

**重要**  
建議您先閱讀說明管理 Amazon ElastiCache 資源存取基本槪念與選項的主題。如需詳細資訊，請參閱[管理 ElastiCache 資源存取許可的概觀](IAM.Overview.md)。

本主題中的各節涵蓋下列內容：
+ [AWS Amazon ElastiCache 的 受管政策](IAM.IdentityBasedPolicies.PredefinedPolicies.md)
+ [客戶受管政策範例](#IAM.IdentityBasedPolicies.CustomerManagedPolicies)

以下顯示使用 Redis OSS 時的許可政策範例。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Sid": "AllowClusterPermissions",
            "Effect": "Allow",
            "Action": [
                "elasticache:CreateServerlessCache",
                "elasticache:CreateCacheCluster",
                "elasticache:DescribeServerlessCaches",
                "elasticache:DescribeReplicationGroups",
                "elasticache:DescribeCacheClusters",
                "elasticache:ModifyServerlessCache",
                "elasticache:ModifyReplicationGroup",
                "elasticache:ModifyCacheCluster"
            ],
            "Resource": "*"
        },
        {
            "Sid": "AllowUserToPassRole",
            "Effect": "Allow",
            "Action": [ "iam:PassRole" ],
            "Resource": "arn:aws:iam::123456789012:role/EC2-roles-for-cluster"
        }
    ]
}
```

------

以下顯示使用 Memcached 時的許可政策範例。

------
#### [ JSON ]

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [{
        "Sid": "AllowClusterPermissions",
        "Effect": "Allow",
        "Action": [
            "elasticache:CreateServerlessCache",
            "elasticache:CreateCacheCluster",
            "elasticache:DescribeServerlessCaches",
            "elasticache:DescribeCacheClusters",
            "elasticache:ModifyServerlessCache",
            "elasticache:ModifyCacheCluster"
        ],
        "Resource": "*"
    },
    {
        "Sid": "AllowUserToPassRole",
        "Effect": "Allow",
        "Action": [ "iam:PassRole" ],
        "Resource": "arn:aws:iam::123456789012:role/EC2-roles-for-cluster"
    }
    ]
}
```

------

此政策具有兩個陳述式：
+ 第一個陳述式授予 Amazon ElastiCache 動作 (`elasticache:Create*`、`elasticache:Describe*`、`elasticache:Modify*`) 的許可。
+ 第二個陳述式會對 `Resource` 值結尾指定的 IAM 角色名稱授予 IAM 動作 (`iam:PassRole`) 的許可。

此政策不指定 `Principal` 元素，因為您不會在以身分為基礎的政策中，指定取得許可的主體。當您將政策連接至使用者時，這名使用者即為隱含主體。當您將許可政策連接至 IAM 角色，該角色的信任政策中所識別的委託人即取得許可。

如需詳列所有 Amazon ElastiCache API 動作及適用資源的表格，請參閱「[ElastiCache API 許可：動作、資源和條件參考](IAM.APIReference.md)」。

## 客戶受管政策範例
<a name="IAM.IdentityBasedPolicies.CustomerManagedPolicies"></a>

如果您未使用預設政策並選擇使用自訂受管政策，請確保下列兩件事的其中一項。您應具有呼叫 `iam:createServiceLinkedRole` 的許可 (如需詳細資訊，請參閱[範例 4：允許使用者呼叫 IAM CreateServiceLinkedRole API](#create-service-linked-role-policy))。或者，您應已建立 ElastiCache 服務連結角色。

與使用 Amazon ElastiCache 主控台所需的最低許可相結合時，本節中的範例政策會授予額外的許可。這些範例也與AWS SDKs和 相關AWS CLI。

如需設定 IAM 使用者和群組的說明，請參閱 *IAM 使用者指南*中的[建立您的第一個 IAM 使用者和管理員群組](https://docs.aws.amazon.com/IAM/latest/UserGuide/getting-started_create-admin-group.html)。

**重要**  
在生產環境中使用 IAM 政策之前，請一律先徹底進行測試。使用 ElastiCache 主控台時，一些看似簡單的 ElastiCache 動作可能需要其他動作來支援。例如， 會`elasticache:CreateCacheCluster`授予建立 ElastiCache 叢集的許可。但是，若要執行此操作，ElastiCache 主控台需使用許多 `Describe` 和 `List` 動作來填入主控台清單。

**Topics**
+ [範例 1：允許使用者對 ElastiCache 資源進行唯讀存取](#example-allow-list-current-elasticache-resources)
+ [範例 2：允許使用者執行常見的 ElastiCache 系統管理員任務](#example-allow-specific-elasticache-actions)
+ [範例 3：允許使用者存取所有 ElastiCache API 動作](#allow-unrestricted-access)
+ [範例 4：允許使用者呼叫 IAM CreateServiceLinkedRole API](#create-service-linked-role-policy)
+ [範例 5：允許使用者使用 IAM 身分驗證連線到無伺服器快取](#iam-connect-policy)

### 範例 1：允許使用者對 ElastiCache 資源進行唯讀存取
<a name="example-allow-list-current-elasticache-resources"></a>

下列政策會授予 ElastiCache 動作的許可，以允許使用者列出資源。您通常會將此類型的許可政策連接到管理員群組。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[{
      "Sid": "ECReadOnly",
      "Effect":"Allow",
      "Action": [
          "elasticache:Describe*",
          "elasticache:List*"],
      "Resource":"*"
      }
   ]
}
```

------

### 範例 2：允許使用者執行常見的 ElastiCache 系統管理員任務
<a name="example-allow-specific-elasticache-actions"></a>

常見的系統管理員任務包括修改資源。系統管理員也可能想要取得 ElastiCache 事件的相關資訊。下列政策為使用者授予執行 ElastiCache 動作的許可，以進行這些一般系統管理員任務。您通常會將此類型的許可政策連接到系統管理員群組。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[{
      "Sid": "ECAllowMutations",
      "Effect":"Allow",
      "Action":[
          "elasticache:Modify*",
          "elasticache:Describe*",
          "elasticache:ResetCacheParameterGroup"
      ],
      "Resource":"*"
      }
   ]
}
```

------

### 範例 3：允許使用者存取所有 ElastiCache API 動作
<a name="allow-unrestricted-access"></a>

以下政策允許使用者存取所有 ElastiCache 動作。建議您只將此類型的許可政策授予管理員使用者。

------
#### [ JSON ]

****  

```
{
   "Version":"2012-10-17",		 	 	 
   "Statement":[{
      "Sid": "ECAllowAll",
      "Effect":"Allow",
      "Action":[
          "elasticache:*" 
      ],
      "Resource":"*"
      }
   ]
}
```

------

### 範例 4：允許使用者呼叫 IAM CreateServiceLinkedRole API
<a name="create-service-linked-role-policy"></a>

下列政策允許使用者呼叫 IAM `CreateServiceLinkedRole` API。建議您只將此類型的許可政策授予呼叫變動 ElastiCache 作業的使用者。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement":[
    {
      "Sid":"CreateSLRAllows",
      "Effect":"Allow",
      "Action":[
        "iam:CreateServiceLinkedRole"
      ],
      "Resource":"*",
      "Condition":{
        "StringLike":{
        "iam:AWSServiceName":"elasticache.amazonaws.com"
        }
      }
    }
  ]
}
```

------

### 範例 5：允許使用者使用 IAM 身分驗證連線到無伺服器快取
<a name="iam-connect-policy"></a>

下列政策允許任何使用者在 2023-04-01 到 2023-06-30 之間，使用 IAM 身分驗證連線到任何無伺服器快取。

------
#### [ JSON ]

****  

```
{
  "Version":"2012-10-17",		 	 	 
  "Statement" :
  [
    {
      "Effect" : "Allow",
      "Action" : ["elasticache:Connect"],
      "Resource" : [
        "arn:aws:elasticache:us-east-1:123456789012:serverlesscache:*"
      ],
      "Condition": {
        "DateGreaterThan": {"aws:CurrentTime": "2023-04-01T00:00:00Z"},
        "DateLessThan": {"aws:CurrentTime": "2023-06-30T23:59:59Z"}
      }
    },
    {
      "Effect" : "Allow",
      "Action" : ["elasticache:Connect"],
      "Resource" : [
        "arn:aws:elasticache:us-east-1:123456789012:user:*"
      ]
    }
  ]
}
```

------