

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

# 搭配 Amazon OpenSearch Serverless 使用資料生命週期政策
<a name="serverless-lifecycle"></a>

Amazon OpenSearch Serverless 中的資料生命週期政策會定義 OpenSearch Serverless 在時間序列集合中保留資料的時間長度。例如，您可以設定政策，在 OpenSearch Serverless 刪除日誌資料之前將其保留 30 天。

您可以在 中的每個時間序列集合中為每個索引設定個別政策 AWS 帳戶。OpenSearch Serverless 會保留文件至少您在政策中指定的持續時間。然後，它會盡力自動刪除文件，通常是在保留期間的 48 小時或 10% 內，以較長者為準。

只有時間序列集合支援資料生命週期政策。搜尋和向量搜尋集合不會。

**Topics**
+ [資料生命週期政策](#serverless-lifecycle-policies)
+ [所需的許可](#serverless-lifecycle-permissions)
+ [政策優先順序](#serverless-lifecycle-precedence)
+ [政策語法](#serverless-lifecycle-syntax)
+ [建立資料生命週期政策](#serverless-lifecycle-create)
+ [更新資料生命週期政策](#serverless-lifecycle-update)
+ [刪除資料生命週期政策](#serverless-lifecycle-delete)

## 資料生命週期政策
<a name="serverless-lifecycle-policies"></a>

在資料生命週期政策中，您可以指定一系列規則。資料生命週期政策可讓您管理與符合這些規則的索引或集合相關聯的資料保留期。這些規則定義索引或索引群組中資料的保留期間。每個規則都包含資源類型 (`index`)、保留期間，以及保留期間適用的資源 （索引） 清單。

您可以使用下列其中一種格式定義保留期：
+ `"MinIndexRetention": "24h"` – OpenSearch Serverless 會保留指定期間內的索引資料，以小時或天為單位。您可以將此期間設定為從 `24h`到 `3650d`。
+ `"NoMinIndexRetention": true` – OpenSearch Serverless 無限期保留索引資料。

在下列範例政策中，第一個規則會為集合 中的所有索引指定 15 天的保留期間`marketing`。第二個規則指定`finance`集合`log`中以 開頭的所有索引名稱都未設定保留期間，且將無限期保留。

```
{
   "lifeCyclePolicyDetail": {
      "type": "retention",
      "name": "my-policy",
      "policyVersion": "MTY4ODI0NTM2OTk1N18x",
      "policy": {
         "Rules": [
            {
            "ResourceType":"index",
            "Resource":[
               "index/marketing/*"
            ],
            "MinIndexRetention": "15d"
         },
         {
            "ResourceType":"index",
            "Resource":[
               "index/finance/log*"
            ],
            "NoMinIndexRetention": true
         }
         ]
      },
      "createdDate": 1688245369957,
      "lastModifiedDate": 1688245369957
   }
}
```

在下列範例政策規則中，OpenSearch Serverless 會無限期保留帳戶內所有集合之所有索引中的資料。

```
{
   "Rules": [
      {
         "ResourceType": "index",
         "Resource": [
            "index/*/*"
         ]
      }
   ],
   "NoMinIndexRetention": true
}
```

## 所需的許可
<a name="serverless-lifecycle-permissions"></a>

OpenSearch Serverless 的生命週期政策使用以下 AWS Identity and Access Management (IAM) 許可。您可以指定 IAM 條件，將使用者限制為與特定集合和索引相關聯的資料生命週期政策。
+ `aoss:CreateLifecyclePolicy` – 建立資料生命週期政策。
+ `aoss:ListLifecyclePolicies` – 列出目前帳戶中的所有資料生命週期政策。
+ `aoss:BatchGetLifecyclePolicy` – 檢視與帳戶或政策名稱相關聯的資料生命週期政策。
+ `aoss:BatchGetEffectiveLifecyclePolicy` – 檢視指定資源的資料生命週期政策 (`index` 是唯一支援的資源）。
+ `aoss:UpdateLifecyclePolicy` – 修改指定的資料生命週期政策，並變更其保留設定或資源。
+ `aoss:DeleteLifecyclePolicy` – 刪除資料生命週期政策。

下列身分型存取政策可讓使用者檢視所有資料生命週期政策，並使用資源模式 更新政策`index/application-logs`：

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

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "aoss:UpdateLifecyclePolicy"
            ],
            "Resource": "*",
            "Condition": {
                "StringEquals": {
                    "aoss:collection": "application-logs"
                }
            }
        },
        {
            "Effect": "Allow",
            "Action": [
                "aoss:ListLifecyclePolicies",
                "aoss:BatchGetLifecyclePolicy"
            ],
            "Resource": "*"
        }
    ]
}
```

------

## 政策優先順序
<a name="serverless-lifecycle-precedence"></a>

在某些情況下，資料生命週期政策規則可能會在政策內或跨政策之間重疊。發生這種情況時，具有更特定資源名稱或索引模式的規則會使用更一般的資源名稱或模式覆寫*兩個*規則通用的任何索引的規則。

例如，在下列政策中，兩個規則適用於索引 `index/sales/logstash`。在這種情況下，第二個規則優先，因為 `index/sales/log*` 是 最長的相符項目`index/sales/logstash`。因此，OpenSearch Serverless 不會設定索引的保留期間。

```
{
      "Rules":[
         {
            "ResourceType":"index",
            "Resource":[
               "index/sales/*",
            ],
            "MinIndexRetention": "15d"
         },
         {
            "ResourceType":"index",
            "Resource":[
               "index/sales/log*",
            ],
            "NoMinIndexRetention": true
         }
      ]
   }
```

## 政策語法
<a name="serverless-lifecycle-syntax"></a>

提供一個或多個*規則*。這些規則會為您的 OpenSearch Serverless 索引定義資料生命週期設定。

每個規則都包含下列元素。您可以在每個規則`NoMinIndexRetention`中提供 `MinIndexRetention`或 ，但不能同時提供兩者。


| Element | Description | 
| --- | --- | 
| Resource Type (資源類型) | 套用規則的資源類型。資料生命週期政策唯一支援的選項是 index。 | 
| Resource | 資源名稱和/或模式的清單。模式由字首和萬用字元 (\$1) 組成，允許相關聯的許可套用至多個資源。例如 index/<collection-name\$1pattern>/<index-name\$1pattern>。 | 
| MinIndexRetention | 將文件保留在索引中的最短期間，以天 (d) 或小時 (h) 為單位。下限為 24h，上限為 3650d。 | 
| NoMinIndexRetention | 如果為 true，OpenSearch Serverless 會無限期保留文件。 | 

在下列範例中，第一個規則會套用至`autoparts-inventory`模式 (`index/autoparts-inventory/*`) 下的所有索引，並要求資料保留至少 20 天，才能進行任何動作，例如刪除或封存。

第二個規則以符合`auto*/gear`模式 (`index/auto*/gear`) 的索引為目標，將最短保留期間設定為 24 小時。

第三個規則特別適用於`tires`索引，沒有最短保留期間，這表示可以立即刪除或封存此索引中的資料，或根據其他條件。這些規則有助於管理具有不同保留時間或沒有保留限制的索引資料的保留。

```
{
  "Rules": [
    {
      "ResourceType": "index",
      "Resource": [
        "index/autoparts-inventory/*"
      ],
      "MinIndexRetention": "20d"
    },
    {
      "ResourceType": "index",
      "Resource": [
        "index/auto*/gear"
      ],
      "MinIndexRetention": "24h"
    },
    {
      "ResourceType": "index",
      "Resource": [
        "index/autoparts-inventory/tires"
      ],
      "NoMinIndexRetention": true
    }
  ]
}
```

## 建立資料生命週期政策
<a name="serverless-lifecycle-create"></a>

若要建立資料生命週期政策，您可以定義根據指定條件管理資料保留和刪除的規則。

### 主控台
<a name="serverless-lifecycle-create-console"></a>

**建立資料生命週期政策**

1. 登入 Amazon OpenSearch Service 主控台，網址為 https：//[https://console.aws.amazon.com/aos/home](https://console.aws.amazon.com/aos/home)。

1. 在左側導覽窗格中，選擇**資料生命週期政策**。

1. 選擇**建立資料生命週期政策**。

1. 輸入政策的描述性名稱。

1. 針對**資料生命週期**，選擇**新增**並選取政策的集合和索引。

   首先選擇索引所屬的集合。然後，從清單中選擇索引或輸入索引模式。若要選取所有集合做為來源，請輸入星號 (`*`)。

1. 對於**資料保留**，您可以選擇無限期保留資料，或取消選取**無限制 （絕不刪除）**，並指定一段時間後 OpenSearch Serverless 會自動從 Amazon S3 刪除資料。

1. 選擇**儲存**，然後選擇**建立**。

### AWS CLI
<a name="serverless-lifecycle-create-cli"></a>

若要使用 建立資料生命週期政策 AWS CLI，請使用 [create-lifecycle-policy](https://docs.aws.amazon.com/cli/latest/reference/opensearchserverless/create-lifecycle-policy.html) 命令搭配下列選項：
+ `--name` – 政策的名稱。
+ `--type` – 政策的類型。目前，唯一可用的值是 `retention`。
+ `--policy` – 資料生命週期政策。此參數同時接受內嵌政策和 .json 檔案。您必須將內嵌政策編碼為 JSON 逸出字串。若要在 檔案中提供政策，請使用 格式 `--policy file://my-policy.json`。

**Example**  

```
aws opensearchserverless create-lifecycle-policy \
  --name my-policy \
  --type retention \
  --policy "{\"Rules\":[{\"ResourceType\":\"index\",\"Resource\":[\"index/autoparts-inventory/*\"],\"MinIndexRetention\": \"81d\"},{\"ResourceType\":\"index\",\"Resource\":[\"index/sales/orders*\"],\"NoMinIndexRetention\":true}]}"
```

## 更新資料生命週期政策
<a name="serverless-lifecycle-update"></a>

若要更新資料生命週期政策，您可以修改現有的規則，以反映資料保留或刪除需求的變更。這可讓您隨著資料管理需求的演進調整政策。

從更新政策到 OpenSearch Serverless 開始強制執行新的保留期間，可能會有幾分鐘的延遲時間。

### 主控台
<a name="serverless-lifecycle-update-console"></a>

**更新資料生命週期政策**

1. 登入 Amazon OpenSearch Service 主控台，網址為 https：//[https://console.aws.amazon.com/aos/home](https://console.aws.amazon.com/aos/home)。

1. 在左側導覽窗格中，選擇**資料生命週期政策**。

1. 選取您要更新的資料生命週期政策，然後選擇**編輯**。

1. 使用視覺化編輯器或 JSON 編輯器修改政策。

1. 選擇**儲存**。

### AWS CLI
<a name="serverless-lifecycle-update-cli"></a>

若要使用 更新資料生命週期政策 AWS CLI，請使用 [update-lifecycle-policy](https://docs.aws.amazon.com/cli/latest/reference/opensearchserverless/update-lifecycle-policy.html) 命令。

您必須在請求中包含 `--policy-version` 參數。您可以使用 [list-lifecycle-policies](https://docs.aws.amazon.com/cli/latest/reference/opensearchserverless/list-lifecycle-policies.html) 或 [batch-get-lifecycle-policy](https://docs.aws.amazon.com/cli/latest/reference/opensearchserverless/batch-get-lifecycle-policy.html) 命令來擷取政策版本。我們建議您包含最新的政策版本，以防止意外覆寫其他人所做的變更。

下列請求會使用新的政策 JSON 文件更新資料生命週期政策。

**Example**  

```
aws opensearchserverless update-lifecycle-policy \
  --name my-policy \
  --type retention \
  --policy-version MTY2MzY5MTY1MDA3Ml8x \
  --policy file://my-new-policy.json
```

## 刪除資料生命週期政策
<a name="serverless-lifecycle-delete"></a>

當您刪除資料生命週期政策時，OpenSearch Serverless 不會再在任何相符的索引上強制執行它。

### 主控台
<a name="serverless-lifecycle-delete-console"></a>

**刪除資料生命週期政策**

1. 登入 Amazon OpenSearch Service 主控台，網址為 https：//[https://console.aws.amazon.com/aos/home](https://console.aws.amazon.com/aos/home)。

1. 在左側導覽窗格中，選擇**資料生命週期政策**。

1. 選取您要刪除的政策，然後選擇**刪除**並確認刪除。

### AWS CLI
<a name="serverless-lifecycle-delete-cli"></a>

若要使用 刪除資料生命週期政策 AWS CLI，請使用 [delete-lifecycle-policy](https://docs.aws.amazon.com/cli/latest/reference/opensearchserverless/delete-lifecycle-policy.html) 命令。

**Example**  

```
aws opensearchserverless delete-lifecycle-policy \
  --name my-policy \
  --type retention
```