

# インポートオペレーションを元に戻す
<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. [**スタック**] ページで、スタックを選択した状態で [**更新**] を選択し、[**Update stack (standard) (スタックを更新（標準）)**] を選択します。

   1. [**テンプレートを準備**] で、[**現在のテンプレートを置換**] を選択します。

   1. [**Specify template (テンプレートを指定)**] の下にある `GamesTable` で、`DeletionPolicy` 属性により更新されたソーステンプレートを指定し、[**次へ**] を選択します。
      + [**Amazon S3 URL**] を選択し、テキストボックスで更新されたソーステンプレートの URL を指定します。
      + [**Upload a template file (テンプレートファイルのアップロード)**] を選択し、更新されたソーステンプレートファイルを参照します。

   1. [**Specify stack details (スタック詳細の指定)**] ページでは、変更の必要はありません。[**次へ**] を選択します。

   1. [**スタックオプションの設定**] ページでは、変更の必要はありません。[**次へ**] を選択します。

   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}}
   ```