View a markdown version of this page

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

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

在堆疊之間移動資源

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

若您是初次匯入,建議您先檢閱 將 AWS 資源匯入 CloudFormation 堆疊 主題中的簡介資訊。

重要

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

使用 重構堆疊 AWS 管理主控台

  1. 在來源範本中,為您希望移動的資源指定 Retain DeletionPolicy。

    在以下範例來源範本中,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. 開啟 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 (指定堆疊詳細資訊) 頁面上,不需要進行任何變更。選擇下一步。

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

    6. 在 檢閱 SourceStackName 頁面上,檢閱您的變更。如果您的範本包含 IAM 資源,請選取 I acknowledge that this template may create IAM resources (我知道此範本可能會建立 IAM 資源),以指定您要使用此範本中的 IAM 資源。如需有關使用範本中 IAM 資源的詳細資訊,請參閱使用 控制 CloudFormation 存取 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. 選擇下一步。

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

      重要

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

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

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

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

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

使用 重構堆疊 AWS CLI

  1. 在來源範本中,為您希望移動的資源指定 Retain DeletionPolicy。

    在以下範例來源範本中,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 格式參數。

    例如,若要匯入 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
    注意

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