

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

# 透過匯入作業解決漂移
<a name="resource-import-resolve-drift"></a>

在某些情況下，資源的組態已經從其預期組態中漂移，並且您想要接受新的組態作為預期組態。在大多數情況下，您會透過使用新組態更新堆疊範本中的資源來定義，然後執行堆疊更新來解決漂移結果。不過，如果新組態更新需要取代的資源屬性，則會在堆疊更新期間重新建立資源。如果您想要保留現有資源，可以使用資源匯入功能來更新資源並解決漂移結果，而不會導致資源被取代。

透過匯入作業解決資源的漂移包含下列基本步驟：
+ [將 DeletionPolicy 屬性 (設為「保留」) 新增至資源](#resource-import-resolve-drift-console-step-01-update-stack)。這可確保現有資源在從堆疊中移除時保留，而不是刪除它。
+ [從範本中移除資源，並執行堆疊更新作業](#resource-import-resolve-drift-console-step-02-remove-drift)。這會從堆疊中移除資源，但不會刪除它。
+ [在堆疊範本中描述資源的實際狀態，然後再將現有資源匯入堆疊](#resource-import-resolve-drift-console-step-03-update-template)。這會將資源新增回堆疊，並解決造成漂移結果的屬性差異。

如需資源匯入的詳細資訊，請參閱 [手動將 AWS 資源匯入 CloudFormation 堆疊](import-resources-manually.md)。如需支援匯入的資源清單，請參閱 [資源類型支援](resource-import-supported-resources.md)。

在此範例中，我們使用下列範本，名為 `templateToImport.json`。

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

```
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Import test",
    "Resources": {
         "ServiceTable":{
           "Type":"AWS::DynamoDB::Table",
           "Properties":{
              "TableName":"Service",
              "AttributeDefinitions":[
                 {
                    "AttributeName":"key",
                    "AttributeType":"S"
                 }
              ],
              "KeySchema":[
                 {
                    "AttributeName":"key",
                    "KeyType":"HASH"
                 }
              ],
              "BillingMode": "PROVISIONED",
              "ProvisionedThroughput":{
                 "ReadCapacityUnits":5,
                 "WriteCapacityUnits":1
              }
           }
        },
        "GamesTable": {
            "Type": "AWS::DynamoDB::Table",
            "Properties": {
                "TableName": "Games",
                "AttributeDefinitions": [
                    {
                        "AttributeName": "key",
                        "AttributeType": "S"
                    }
                ],
                "KeySchema": [
                    {
                        "AttributeName": "key",
                        "KeyType": "HASH"
                    }
                ],
                "BillingMode": "PROVISIONED",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": 5,
                    "WriteCapacityUnits": 1
                }
            }
        }
    }
}
```

------
#### [ Example YAML ]

```
AWSTemplateFormatVersion: 2010-09-09
Description: Import test
Resources:
  ServiceTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Service
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      BillingMode: PROVISIONED
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 1
  GamesTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Games
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      BillingMode: PROVISIONED
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 1
```

------

在此範例中，讓我們假設使用者變更了 CloudFormation 外部的資源。執行漂移檢測後，我們發現 `GamesTable` 已將 `BillingMode` 修改為 `PAY_PER_REQUEST`。如需關於漂移偵測的詳細資訊，請參閱 [透過漂移偵測功能，偵測堆疊和資源的未受管組態變更](using-cfn-stack-drift.md)。

![\[漂移結果會在主控台中顯示預期和實際結果。\]](http://docs.aws.amazon.com/zh_tw/AWSCloudFormation/latest/UserGuide/images/drift-results-gamestable.png)


我們的堆疊現已過時，而我們的資源卻是即時的，但我們還是希望能保留預期的資源組態。我們可以透過導入操作解決漂移而不中斷服務來做到這一點。

## 使用 CloudFormation 主控台透過匯入作業解決漂移
<a name="resource-import-resolve-drift-console"></a>

### 步驟 1. 使用保留刪除政策更新堆疊
<a name="resource-import-resolve-drift-console-step-01-update-stack"></a>

**使用具有 `Retain` 選項的 `DeletionPolicy` 屬性來更新堆疊**

1. 登入 AWS 管理主控台 並在 https：//[https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/cloudformation/) 開啟 CloudFormation 主控台。

1. 在**堆疊**頁面上，選擇已漂移的堆疊。

1. 選擇**更新**，然後從堆疊詳細資料窗格中選擇**取代目前的範本**。

1. 在**指定範本**頁面上，使用下列其中一種方法，透過 `Retain` 選項提供包含 `DeletionPolicy` 屬性的更新範本：
   + 選擇 **Amazon S3 URL**，然後在文字方塊中為您的範本指定 URL。
   + 選擇 **Upload a template file (上傳範本檔案)**，然後瀏覽您的範本。

   然後選擇**下一步**。

1. 複查**指定堆疊詳細資料**頁面，然後選擇**下一步**。

1. 檢閱**配置堆疊選項**頁面，並選擇**下一步**。

1. 在 **檢閱 *stack-name***頁面上，選擇**更新堆疊**。

*結果*：在堆疊的**事件**頁面上，狀態為 `UPDATE_COMPLETE`。

若要解決匯入作業的漂移，而不會中斷服務，請為您要從堆疊中移除的資源指定 `Retain` [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)。在下列範例中，我們已將 [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html) 屬性 (設為 `Retain`) 新增至 `GamesTable` 資源。

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

```
    "GamesTable": {
        "Type": "AWS::DynamoDB::Table",
        "DeletionPolicy": "Retain",
        "Properties": {
            "TableName": "Games",
```

------
#### [ Example YAML ]

```
  GamesTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
      TableName: Games
```

------

### 步驟 2. 移除漂移的資源、相關參數和輸出
<a name="resource-import-resolve-drift-console-step-02-remove-drift"></a>

**移除漂移資源、相關參數和輸出**

1. 選擇**更新**，然後從堆疊詳細資料窗格中選擇**取代目前的範本**。

1. 在**指定範本**頁面上，使用下列其中一種方法，提供已更新範本的資源、相關參數和輸出：
   + 選擇 **Amazon S3 URL**，然後在文字方塊中為您的範本指定 URL。
   + 選擇 **Upload a template file (上傳範本檔案)**，然後瀏覽您的範本。

   然後選擇**下一步**。

1. 複查**指定堆疊詳細資料**頁面，然後選擇**下一步**。

1. 檢閱**配置堆疊選項**頁面，並選擇**下一步**。

1. 在 **檢閱 *stack-name***頁面上，選擇**更新堆疊**。

*結果*：**邏輯 ID** `GamesTable` 在堆疊的**事件**頁面 上的狀態為 `DELETE_SKIPPED`。

等待直到 CloudFormation 完成堆疊更新作業。堆疊更新作業完成後，請從堆疊範本移除資源、相關參數和輸出。然後，匯入更新的範本。完成這些動作之後，範例範本現在如下所示。

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

```
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Import test",
    "Resources": {
         "ServiceTable":{
           "Type":"AWS::DynamoDB::Table",
           "Properties":{
              "TableName":"Service",
              "AttributeDefinitions":[
                 {
                    "AttributeName":"key",
                    "AttributeType":"S"
                 }
              ],
              "KeySchema":[
                 {
                    "AttributeName":"key",
                    "KeyType":"HASH"
                 }
              ],
              "BillingMode": "PROVISIONED",
              "ProvisionedThroughput":{
                 "ReadCapacityUnits":5,
                 "WriteCapacityUnits":1
              }
           }
        }
    }
}
```

------
#### [ Example YAML ]

```
AWSTemplateFormatVersion: 2010-09-09
Description: Import test
Resources:
  ServiceTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Service
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      BillingMode: PROVISIONED
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 1
```

------

### 步驟 3。更新範本以符合資源的即時狀態
<a name="resource-import-resolve-drift-console-step-03-update-template"></a>

**更新範本以符合資源的即時狀態**

1. 若要匯入更新的範本，請選擇**堆疊動作**，然後選擇**將資源匯入堆疊**。  
![\[主控台中 Import resource into stack (將資源匯入堆疊) 的選項。\]](http://docs.aws.amazon.com/zh_tw/AWSCloudFormation/latest/UserGuide/images/stack-actions-import.png)

1. 請閱讀**匯入概觀**頁面，以取得您在此操作期間必須提供的項目清單，接著選擇**下一步**。

1. 在**指定範本**頁面上，使用以下其中一種方法提供您更新後的範本。
   + 選擇 **Amazon S3 URL**，然後在文字方塊中為您的範本指定 URL。
   + 選擇 **Upload a template file (上傳範本檔案)**，然後瀏覽您的範本。

   然後選擇**下一步**。

1. 在 **Identify resources (識別資源)** 頁面上，識別每個目標資源。如需詳細資訊，請參閱[資源識別碼](import-resources-manually.md#resource-import-identifiers-unique-ids)。

   1. 在 **Identifer property (識別碼屬性)** 下方，選擇資源識別碼類型。例如， `TableName` 屬性會識別 `AWS::DynamoDB::Table` 資源。

   1. 在**識別碼值**下方，輸入實際的屬性值。在範例範本中，用於 `GamesTable` 資源的 `TableName` 是 `Games`。

   1. 選擇**下一步**。

1. 檢閱**指定堆疊詳細資料**頁面，然後選擇**下一步**。

1. 在**匯入概觀**頁面上，檢閱要匯入的資源，然後選擇**匯入資源**。這會將 `AWS::DynamoDB::Table` 資源類型導回堆疊中。

*結果*：在此範例中，我們透過匯入作業解決資源漂移，而不會中斷服務。您可以在 Events (事件) 索引標籤的 CloudFormation 主控台中檢查匯入動作的進度。匯入的資源會有一個 `IMPORT_COMPLETE` 狀態，後接**資源匯入完成**的 `CREATE_COMPLETE` 狀態作為狀態原因。

等待直到 CloudFormation 完成堆疊更新作業。堆疊更新作業完成後，請更新範本，以符合資源的實際漂移狀態。例如，`BillingMode` 將設為 `PAY_PER_REQUEST` 和 `ReadCapacityUnits`，`WriteCapacityUnits` 將設為 `0`。

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

```
{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "Import test",
    "Resources": {
         "ServiceTable":{
           "Type":"AWS::DynamoDB::Table",
           "Properties":{
              "TableName":"Service",
              "AttributeDefinitions":[
                 {
                    "AttributeName":"key",
                    "AttributeType":"S"
                 }
              ],
              "KeySchema":[
                 {
                    "AttributeName":"key",
                    "KeyType":"HASH"
                 }
              ],
              "BillingMode": "PROVISIONED",
              "ProvisionedThroughput":{
                 "ReadCapacityUnits":5,
                 "WriteCapacityUnits":1
              }
           }
        },
        "GamesTable": {
            "Type": "AWS::DynamoDB::Table",
            "DeletionPolicy": "Retain",
            "Properties": {
                "TableName": "Games",
                "AttributeDefinitions": [
                    {
                        "AttributeName": "key",
                        "AttributeType": "S"
                    }
                ],
                "KeySchema": [
                    {
                        "AttributeName": "key",
                        "KeyType": "HASH"
                    }
                ],
                "BillingMode": "PAY_PER_REQUEST",
                "ProvisionedThroughput": {
                    "ReadCapacityUnits": 0,
                    "WriteCapacityUnits": 0
                }
            }
        }
    }
}
```

------
#### [ Example YAML ]

```
AWSTemplateFormatVersion: 2010-09-09
Description: Import test
Resources:
  ServiceTable:
    Type: AWS::DynamoDB::Table
    Properties:
      TableName: Service
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      BillingMode: PROVISIONED
      ProvisionedThroughput:
        ReadCapacityUnits: 5
        WriteCapacityUnits: 1
  GamesTable:
    Type: AWS::DynamoDB::Table
    DeletionPolicy: Retain
    Properties:
      TableName: Games
      AttributeDefinitions:
        - AttributeName: key
          AttributeType: S
      KeySchema:
        - AttributeName: key
          KeyType: HASH
      BillingMode: PAY_PER_REQUEST
      ProvisionedThroughput:
        ReadCapacityUnits: 0
        WriteCapacityUnits: 0
```

------