

# 恢复导入操作
<a name="resource-import-revert"></a>

要恢复导入操作，请为要从模板中删除的资源指定 `Retain` 删除策略，以确保从堆栈中删除时保留该资源。

## 使用 AWS 管理控制台恢复导入操作
<a name="resource-import-revert-console"></a>

1. 为要从堆栈中删除的资源指定 `Retain` [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)。在以下示例模板中，`GamesTable` 是该恢复操作的目标。  
**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"
                    }
                 ],
                 "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"
                       }
                   ],
                   "ProvisionedThroughput": {
                       "ReadCapacityUnits": 5,
                       "WriteCapacityUnits": 1
                   }
               }
           }
       }
   }
   ```

1. 打开 CloudFormation 控制台以执行堆栈更新，从而应用删除策略。

   1. 在 **Stacks (堆栈)** 页面上，在选择了堆栈的情况下，选择 **Update (更新)**，然后选择 **Update stack (standard) (更新堆栈 (标准))**。

   1. 在 **Prepare template (准备模板)** 下面，选择 **Replace current template (替换当前模板)**。

   1. 在 **Specify template (指定模板)** 下面，在 `GamesTable` 上为更新的源模板提供 `DeletionPolicy` 属性，然后选择 **Next (下一步)**。
      + 选择 **Amazon S3 URL**，然后在文本框中指定更新的源模板的 URL。
      + 选择 **Upload a template file (上传模板文件)**，然后浏览更新的源模板文件。

   1. 在 **Specify stack details (指定堆栈详细信息)** 页面上，不需要进行任何更改。选择**下一步**。

   1. 在 **Configure stack options (配置堆栈选项)** 页面上，不需要进行任何更改。选择**下一步**。

   1. 在**查看 *MyStack*** 页面上，检查所做的更改。如果您的模板包含 IAM 资源，请选择 **I acknowledge that this template may create IAM resources (我确认此模板可创建 IAM 资源)** 以指定您要使用模板中的 IAM 资源。有关更多信息，请参阅 [确认 CloudFormation 模板中的 IAM 资源](control-access-with-iam.md#using-iam-capabilities)。然后，创建更改集以更新源堆栈，或者直接更新源堆栈。

1. 从堆栈模板中删除资源、相关参数和输出。在该示例中，模板现在如下所示。  
**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"
                    }
                 ],
                 "ProvisionedThroughput":{
                    "ReadCapacityUnits":5,
                    "WriteCapacityUnits":1
                 }
              }
           }
       }
   }
   ```

1. 重复步骤 2，以从堆栈中删除资源 (`GamesTable`) 及其相关参数和输出。

## 使用 AWS CLI恢复导入操作
<a name="resource-import-revert-cli"></a>

1. 为要从堆栈中删除的资源指定 `Retain` [DeletionPolicy](https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-attribute-deletionpolicy.html)。在以下示例模板中，`GamesTable` 是该恢复操作的目标。  
**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"
                    }
                 ],
                 "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"
                       }
                   ],
                   "ProvisionedThroughput": {
                       "ReadCapacityUnits": 5,
                       "WriteCapacityUnits": 1
                   }
               }
           }
       }
   }
   ```

1. 更新堆栈以将删除策略应用于资源。

   ```
   aws cloudformation update-stack --stack-name MyStack
   ```

1. 从堆栈模板中删除资源、相关参数和输出。在该示例中，模板现在如下所示。  
**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"
                    }
                 ],
                 "ProvisionedThroughput":{
                    "ReadCapacityUnits":5,
                    "WriteCapacityUnits":1
                 }
              }
           }
       }
   }
   ```

1. 更新堆栈以从堆栈中删除资源 (`GamesTable`) 及其相关参数和输出。

   ```
   aws cloudformation update-stack --stack-name MyStack
   ```