

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

# Amazon ECR 中的生命週期政策範例
<a name="lifecycle_policy_examples"></a>

以下是顯示語法的生命週期政策範例。

若要查看政策屬性的詳細資訊，請參閱 [Amazon ECR 中的生命週期政策屬性](lifecycle_policy_parameters.md)。如需使用 建立生命週期政策的說明 AWS CLI，請參閱 [建立生命週期政策 (AWS CLI)](lp_creation.md#lp-creation-cli)。

## 生命週期政策範本
<a name="lifecycle_policy_syntax"></a>

在與儲存庫建立關聯之前，會評估生命週期政策的內容。以下是生命週期政策的 JSON 語法範本。

```
{
        "rules": [
            {
                "rulePriority": integer,
                "description": "string",
                "selection": {
                    "tagStatus": "tagged"|"untagged"|"any",
                    "tagPatternList": list<string>,
                    "tagPrefixList": list<string>,
                    "storageClass": "standard"|"archive",
                    "countType": "imageCountMoreThan"|"sinceImagePushed"|"sinceImagePulled"|"sinceImageTransitioned",
                    "countUnit": "string",
                    "countNumber": integer
                },
                "action": {
                    "type": "expire"|"transition",
                    "targetStorageClass": "archive"
                }
            }
        ]
    }
```

## 篩選映像存在時間
<a name="lifecycle_policy_example_age"></a>

以下範例顯示政策的生命週期政策語法，該政策能尋找以 `prod` 標籤開頭的映像，使用 `prod*` 的`tagPatternList`，將存在時間同樣超過 `14` 天的映像設為過期。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Expire images older than 14 days",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 14
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

## 篩選上次提取的時間
<a name="lifecycle_policy_example_last_pulled"></a>

下列範例顯示政策的生命週期政策語法，該政策會將映像轉換為未在`90`幾天內提取的封存儲存。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive images not pulled in 90 days",
            "selection": {
                "tagStatus": "any",
                "countType": "sinceImagePulled",
                "countUnit": "days",
                "countNumber": 90
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        }
    ]
}
```

**重要**  
`sinceImagePulled` 計數類型必須與 `transition`動作搭配使用。它無法與 `expire`動作搭配使用。若要根據提取活動刪除映像，請先將其轉換為使用 封存儲存體`sinceImagePulled`，然後使用 `sinceImageTransitioned`搭配 `expire`動作來刪除它們。在刪除之前，映像必須位於封存儲存中至少 90 天。

## 篩選封存轉換時間
<a name="lifecycle_policy_example_transitioned"></a>

下列範例顯示政策的生命週期政策語法，該政策會將封存儲存超過 `365` 天的封存映像過期。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Expire images archived for more than 365 days",
            "selection": {
                "tagStatus": "any",
                "storageClass": "archive",
                "countType": "sinceImageTransitioned",
                "countUnit": "days",
                "countNumber": 365
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

**重要**  
`sinceImageTransitioned` 計數類型必須與 `expire`動作和 `archive`儲存體方案搭配使用。在刪除之前，映像必須位於封存儲存中至少 90 天。

## 篩選映像計數
<a name="lifecycle_policy_example_number"></a>

以下範例顯示政策的生命週期政策語法，該政策能只保留一個未標記的映像，並將其他所有映像設為過期。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Keep only one untagged image, expire all others",
            "selection": {
                "tagStatus": "untagged",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

## 篩選多個規則
<a name="lp_example_multiple"></a>

以下是在生命週期政策中使用多個規則的範例。範例儲存庫與指定的生命週期政策與結果說明同時提供。

### 範例 A
<a name="lp_example_multiple_a"></a>

儲存庫內容：
+ Image A, Taglist: ["beta-1", "prod-1"], Pushed: 10 days ago
+ Image B, Taglist: ["beta-2", "prod-2"], Pushed: 9 days ago
+ Image C, Taglist: ["beta-3"], Pushed: 8 days ago

生命週期政策文字：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 2,
            "description": "Rule 2",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["beta*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

生命週期政策的邏輯會是：
+ 規則 1 找出含有前綴 `prod` 標記的映像。將會從最舊的映像開始標記，直到沒有或剩餘很少符合的映像。標記映像 A 為過期。
+ 規則 2 找出含有前綴 `beta` 標記的映像。將會從最舊的映像開始標記，直到沒有或剩餘很少符合的映像。標記映像 A 與映像 B 為過期。但是，映像 A 已被規則 1 看到，而若映像 B 已過期，則會因違反規則 1 而被略過。
+ 結果：映像 A 已過期。

### 範例：B
<a name="lp_example_multiple_b"></a>

這是與前一個範例相同的儲存庫，但是規則優先順序已變更以說明結果。

儲存庫內容：
+ Image A, Taglist: ["beta-1", "prod-1"], Pushed: 10 days ago
+ Image B, Taglist: ["beta-2", "prod-2"], Pushed: 9 days ago
+ Image C, Taglist: ["beta-3"], Pushed: 8 days ago

生命週期政策文字：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["beta*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 2,
            "description": "Rule 2",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

生命週期政策的邏輯會是：
+ 規則 1 找出含有前綴 `beta` 標記的映像。將會從最舊的映像開始標記，直到沒有或剩餘很少符合的映像。將看到所有三個映像並標記映像 A 與映像 B 為過期。
+ 規則 2 找出含有前綴 `prod` 標記的映像。將會從最舊的映像開始標記，直到沒有或剩餘很少符合的映像。將不會看到映像，因所有可用映像已被規則 1 看到，因此將不會標記其他映像。
+ 結果：映像 A 與 B 皆已過期。

## 篩選單一規則中的多個標籤
<a name="lp_example_difftype"></a>

以下範例說明在單一規則中採用多標籤模式的生命週期政策語法。範例儲存庫與指定的生命週期政策與結果說明同時提供。

### 範例 A
<a name="lp_example_difftype_a"></a>

當單一規則指定多個標籤模式，映像必須符合所有列出的標籤模式。

儲存庫內容：
+ Image A, Taglist: ["alpha-1"], Pushed: 12 days ago
+ Image B, Taglist: ["beta-1"], Pushed: 11 days ago
+ Image C, Taglist: ["alpha-2", "beta-2"], Pushed: 10 days ago
+ Image D, Taglist: ["alpha-3"], Pushed: 4 days ago
+ Image E, Taglist: ["beta-3"], Pushed: 3 days ago
+ Image F, Taglist: ["alpha-4", "beta-4"], Pushed: 2 days ago

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["alpha*", "beta*"],
                "countType": "sinceImagePushed",
                "countNumber": 5,
                "countUnit": "days"
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

生命週期政策的邏輯會是：
+ 規則 1 找出含有前綴 `alpha` 和 `beta` 標籤的映像。看到映像 C 與 F。應標記存在時間大於五天的映像，應是映像 C。
+ 結果：映像 C 已過期。

### 範例：B
<a name="lp_example_difftype_b"></a>

下列範例說明非專屬的標籤。

儲存庫內容：
+ Image A, Taglist: ["alpha-1", "beta-1", "gamma-1"], Pushed: 10 days ago
+ Image B, Taglist: ["alpha-2", "beta-2"], Pushed: 9 days ago
+ Image C, Taglist: ["alpha-3", "beta-3", "gamma-2"], Pushed: 8 days ago

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["alpha*", "beta*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

生命週期政策的邏輯會是：
+ 規則 1 找出含有前綴 `alpha` 和 `beta` 標籤的映像。看到所有映像。將會從最舊的映像開始標記，直到沒有或剩餘很少符合的映像。標記映像 A 與 B 為過期。
+ 結果：映像 A 與 B 皆已過期。

## 篩選所有映像
<a name="lp_example_allimages"></a>

以下生命週期政策範例說明含有不同篩選條件的映像。範例儲存庫與指定的生命週期政策與結果說明同時提供。

### 範例 A
<a name="lp_example_difftype_a"></a>

下列顯示套用到所有規則的政策之生命週期政策語法，但是只保留一個映像並將其他所有映像設為過期。

儲存庫內容：
+ Image A, Taglist: ["alpha-1"], Pushed: 4 days ago
+ Image B, Taglist: ["beta-1"], Pushed: 3 days ago
+ Image C, Taglist: [], Pushed: 2 days ago
+ Image D, Taglist: ["alpha-2"], Pushed: 1 day ago

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "any",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

生命週期政策的邏輯會是：
+ 規則 1 找出所有映像。會看到映像 A、B、C 與 D。除了最新映像外，應將所有映像設為過期。標記映像 A、B、C 為過期。
+ 結果：映像 A、B 與 C 皆已過期。

### 範例：B
<a name="lp_example_difftype_b"></a>

以下範例說明在單一規則中整合所有規則類型的生命週期政策。

儲存庫內容：
+ Image A, Taglist: ["alpha-", "beta-1", "-1"], Pushed: 4 days ago
+ Image B, Taglist: [], Pushed: 3 days ago
+ Image C, Taglist: ["alpha-2"], Pushed: 2 days ago
+ Image D, Taglist: ["git hash"], Pushed: 1 day ago
+ Image E, Taglist: [], Pushed: 1 day ago

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Rule 1",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["alpha*"],
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 2,
            "description": "Rule 2",
            "selection": {
                "tagStatus": "untagged",
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        },
        {
            "rulePriority": 3,
            "description": "Rule 3",
            "selection": {
                "tagStatus": "any",
                "countType": "imageCountMoreThan",
                "countNumber": 1
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```

生命週期政策的邏輯會是：
+ 規則 1 找出含有前綴 `alpha` 標記的映像。找出映像 A 與映像 C。應保留最新映像並將其他標記為過期。標記映像 A 為過期。
+ 規則 2 找出未標記的映像。找出映像 B 與映像 E。應標記所有超過一天的映像為過期。標記映像 B 為過期。
+ 規則 3 找出所有映像。找出映像 A、B、C、D 與 E。應保留最新映像並將其他標記為過期。但是，無法標記映像 A、B、C 或 E，因為會以較高的優先順序規則來找出它們。標記映像 D 為過期。
+ 結果：映像 A、B 與 D 皆已過期。

## 存檔範例
<a name="lp_example_archive"></a>

下列範例顯示存檔映像而非刪除映像的生命週期政策。

### 封存超過指定天數的影像
<a name="lp_example_archive_age"></a>

下列範例顯示生命週期政策，以超過 `prod`30 天的標籤封存映像：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive production images older than 30 days",
            "selection": {
                "tagStatus": "tagged",
                "tagPatternList": ["prod*"],
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 30
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        }
    ]
}
```

### 封存未在指定的天數內提取的映像
<a name="lp_example_archive_pull"></a>

下列範例顯示生命週期政策，該政策會封存 90 天內尚未提取的影像：

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive images not pulled in 90 days",
            "selection": {
                "tagStatus": "any",
                "countType": "sinceImagePulled",
                "countUnit": "days",
                "countNumber": 90
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        }
    ]
}
```

### 合併封存和過期規則
<a name="lp_example_archive_delete"></a>

下列範例顯示的生命週期政策會封存超過 30 天的映像，然後永久過期已封存超過 365 天的映像：

**注意**  
封存映像的最短儲存期間為 90 天。您無法設定生命週期政策來刪除已存檔不到 90 天的映像。如果您必須刪除已封存不到 90 天的映像，則需要使用 **batch-delete-image** API，但需支付 90 天最低儲存持續時間的費用。

```
{
    "rules": [
        {
            "rulePriority": 1,
            "description": "Archive images older than 30 days",
            "selection": {
                "tagStatus": "any",
                "countType": "sinceImagePushed",
                "countUnit": "days",
                "countNumber": 30
            },
            "action": {
                "type": "transition",
                "targetStorageClass": "archive"
            }
        },
        {
            "rulePriority": 2,
            "description": "Expire images archived for more than 365 days",
            "selection": {
                "tagStatus": "any",
                "storageClass": "archive",
                "countType": "sinceImageTransitioned",
                "countUnit": "days",
                "countNumber": 365
            },
            "action": {
                "type": "expire"
            }
        }
    ]
}
```