本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
將現有資源匯入堆疊
在此匯入操作期間,您需要提供以下資訊。
-
描述整個堆疊的範本,包括已是堆疊一部分的資源,以及要匯入的資源。要匯入的每個資源DeletionPolicy 屬性在您的範本中都必須有一個。
-
每個目標資源的唯一識別碼。請前往適當的服務主控台,以取得唯一識別碼。
在本演練中,我們會提供以下稱為 TemplateToImport.json
的範例範本。ServiceTable
目前是堆疊的一部分,GamesTable
則是匯入目標。
{ "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 } } } } }
使用 AWS Management Console 將現有資源匯入堆疊
注意
AWS CloudFormation 主控台不支援在匯入資源時使用內部函數 Fn::Transform。您可以使用 AWS Command Line Interface 來匯入使用內部函數 Fn::Transform 的資源。
請登入 AWS Management Console,開啟位於 https://console.aws.amazon.com/cloudformation
的 AWS CloudFormation 主控台。 -
在 Stacks (堆疊) 頁面上,選擇您要匯入資源的堆疊。
-
選擇 Stack actions (堆疊動作),然後選擇 Import resources into stack (將資源匯入堆疊)。
-
檢閱 Import overview (匯入概觀) 頁面,然後選擇 Next (下一步)。
-
在 Specify template (指定範本) 頁面上,使用以下其中一種方法提供您更新後的範本,然後選擇 Next (下一步)。
-
選擇 Amazon S3 URL,然後在文字方塊中為您的範本指定 URL。
-
選擇 Upload a template file (上傳範本檔案),然後瀏覽您的範本。
-
-
在 Identify resources (識別資源) 頁面上,識別每個目標資源。
-
在 Identifer property (識別碼屬性) 下方,選擇資源識別碼類型。例如,
AWS::DynamoDB::Table
資源可以使用TableName
屬性進行識別。 -
在 Identifer value (識別碼值) 下方,輸入實際的屬性值。例如,範例範本中
GamesTable
資源的TableName
是
。Games
-
選擇下一步。
-
-
在 Specify stack details (識別堆疊詳細資訊) 頁面上,更新任何參數,然後選擇 Next (下一步)。這會自動建立變更集合。
注意
如果您修改了啟動建立、更新或刪除操作的現有參數,匯入操作便會失敗。
-
在 Review
stack-name
(檢閱 stack-name) 頁面上,檢閱要匯入的資源,然後選擇 Import resources (匯入資源)。這會自動執行在最後一個步驟中建立的變更集合。此時會將任何堆疊層級標籤套用到匯入的資源。如需詳細資訊,請參閱 設定 AWS CloudFormation 堆疊選項。隨即會顯示堆疊的 Events (事件) 頁面。
-
(選用) 在 堆疊上執行漂移偵測,確認範本和匯入資源的實際組態相符。如需偵測漂移的詳細資訊,請參閱偵測整個 CloudFormation 堆疊上的漂移。
-
(選用) 如果您匯入的資源與其預期的範本組態不相符,請修正範本組態,或是直接更新資源。如需匯入漂移資源的詳細資訊,請參閱透過匯入作業解決漂移。
使用 AWS CLI 將現有資源匯入堆疊
-
選用地執行
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
-
以以下格式編寫要匯入的資源清單,以及這些資源的唯一識別碼。
[{\"ResourceType\":\"
AWS::DynamoDB::Table
\",\"LogicalResourceId\":\"GamesTable
\",\"ResourceIdentifier\":{\"TableName
\":\"Games
\"}}] -
使用下列參數建立
IMPORT
類型的變更集。--resources-to-import
不支援內嵌 YAML。>
aws cloudformation create-change-set --stack-name
TargetStack
--change-set-nameImportChangeSet
--change-set-typeIMPORT
--resources-to-import "[{\"ResourceType\":\"AWS::DynamoDB::Table
\",\"LogicalResourceId\":\"GamesTable
\",\"ResourceIdentifier\":{\"TableName
\":\"Games
\"}}]" --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" } } ]
-
檢閱變更集合,確認您將匯入正確的資源。
>
aws cloudformation describe-change-set --change-set-name
ImportChangeSet
--stack-nameTargetStack
-
執行變更集來匯入資源。此時會將任何堆疊層級標籤套用到匯入的資源。如需詳細資訊,請參閱 設定 AWS CloudFormation 堆疊選項。成功完成操作
(IMPORT_COMPLETE)
後,資源便已順利匯入。>
aws cloudformation execute-change-set --change-set-name
ImportChangeSet
--stack-nameTargetStack
-
(選用) 在
IMPORT_COMPLETE
堆疊上執行漂移偵測,確認範本和匯入資源的實際組態相符。如需偵測漂移的詳細資訊,請參閱偵測整個 CloudFormation 堆疊上的漂移。>
aws cloudformation detect-stack-drift --stack-name
TargetStack
{ "Stack-Drift-Detection-Id" : "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
-
(選用) 如果您匯入的資源與其預期的範本組態不相符,請修正範本組態,或是直接更新資源。如需匯入漂移資源的詳細資訊,請參閱透過匯入作業解決漂移。