在堆疊之間移動資源 - AWS CloudFormation

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

在堆疊之間移動資源

使用 resource import 功能,您可以在資源之間移動 (或是「重構」) 堆疊。您需要先將 Retain 刪除政策新增到您要移動的資源,以確保當您從來源堆疊移除該資源,並匯入到目標堆疊時,仍會保留該資源。

如果您不熟悉匯入,建議您先檢閱使用 AWS 資源導入將資源導入到 CloudFormation 堆棧中主題中的介紹性資訊。

重要

並非所有資源都支援匯入操作。從堆疊移除資源之前,請參閱支援匯入操作的資源。如果您從堆疊移除不支援匯入操作的資源,則無法將資源匯入另一個堆疊或放回來源堆疊。

使用重構堆疊 AWS Management Console

  1. 在來源範本中,RetainDeletionPolicy為您要移動的資源指定。

    在以下範例來源範本中,Games 是此重構的目標。

    範例 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 } } } } }
  2. 開啟主 AWS CloudFormation 控台以執行堆疊更新以套用刪除原則。

    1. 堆疊頁面上,已選擇堆疊的情況下選擇更新

    2. Prepare template (準備範本) 下方,選擇 Replace current template (取代目前範本)

    3. Specify template (指定範本) 下方,提供在 GamesTable 上具備 DeletionPolicy 屬性的更新來源範本,然後選擇 Next (下一步)

      • 選擇 Amazon S3 URL,然後在文字方塊中指定URL給更新的來源範本。

      • 選擇 Upload a template file (上傳範本檔案),然後瀏覽更新後的來源範本檔案。

    4. Specify stack details (指定堆疊詳細資訊) 頁面上,不需要進行任何變更。選擇 Next (下一步)

    5. Configure stack options (設定堆疊選項) 頁面上,不需要進行任何變更。選擇 Next (下一步)

    6. 關於檢SourceStackName」頁面上,檢閱您的變更。如果您的範本包含IAM資源,請選取 [我確認此範本可能會建立IAM資源],以指定您要使用範本中的IAM資源。如需有關在範本中使用IAM資源的詳細資訊,請參閱控制存取 AWS Identity and Access Management。然後,透過建立變更集合來更新來源堆疊,或直接更新來源堆疊。

  3. 從來源範本移除資源、相關參數和輸出,然後將這些項目新增到目標範本。

    來源範本現在看起來會如下。

    範例 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 } } } } }

    以下範例目標範本目前具備 PlayersTable 資源,現在也包含了 GamesTable

    範例 JSON
    { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "PlayersTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": "Players", "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 } } } } }
  4. 重複步驟 2 到 3 以再次更新來源堆疊,這一次請從堆疊刪除目標資源。

  5. 執行匯入操作,將 GamesTable 新增到目標堆疊。

    1. Stacks (堆疊) 頁面上,在選取父堆疊的情況下,選擇 Stack actions (堆疊動作),然後選擇 Import resources into stack (將資源匯入堆疊)

      主控台中 Import resource into stack (將資源匯入堆疊) 的選項。
    2. 請閱讀 Import overview (匯入概觀) 頁面,以取得您在此操作期間必須提供的項目清單。然後選擇下一步

    3. Specify template (指定範本) 頁面上,完成以下其中一項操作,然後選擇 Next (下一步)。

      • 選擇 Amazon S3 URL,然後在文字方塊URL中指定。

      • 選擇 Upload a template file (上傳範本檔案),然後瀏覽要上傳的檔案。

    4. Identify resources (識別資源) 頁面上,識別您正在移動的資源 (在此範例中為 GamesTable)。如需詳細資訊,請參閱資源識別碼

      1. Identifer property (識別碼屬性) 下方,選擇資源識別碼類型。例如,AWS::DynamoDB::Table 資源可以使用 TableName 屬性進行識別。

      2. Identifer value (識別碼值) 下方,輸入實際的屬性值。例如:GamesTables

      3. 選擇 Next (下一步)

    5. Specify stack details (識別堆疊詳細資訊) 頁面上,修改任何參數,然後選擇 Next (下一步)。這會自動建立變更集合。

      重要

      如果您修改了啟動建立、更新或刪除操作的現有參數,匯入操作便會失敗。

    6. 關於檢TargetStackName頁面上,確認正在匯入正確的資源,然後選擇 [匯入資源]。這會自動啟動在最後一個步驟中建立的變更集。此時會將任何堆疊層級標籤套用到匯入的資源。

    7. 隨即會顯示父堆疊 Stack details (堆疊詳細資訊) 頁面的 Events (事件) 窗格。

      主控台中的 Events (事件) 標籤。
      注意

      在此匯入操作後不需要在父堆疊上執行漂移偵測,因為 AWS::CloudFormation::Stack 資源已由 AWS CloudFormation進行管理。

使用重構堆疊 AWS CLI

  1. 在來源範本中,RetainDeletionPolicy為您要移動的資源指定。

    在以下範例來源範本中,GamesTable 是此重構的目標。

    範例 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 } } } } }
  2. 更新來源堆疊,以將刪除政策套用到資源。

    aws cloudformation update-stack --stack-name SourceStackName
  3. 從來源範本移除資源、相關參數和輸出,然後將這些項目新增到目標範本。

    來源範本現在看起來會如下。

    範例 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 } } } } }

    以下範例目標範本目前具備 PlayersTable 資源,現在也包含了 GamesTable

    範例 JSON
    { "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "PlayersTable": { "Type": "AWS::DynamoDB::Table", "Properties": { "TableName": "Players", "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 } } } } }
  4. 更新來源堆疊,以從堆疊刪除 GamesTable 資源及其相關參數和輸出。

    aws cloudformation update-stack --stack-name SourceStackName
  5. 以下列JSON字串格式撰寫要匯入的實際資源清單及其唯一識別碼。如需詳細資訊,請參閱資源識別碼

    [{"ResourceType":"AWS::DynamoDB::Table","LogicalResourceId":"GamesTable","ResourceIdentifier":{"TableName":"Games"}}]

    或者,您也可以在組態檔案中指定 JSON-format 參數。

    例如,若要匯入GamesTable,您可以建立 ResourcesToImport.txt 包含下列組態的檔案。

    [ { "ResourceType":"AWS::DynamoDB::Table", "LogicalResourceId":"GamesTable", "ResourceIdentifier": { "TableName":"Games" } } ]
  6. 若要建立變更集,請使用下列create-change-set指令並取代預留位置文字。對於 --change-set-type 選項,請指定值為 IMPORT。對於該--resources-to-import選項,請用剛剛創建的實際字JSON符串替換示例JSON字符串。

    aws cloudformation create-change-set \ --stack-name TargetStackName --change-set-name ImportChangeSet \ --change-set-type IMPORT \ --template-body file://TemplateToImport.json \ --resources-to-import "'[{"ResourceType":"AWS::DynamoDB::Table","LogicalResourceId":"GamesTable","ResourceIdentifier":{"TableName":"Games"}}]'"
    注意

    --resources-to-import不支持內聯YAML。轉義JSON字符串中引號的要求因您的終端而異。如需詳細資訊,請參閱《使用指南》中的〈在字串內使用引號。AWS Command Line Interface

    或者,您也可以使用檔案URL做為--resources-to-import選項的輸入,如下列範例所示。

    --resources-to-import file://ResourcesToImport.txt
  7. 檢閱變更集合,確認您正在將正確的資源匯入目標堆疊。

    aws cloudformation describe-change-set \ --change-set-name ImportChangeSet
  8. 若要啟動變更集並匯入資源,請使用下列execute-change-set指令並取代預留位置文字。此時會將任何堆疊層級標籤套用到匯入的資源。成功完成操作 (IMPORT_COMPLETE) 後,資源便已順利匯入。

    aws cloudformation execute-change-set \ --change-set-name ImportChangeSet --stack-name TargetStackName
    注意

    在此匯入操作後不需要在父堆疊上執行漂移偵測,因為資源已由 AWS CloudFormation進行管理。