從現有資源建立堆疊 - AWS CloudFormation

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

從現有資源建立堆疊

在此匯入操作期間,您需要提供以下資訊。

  • 描述將位於新堆疊中資源及資源組態的範本。範本中的每個資源都必須有一個DeletionPolicy 屬性.

  • 每個目標資源的唯一識別碼。請前往適當的服務主控台,以取得唯一識別碼。

在本演練中,我們會提供以下稱為 TemplateToImport.json 的範例範本。ServiceTableGamesTable 是匯入目標。

{ "AWSTemplateFormatVersion": "2010-09-09", "Description": "Import test", "Resources": { "ServiceTable": { "Type": "AWS::DynamoDB::Table", "DeletionPolicy": "Retain", "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 } } } } }

使用 AWS Management Console 從現有資源建立堆疊

  1. 請登入 AWS Management Console,開啟位於 https://console.aws.amazon.com/cloudformation 的 AWS CloudFormation 主控台。

  2. Stacks (堆疊) 頁面上,選擇 Create stack (建立堆疊),然後選擇 With existing resources (import resources) (使用現有資源 (匯入資源))

    
       主控台中 Create stack from existing resources (從現有資源建立堆疊) 的選項。
  3. 請閱讀 Import overview (匯入概觀) 頁面,以取得您在此操作期間必須提供的項目清單。然後選擇下一步

  4. Specify template (指定範本) 頁面上,使用以下其中一種方法提供您的範本,然後選擇 Next (下一步)

    • 選擇 Amazon S3 URL,然後在文字方塊中為您的範本指定 URL。

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

  5. Identify resources (識別資源) 頁面上,識別每個目標資源。

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

    2. Identifer value (識別碼值) 下方,輸入實際的屬性值。例如,範例範本中 GamesTable 資源的 TableNameGames

      
         主控台中的 Identify resources (識別資源) 頁面。
    3. 選擇下一步

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

    重要

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

  7. Review stack-name (檢閱 stack-name) 頁面上,確認您正在匯入正確的資源,然後選擇 Import resources (匯入資源)。這會自動執行在最後一個步驟中建立的變更集合。

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

    
       主控台中的 Events (事件) 標籤。
  8. (選用) 在 堆疊上執行漂移偵測,確認範本和匯入資源的實際組態相符。如需偵測漂移的詳細資訊,請參閱偵測整個 CloudFormation 堆疊上的漂移

  9. (選用) 如果您匯入的資源與其預期的範本組態不相符,請修正範本組態,或是直接更新資源。在本演練中,我們會修正範本組態,使其與資源的實際組態相符。

    1. 針對受影響的資源還原匯入操作

    2. 再次將匯入目標新增至您的範本,確認範本組態與實際組態相符。

    3. 使用修改後的範本重複步驟 2 到 8,再次匯入資源。

使用 AWS CLI 從現有資源建立堆疊

  1. 開啟 AWS CLI。

  2. 選用地執行 GetTemplateSummary,以了解何種屬性會識別您範本中的每個資源類型。例如,AWS::DynamoDB::Table 資源可以使用 TableName 屬性進行識別。針對範例範本中的 GamesTable 資源,TableName 的值是 Games

    > aws cloudformation get-template-summary --template-url https://DOC-EXAMPLE-BUCKET.s3.us-west-2.amazonaws.com/TemplateToImport.json
  3. 以以下格式編寫您範本中目標資源的清單,以及這些資源的唯一識別碼。

    [{\"ResourceType\":\"AWS::DynamoDB::Table\",\"LogicalResourceId\":\"GamesTable\",\"ResourceIdentifier\":{\"TableName\":\"Games\"}}]
  4. 使用下列參數建立 IMPORT 類型的變更集。--resources-to-import 不支援內嵌 YAML。

    > aws cloudformation create-change-set --stack-name TargetStack --change-set-name ImportChangeSet --change-set-type IMPORT --resources-to-import "[{\"ResourceType\":\"AWS::DynamoDB::Table\",\"LogicalResourceId\":\"GamesTable\",\"ResourceIdentifier\":{\"TableName\":\"Games\"}},{\"ResourceType\":\"AWS::DynamoDB::Table\",\"LogicalResourceId\":\"ServiceTable\",\"ResourceIdentifier\":{\"TableName\":\"Service\"}}]" --template-url https://DOC-EXAMPLE-BUCKET.s3.us-west-2.amazonaws.com/TemplateToImport.json

    AWS CLI 也支援將文字檔案作為 --resources-to-import 參數的輸入,如以下範例所示。

    --resources-to-import file://ResourcesToImport.txt

    在本逐步解說中,file://ResourcesToImport.txt 包含下列內容。

    [ { "ResourceType":"AWS::DynamoDB::Table", "LogicalResourceId":"GamesTable", "ResourceIdentifier":{ "TableName":"Games" } }, { "ResourceType":"AWS::DynamoDB::Table", "LogicalResourceId":"ServiceTable", "ResourceIdentifier":{ "TableName":"Service" } } ]
  5. 檢閱變更集合,確認您將匯入正確的資源。

    > aws cloudformation describe-change-set --change-set-name ImportChangeSet --stack-name TargetStack
  6. 執行變更集來匯入資源。成功完成操作 (IMPORT_COMPLETE) 後,資源便已順利匯入。

    > aws cloudformation execute-change-set --change-set-name ImportChangeSet --stack-name TargetStack
  7. (選用) 在 IMPORT_COMPLETE 堆疊上執行漂移偵測,確認範本和匯入資源的實際組態相符。如需偵測漂移的詳細資訊,請參閱在個別堆疊資源上偵測偏離

    > aws cloudformation detect-stack-drift --stack-name TargetStack { "StackDriftDetectionId" : "624af370-311a-11e8-b6b7-500cexample" } > aws cloudformation describe-stack-drift-detection-status --stack-drift-detection-id 624af370-311a-11e8-b6b7-500cexample > aws cloudformation describe-stack-resource-drifts --stack-name TargetStack
  8. (選用) 如果您匯入的資源與其預期的範本組態不相符,請修正範本組態,或是直接更新資源。在本演練中,我們會修正範本組態,使其與資源的實際組態相符。

    1. 針對受影響的資源還原匯入操作

    2. 再次將匯入目標新增至您的範本,確認範本組態與實際組態相符。

    3. 使用修改後的範本重複步驟 4 到 7,再次匯入資源。