

# 通过导入操作消除偏差
<a name="resource-import-resolve-drift"></a>

在某些情况下，资源的配置可能已偏离其预期配置，而您希望接受新配置作为预期配置。在大多数情况下，您可以通过使用新配置更新堆栈模板中的资源定义来消除偏差结果，然后执行堆栈更新。但是，如果新配置更新了需要替换的资源属性，则在堆栈更新期间将重新创建该资源。如果要保留现有资源，则可以使用资源导入功能来更新资源并消除偏差结果，而不会导致替换资源。

通过导入操作消除资源偏差的过程包括以下基本步骤：
+ [将设置为 Retain 的 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_cn/AWSCloudFormation/latest/UserGuide/images/drift-results-gamestable.png)


我们的堆栈现在已经过时，我们的资源处于活动状态，但我们希望保留预期的资源配置。为此，我们可以通过导入操作来消除偏差，而不会中断服务。

## 使用 CloudFormation 控制台通过导入操作消除偏差
<a name="resource-import-resolve-drift-console"></a>

### 步骤 1：使用 Retain 删除策略更新堆栈
<a name="resource-import-resolve-drift-console-step-01-update-stack"></a>

**使用带有 `Retain` 选项的 `DeletionPolicy` 属性更新堆栈**

1. 登录到 AWS 管理控制台 并打开 CloudFormation 控制台 [https://console.aws.amazon.com/cloudformation](https://console.aws.amazon.com/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)。在以下示例中，我们向 `GamesTable` 资源添加了一个设置为 `Retain` 的 [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html) 属性。

------
#### [ 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 resources into stack (将资源导入到堆栈) 选项。\]](http://docs.aws.amazon.com/zh_cn/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. 在 **Identifier property (标识符属性)** 下面，选择资源标识符的类型。例如，`TableName` 属性标识 `AWS::DynamoDB::Table` 资源。

   1. 在**标识符值**下，输入实际属性值。在示例模板中，`GamesTable` 资源的 `TableName` 为 `Games`。

   1. 选择**下一步**。

1. 查看**指定堆栈详细信息**页面，然后选择**下一步**。

1. 在**导入概述**页面上，查看正在导入的资源，然后选择**导入资源**。这会将 `AWS::DynamoDB::Table` 资源类型导入回您的堆栈中。

*结果*：在此示例中，我们通过导入操作消除了资源偏差，而不会中断服务。您可以在 CloudFormation 控制台的“Events (事件)”选项卡中检查导入操作的进度。导入的资源将具有 `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
```

------