

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

# 還原匯入操作
<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. 在**檢閱 *stack\$1name*** 頁面上，檢閱您的變更。如果您的範本包含 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
   ```