

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

# 管理允許的 AMI 的設定
<a name="manage-settings-allowed-amis"></a>

您可管理允許的 AMI 的設定。這些設定依據每個帳戶每個區域設定。

**Topics**
+ [啟用允許 AMI](#enable-allowed-amis-criteria)
+ [設定允許的 AMI 條件](#update-allowed-amis-criteria)
+ [停用允許 AMI](#disable-allowed-amis-criteria)
+ [獲取允許的 AMI 條件](#identify-allowed-amis-state-and-criteria)
+ [尋找允許的 AMI](#identify-amis-that-meet-allowed-amis-criteria)
+ [尋找透過不允許的 AMI 啟動的執行個體](#identify-instances-with-allowed-AMIs)

## 啟用允許 AMI
<a name="enable-allowed-amis-criteria"></a>

您可啟用允許的 AMI 並指定允許的 AMI 條件 建議以稽核模式開始，該模式可顯示哪些 AMI 會受條件影響，而不會實際限制存取權。

------
#### [ Console ]

**若要啟用允許 AMI**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇 **Dashboard (儀表板)**。

1. 在**帳戶屬性**卡的設定下****，選擇**允許的 AMIs**。

1. 在**允許的 AMI** 索引標籤上，選擇**管理**。

1. 針對**允許的 AMI 設定**，選擇**稽核模式**或**已啟用**。建議以稽核模式開始，測試條件，然後返回此步驟，以便啟用允許的 AMI。

1. (選用) 針對 **AMI 條件**，輸入 JSON 格式的條件。

1. 選擇**更新**。

------
#### [ AWS CLI ]

**若要啟用允許 AMI**  
請使用 [enable-allowed-images-settings](https://docs.aws.amazon.com/cli/latest/reference/ec2/enable-allowed-images-settings.html) 命令。

```
aws ec2 enable-allowed-images-settings --allowed-images-settings-state enabled
```

如需轉為啟用稽核模式，指定 `audit-mode` 而不是 `enabled`。

```
aws ec2 enable-allowed-images-settings --allowed-images-settings-state audit-mode
```

------
#### [ PowerShell ]

**若要啟用允許 AMI**  
使用 [Enable-EC2AllowedImagesSetting](https://docs.aws.amazon.com/powershell/latest/reference/items/Enable-EC2AllowedImagesSetting.html) cmdlet。

```
Enable-EC2AllowedImagesSetting -AllowedImagesSettingsState enabled
```

如需轉為啟用稽核模式，指定 `audit-mode` 而不是 `enabled`。

```
Enable-EC2AllowedImagesSetting -AllowedImagesSettingsState audit-mode
```

------

## 設定允許的 AMI 條件
<a name="update-allowed-amis-criteria"></a>

在啟用允許的 AMI 之後，您可設定或取代允許的 AMI 條件。

如需了解正確的組態與有效值，請參閱 [允許的 AMI 組態](ec2-allowed-amis.md#allowed-amis-json-configuration) 和 [允許的 AMI 參數](ec2-allowed-amis.md#allowed-amis-criteria)。

------
#### [ Console ]

**如需設定允許的 AMI 條件**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇 **Dashboard (儀表板)**。

1. 在**帳戶屬性**卡的設定下****，選擇**允許的 AMIs**。

1. 在**允許的 AMI** 索引標籤上，選擇**管理**。

1. 針對 **AMI 條件**，輸入 JSON 格式的條件。

1. 選擇**更新**。

------
#### [ AWS CLI ]

**如需設定允許的 AMI 條件**  
請使用 [replace-image-criteria-in-allowed-images-settings](https://docs.aws.amazon.com/cli/latest/reference/ec2/replace-image-criteria-in-allowed-images-settings.html) 命令，然後指定包含允許的 AMI 條件的 JSON 檔案。

```
aws ec2 replace-image-criteria-in-allowed-images-settings --cli-input-json file://file_name.json
```

------
#### [ PowerShell ]

**如需設定允許的 AMI 條件**  
請使用 [Set-EC2ImageCriteriaInAllowedImagesSetting](https://docs.aws.amazon.com/powershell/latest/reference/items/Set-EC2ImageCriteriaInAllowedImagesSetting.html) cmdlet，然後指定包含允許的 AMI 條件的 JSON 檔案。

```
$imageCriteria = Get-Content -Path .\file_name.json | ConvertFrom-Json
Set-EC2ImageCriteriaInAllowedImagesSetting -ImageCriterion $imageCriteria
```

------

## 停用允許 AMI
<a name="disable-allowed-amis-criteria"></a>

您可如下所示停用允許的 AMI。

------
#### [ Console ]

**若要停用允許 AMI**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇 **Dashboard (儀表板)**。

1. 在**帳戶屬性**卡的設定下****，選擇**允許的 AMIs**。

1. 在**允許的 AMI** 索引標籤上，選擇**管理**。

1. 對於**允許 AMI 設定**，選擇**已停用**。

1. 選擇**更新**。

------
#### [ AWS CLI ]

**若要停用允許 AMI**  
請使用 [disable-allowed-images-settings](https://docs.aws.amazon.com/cli/latest/reference/ec2/disable-allowed-images-settings.html) 命令。

```
aws ec2 disable-allowed-images-settings
```

------
#### [ PowerShell ]

**若要停用允許 AMI**  
使用 [Disable-EC2AllowedImagesSetting](https://docs.aws.amazon.com/powershell/latest/reference/items/Disable-EC2AllowedImagesSetting.html) cmdlet。

```
Disable-EC2AllowedImagesSetting
```

------

## 獲取允許的 AMI 條件
<a name="identify-allowed-amis-state-and-criteria"></a>

您可獲取允許的 AMI 設定的目前狀態及允許的 AMI 條件。

------
#### [ Console ]

**如需獲取允許的 AMI 狀態及條件**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇 **Dashboard (儀表板)**。

1. 在**帳戶屬性**卡的設定下****，選擇**允許的 AMIs**。

1. 在**允許的 AMI** 索引標籤上，將**允許的 AMI 設定**設定為**已啟用**、**已停用**或**稽核模式**。

1. 若允許的 AMI 狀態為**已啟用**或**稽核模式**、**AMI 條件**，會顯示 JSON 格式的 AMI 條件。

------
#### [ AWS CLI ]

**如需獲取允許的 AMI 狀態及條件**  
請使用 [get-allowed-images-settings](https://docs.aws.amazon.com/cli/latest/reference/ec2/get-allowed-images-settings.html) 命令。

```
aws ec2 get-allowed-images-settings
```

在下面的範例輸出中，狀態為 `audit-mode`，且映像條件已在帳戶中設定。

```
{
    "State": "audit-mode",
    "ImageCriteria": [
        {
            "MarketplaceProductCodes": [
                "abcdefg1234567890"
            ]
        },
        {
            "ImageProviders": [
                "123456789012",
                "123456789013"
            ],
            "CreationDateCondition": {
                "MaximumDaysSinceCreated": 300
            }
        },
        {
            "ImageProviders": [
                "123456789014"
            ],
            "ImageNames": [
                "golden-ami-*"
            ]
        },
        {
            "ImageProviders": [
                "amazon"
            ],
            "DeprecationTimeCondition": {
                "MaximumDaysSinceDeprecated": 0
            }
        }
    ],
    "ManagedBy": "account"
}
```

------
#### [ PowerShell ]

**如需獲取允許的 AMI 狀態及條件**  
使用 [Get-EC2AllowedImagesSetting](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2AllowedImagesSetting.html) cmdlet。

```
Get-EC2AllowedImagesSetting | Select-Object `
    State, `
    ManagedBy, `
    @{Name='ImageProviders'; Expression={($_.ImageCriteria.ImageProviders)}}, `
    @{Name='MarketplaceProductCodes'; Expression={($_.ImageCriteria.MarketplaceProductCodes)}}, `
    @{Name='ImageNames'; Expression={($_.ImageCriteria.ImageNames)}}, `
    @{Name='MaximumDaysSinceCreated'; Expression={($_.ImageCriteria.CreationDateCondition.MaximumDaysSinceCreated)}}, `
    @{Name='MaximumDaysSinceDeprecated'; Expression={($_.ImageCriteria.DeprecationTimeCondition.MaximumDaysSinceDeprecated)}}
```

在下面的範例輸出中，狀態為 `audit-mode`，且映像條件已在帳戶中設定。

```
State      : audit-mode
ManagedBy  : account
ImageProviders            : {123456789012, 123456789013, 123456789014, amazon}
MarketplaceProductCodes   : {abcdefg1234567890}
ImageNames                : {golden-ami-*}
MaximumDaysSinceCreated  : 300
MaximumDaysSinceDeprecated: 0
```

------

## 尋找允許的 AMI
<a name="identify-amis-that-meet-allowed-amis-criteria"></a>

您可尋找依據目前允許的 AMI 條件而被允許或不被允許的 AMI。

**注意**  
允許的 AMI 必須為稽核模式。

------
#### [ Console ]

**如需檢查 AMI 是否滿足允許的 AMI 條件**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇 **AMI**。

1. 請選取 AMI。

1. 在**詳細資料**索引標籤 (如選取核取方塊) 或在摘要區域 (如選取 AMI ID)，找到**允許映像**欄位。
   + **是** – AMI 滿足允許的 AMI 條件。在啟用允許的 AMI 之後，您帳戶中的使用者將可使用此 AMI。
   + **否** – AMI 不滿足允許的 AMI 條件。

1. 在導覽窗格中，選擇 **AMI Catalog** (AMI 型錄)。

   當 AMI 標記為**不允許**時，表示其不符合允許 AMI 條件。當啟用允許 AMI 時，您帳戶的使用者將無法看到或使用此 AMI。

------
#### [ AWS CLI ]

**如需檢查 AMI 是否滿足允許的 AMI 條件**  
使用 [describe-images](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html) 命令。

```
aws ec2 describe-images \
    --image-id ami-0abcdef1234567890 \
    --query Images[].ImageAllowed \
    --output text
```

以下為範例輸出。

```
True
```

**如需尋找滿足允許的 AMI 條件的 AMI**  
使用 [describe-images](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-images.html) 命令。

```
aws ec2 describe-images \
    --filters "Name=image-allowed,Values=true" \
    --max-items 10 \
    --query Images[].ImageId
```

以下為範例輸出。

```
ami-000eaaa8be2fd162a
ami-000f82db25e50de8e
ami-000fc21eb34c7a9a6
ami-0010b876f1287d7be
ami-0010b929226fe8eba
ami-0010957836340aead
ami-00112c992a47ba871
ami-00111759e194abcc1
ami-001112565ffcafa5e
ami-0011e45aaee9fba88
```

------
#### [ PowerShell ]

**如需檢查 AMI 是否滿足允許的 AMI 條件**  
使用 [Get-EC2Image](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Image.html) cmdlet。

```
(Get-EC2Image -ImageId ami-0abcdef1234567890).ImageAllowed
```

以下為範例輸出。

```
True
```

**如需尋找滿足允許的 AMI 條件的 AMI**  
使用 [Get-EC2Image](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2Image.html) cmdlet。

```
Get-EC2Image `
    -Filter @{Name="image-allows";Values="true"} `
    -MaxResult 10 | `
    Select ImageId
```

以下為範例輸出。

```
ami-000eaaa8be2fd162a
ami-000f82db25e50de8e
ami-000fc21eb34c7a9a6
ami-0010b876f1287d7be
ami-0010b929226fe8eba
ami-0010957836340aead
ami-00112c992a47ba871
ami-00111759e194abcc1
ami-001112565ffcafa5e
ami-0011e45aaee9fba88
```

------

## 尋找透過不允許的 AMI 啟動的執行個體
<a name="identify-instances-with-allowed-AMIs"></a>

您可確定使用不滿足允許的 AMI 條件的 AMI 啟動的執行個體。

------
#### [ Console ]

**如需檢查識別執行個體是否使用不允許的 AMI 啟動**

1. 前往 [https://console.aws.amazon.com/ec2/](https://console.aws.amazon.com/ec2/) 開啟 Amazon EC2 主控台。

1. 在導覽窗格中，選擇**執行個體**。

1. 選取執行個體。

1. 在**詳細資訊**索引標籤的**執行個體詳細資訊**下，找到**允許的映像**。
   + **是** – AMI 滿足允許的 AMI 條件。
   + **否** – AMI 不滿足允許的 AMI 條件。

------
#### [ AWS CLI ]

**如需尋找使用不允許的 AMI 啟動的執行個體**  
請使用 `image-allowed` 篩選條件並搭配 [describe-instance-image-metadata](https://docs.aws.amazon.com/cli/latest/reference/ec2/describe-instance-image-metadata.html) 命令。

```
aws ec2 describe-instance-image-metadata \
    --filters "Name=image-allowed,Values=false" \
    --query "InstanceImageMetadata[*].[InstanceId,ImageMetadata.ImageId]" \
    --output table
```

以下為範例輸出。

```
--------------------------------------------------
|          DescribeInstanceImageMetadata         |
+----------------------+-------------------------+
|  i-08fd74f3f1595fdbd |  ami-09245d5773578a1d6  |
|  i-0b1bf24fd4f297ab9 |  ami-07cccf2bd80ed467f  |
|  i-026a2eb590b4f7234 |  ami-0c0ec0a3a3a4c34c0  |
|  i-006a6a4e8870c828f |  ami-0a70b9d193ae8a799  |
|  i-0781e91cfeca3179d |  ami-00c257e12d6828491  |
|  i-02b631e2a6ae7c2d9 |  ami-0bfddf4206f1fa7b9  |
+----------------------+-------------------------+
```

------
#### [ PowerShell ]

**如需尋找使用不允許的 AMI 啟動的執行個體**  
使用 [Get-EC2InstanceImageMetadata](https://docs.aws.amazon.com/powershell/latest/reference/items/Get-EC2InstanceImageMetadata.html) cmdlet。

```
Get-EC2InstanceImageMetadata `
    -Filter @{Name="image-allowed";Values="false"} | `
    Select InstanceId, @{Name='ImageId'; Expression={($_.ImageMetadata.ImageId)}}
```

以下為範例輸出。

```
InstanceId          ImageId
----------          -------
i-08fd74f3f1595fdbd ami-09245d5773578a1d6
i-0b1bf24fd4f297ab9 ami-07cccf2bd80ed467f
i-026a2eb590b4f7234 ami-0c0ec0a3a3a4c34c0
i-006a6a4e8870c828f ami-0a70b9d193ae8a799
i-0781e91cfeca3179d ami-00c257e12d6828491
i-02b631e2a6ae7c2d9 ami-0bfddf4206f1fa7b9
```

------
#### [ AWS Config ]

您可以新增 **ec2-instance-launched-with-allowed-ami** AWS Config 規則、根據您的需求進行設定，然後使用它來評估您的執行個體。

如需詳細資訊，請參閱《 *AWS Config 開發人員指南*》中的[新增 AWS Config 規則](https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_add-rules.html)和 [ec2-instance-launched-with-allowed-ami](https://docs.aws.amazon.com/config/latest/developerguide/ec2-instance-launched-with-allowed-ami.html)。

------